wailsapp / wails

Create beautiful applications using Go
https://wails.io
MIT License
25.39k stars 1.23k forks source link

Is there any way to "unsubscribe" from an event? #405

Closed vicsnow closed 3 years ago

vicsnow commented 4 years ago

Description I am running a project, having appRun as frontend. There are two containers, I switch between, and both receive different events.

So I do Wails.Events.On when I mount the component And it seems, that I need to unsub or ignore other component's events on unmount So, like the title says, is there any way to "unsubscribe" from an event? Or I just need to have some additional backend logic, which does only one event emission at a time?

Additional context It looks like this: I have <Data1 /> and <Data2 /> containers Data1 has Wails.Events.On("update_data1", ()=>{ ***update logic***}) when it's mounted Data2 has Wails.Events.On("update_data2", ()=>{ ***update logic***}) when it's mounted Let's say, that <Data1/> is currently rendered, after I did render <Data2/> and call it's mounted method Receiving both update_data1 and update_data2 events leads to incorrect rendering So I need to either "unsubscribe" from update_data2 event on <Data2/> unmount , or have some workaround logic, which allows emitting only one event at a time

leaanthony commented 4 years ago

There currently isn't an unsubscribe feature but there probably should be. In your particular case, it sounds like you just need to ignore data2 events when data1 is mounted. If that's really what you want to do then potentially your listener can just test to see which component is mounted and ignore events for the hidden container. I guess I don't get why both containers are listening to each others events. Whilst I think having unsubscribe may be useful, it doesn't feel like that's the right solution for your particular problem.

vicsnow commented 4 years ago

Yea, okay, testing to see which component is mounted sounds like a reasonable solution. And that is an optimization anyways

Jmeigs1 commented 3 years ago

I am running into this issue using react. Binding Wails.Events.On with useEffect leads to the callback being bound multiple times, run when the component is not mounted, etc. Workarounds are possible but its very idiomatic (in react at least) to bind and unbind as part of the component lifecycle.

Any chance this could be re-opened?

leaanthony commented 3 years ago

Funny timing. I just added this to v2. Should be relatively easy to backport.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.