wire-elements / spotlight

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
MIT License
911 stars 71 forks source link

How to persist data while Spotlight is open? #58

Closed LTKort closed 2 years ago

LTKort commented 2 years ago

Hi,

Upon opening Spotlight & starting a command, I want to temporary store some data when when Spotlight is open. In my case all the available Resources from Laravel Nova.

I can store some data when the page is opens, but after starting Spotlight & every keystroke after, the component refreshes and the data is lost. My solution now is to store the data in the session, and flush it on execution, but this is not the most stable solution.

Is it possible to execute a function on start of a command, and keep this data while spotlight is open? After executing the data can be flushed.

PhiloNL commented 2 years ago

Hi,

Would you mind explaining your use case? Maybe add some example code of how you would like things to work. Because I don't exactly understand what you are trying to achieve.

LTKort commented 2 years ago

I want to call a function that retrieves data once, and I want to use this same data in the whole command.

For example:

/**
 * Defining dependencies is optional. If you don't have any dependencies you can remove this method.
 * Dependencies are asked from your user in the order you add the dependencies.
 */
public function dependencies(): ?SpotlightCommandDependencies
{
    ray('getDataFunction'); // Example Function

    return SpotlightCommandDependencies::collection()->add(
        SpotlightCommandDependency::make('resources')
            ->setPlaceholder('Search for a result...')
    );
}

On page load it executes once, when I select the command in spotlight it is executed, and on every keystroke after the function is executed.

I want to call it only once, store the data and use it through the whole SpotlightCommand. Is there a way to do this ?

BTW, my use case is with Laravel Nova, I want to access the available Nova Resources from the initial Nova request. But this is only called once on page load, so on initial execution the data should be stored. Only when starting Spotlight commands the request data isn't available anymore, because it isn't a nova request anymore.

So instead of ;

ray('getData');

I do

$resources = Nova::globallySearchableResources($request);

In short, I want to use a global scope throughout the command, but upon each action the global scopes are cleared.

PhiloNL commented 2 years ago

I don't see any way of doing this at the moment but feel free to suggest a PR.