Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement UI event dispatcher/listener #4492

Merged
merged 14 commits into from
Feb 26, 2024
Merged

Implement UI event dispatcher/listener #4492

merged 14 commits into from
Feb 26, 2024

Conversation

Tetrax-10
Copy link
Contributor

@Tetrax-10 Tetrax-10 commented Jan 23, 2024

Implemented events

Now Stash ui client can create events that can be used by both plugins and stash ui client to do stuffs on a event. For now I have only implemented stash:location event which gets triggered on route change.

Plugin side usage for the event stash:location

PluginApi.Event.addEventListener("stash:location", (e) => console.log("Page Changed", e.detail.data.location.pathname))

Stash Userscript library uses setInterval of 100ms to check for page (href) change, which is performance intense they had no other choice. So now plugins like that can use this event which is very performance friendly. Its not just for page change event devs can implement more useful events like this.

Stash ui client usage for creating a event

To create a stash:page:scenes event, put this code here

Event.dispatch("page", "scenes", { scenes: result.data.findScenes.scenes});

Plugin side code for above event

// prints all scenes when page is /scenes
PluginApi.Event.addEventListener("stash:page:scenes", (e) => console.log("scenes page", e.detail.data.scenes))

Note: Stash ui client can also listen for this event by using Event.addEventListener

@WithoutPants WithoutPants added the feature Pull requests that add a new feature label Jan 24, 2024
@WithoutPants WithoutPants added this to the Version 0.25.0 milestone Jan 24, 2024
@WithoutPants WithoutPants changed the title Implement events Implement UI event dispatcher/listener Jan 24, 2024
@WithoutPants WithoutPants removed this from the Version 0.25.0 milestone Feb 23, 2024
@WithoutPants
Copy link
Collaborator

Putting this on hold until I can determine the necessity of this change. I am not convinced that listening for elements and page changes is the correct approach.

@Tetrax-10
Copy link
Contributor Author

Tetrax-10 commented Feb 23, 2024

I don't have a better solution for avoiding this infinite loop, this loop boosts up my CPU usage, memory usage and starts to lag after few hours. Event listeners are basically used to solve these kinds of issues. With this PR that infinite loop code can be replaced with this event listener. Also there is no element listener in my PR. This stash:page:scenes thing was just an example/guide to use PluginApi.Event.

This implementation is very much accepted in Stack Over Flow. If you dont want to use useEffect then the alternative is to implement this one which is recommended by react-router.

<Route path="/" component={App} onEnter={() => Event.dispatch("location", "", { location })} />

@WithoutPants
Copy link
Collaborator

I was referring to plugins using event listeners to know when to manipulate the dom. It's not a very react way of doing things. However, I get your point, and we should support someone like this to avoid the hacks you describe. It would be possible to insert something like this into a plug-in, but an officially supported way is probably better. I'll take another look at this on Monday.

@WithoutPants WithoutPants added this to the Version 0.25.0 milestone Feb 26, 2024
@WithoutPants WithoutPants merged commit 6a9175c into stashapp:develop Feb 26, 2024
2 checks passed
@Tetrax-10 Tetrax-10 deleted the implement-events branch February 27, 2024 00:05
DogmaDragon added a commit to stashapp/Stash-Docs that referenced this pull request Mar 8, 2024
halkeye pushed a commit to halkeye/stash that referenced this pull request Sep 1, 2024
* page change event
* expose event to plugin api
* Update UIPluginApi.md
* Add to example plugin
---------
Co-authored-by: WithoutPants <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Pull requests that add a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants