Lightnet
Lightnet

Reputation: 91

How to unsubscribe gundb on event function?

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.

Upvotes: 3

Views: 371

Answers (2)

Jonas V
Jonas V

Reputation: 786

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();
});

Upvotes: 0

marknadal
marknadal

Reputation: 7624

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!

Upvotes: 1

Related Questions