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.
How to unsubscribe gundb on event function?
360 views Asked by Lightnet At
2
There are 2 answers
0
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();
});
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:
Basically, the event emitter gets attached to the options object as the
on
property, which you can call.off()
later. I hope this helps!