How to unsubscribe gundb on event function?

360 views Asked by At

How do I stop events triggered by gun('something').on() for gundb a handler (to unsubscribe) so it would stop update the list or changes to the list.

2

There are 2 answers

1
marknadal On

In 0.5 and above you simply call .off().

However, you can't in previous versions. There is a work around though, here is how to do it:

var options = {};

gun.get('something').on(callback, options);

options.on.off()

Basically, the event emitter gets attached to the options object as the on property, which you can call .off() later. I hope this helps!

0
Jonas V On

On the callback to the .on call you get a few arguments.
(value, key, message, onEvent) => void
The last of these arguments contains it's own .off function that specifically removes the listener that the onEvent was handed to.

A relatively simple solution would then be:

let off = ()=>{};
chain.on((value, key, message, evt)=>{
  callback(value, key, message, evt);
  off = ()=> evt.off();
});