Skip to content

[firebase-database] Unsubscribe to event still doesn't work #141

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

Closed
danouche93 opened this issue Oct 12, 2022 · 2 comments
Closed

[firebase-database] Unsubscribe to event still doesn't work #141

danouche93 opened this issue Oct 12, 2022 · 2 comments

Comments

@danouche93
Copy link

After opening 2 tickets #109 #120, the method "off()" to unsubscribe from event still isn't working.

@triniwiz triniwiz added the bug Something isn't working label Oct 13, 2022
triniwiz added a commit that referenced this issue Oct 19, 2022
@triniwiz triniwiz removed the bug Something isn't working label Oct 19, 2022
@triniwiz
Copy link
Member

Hey @danouche93 I added a test for this lmk if that works for you.

@danouche93
Copy link
Author

danouche93 commented Oct 20, 2022

Thank you @triniwiz
I finally found what was the problem. When I called the off() function, I was re-creating a ref to the database path like firebase().database().ref(path), so by doing that it wasn't unsubscribing from the event because it didn't have the right ref.
To fix it, I had to save the ref path to an array so that I could access it to unsubscribe from the event like so :

listenerWrappers: any[] = [];
EventListener(onValueEvent: any, path: string): any {
    var ref = firebase().database().ref(path);
    const listener = ref.on('value', onValueEvent);
    this.listenerWrappers.push({ listener: listener, path: path, ref: ref });
  }

RemoveListeners(path: string) {
    var listenerWrapper = this.listenerWrappers.filter(l => l.path == path)[0];
    if (listenerWrapper) {
      listenerWrapper.ref.off('value', listenerWrapper.listener)
      this.listenerWrappers.splice(this.listenerWrappers.indexOf(listenerWrapper), 1);
    }
  }

I hope it will help people that had the same issue !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants