Zeldruck / Albion-Online-ZQRadar

Free Albion Online Radar
https://discord.gg/xpbgq7E9ya
4 stars 0 forks source link

fix: event listener memory leak #5

Closed lazycoderz1 closed 5 months ago

lazycoderz1 commented 5 months ago

@Zeldruck I think i may have found the cause for issue https://github.com/Zeldruck/Albion-Online-ZQRadar/issues/3

i found that the event listener may have been configured incorrectly

  1. the listener was attach on websocket connect event, this will cause of reattachment of a new listener every time we refresh the page while leaving the older one still exists thus hogging up the memory
  2. it doesn't remove the listener on websocket connection close

i made this changes in attempt to fix it

  1. move up the event listener for the Cap library, i think it's better to put it on the global level so that we don't create a listener every time we connected
  2. changing the websocket handle event from onconnected to onlistening, the onconnected event is called every time we made connection to the websocket that includes page refresh while the onlistening event is only called after the connection is established meaning even after we close the browser tab or refresh it, it will stay alive as long as the connection is still on.
  3. adding an event remover when the websocket connection is closed, this will free up memory as it will not leave any stale connection

i have tried it on my machine and this fixed it hope you can review and verify this, cheers :wave:

lazycoderz1 commented 5 months ago

any update @Zeldruck ?

Zeldruck commented 5 months ago

I haven't had time to look, I'll do it over the weekend @lazycoderz1