I did .on to subscribed to data. To unsubscribe you use .off(). Once I used .off() I no longer receive back data from the data point I unsubscribe to.
Have to restart session to reset flags.
@fuchidahiro is it something like:
gun.get('a').on(cb1);
gun.get('a').once(cb2);
gun.get('a').off(); // this SHOULD unsubscribe everything prior
gun.get('a').on(cb3); // should NOT unsubscribe this
Are you referencing cb3 doesn't work?
I was working in the console and did:
gun.get('model').get('Person').get('John').put({name:"John"})
gun.get('model').get('Person').on(print, true)
gun.get('model').get('Person').get('Tim').put({name:"Tim"})
/* Logs "Tim:[Object object] */
gun.get('model').get('Person').off()
gun.get('model').get('Person').get('Tim').once(print)
/* Logs nothing which implies print function receives no variables */
Which would say cb3 doesn't work
I restart and run ...get('Tim').once(print)
Logs Tim:[Object object] again
:( will need to fix this.
I'm having the same issue. If I try to call .on() or .once() after calling .off() in a node I just don't receive any data (The callback is not called).
I'm pretty sure this is caused by one/some of these obj_del calls in the Gun.chain.off function.
@amark
@brapifra welcome to the community! Thanks for jumping on this issue also.
There is a high probability this is related to the obj_del as you say.
There is also a chance it is related to https://github.com/amark/gun/issues/632 which is now fixed locally and will be published soon. I'll have you retry .off() after that, and if it still doesn't work, then it'll be easier for me to isolate - I wish I had more time to debug, let alone code, but it is pretty precious these days so I want to optimize time as much as possible.
@brapifra @Dletta just checked this against latest NPM version and it is fixed.
Thank you for reporting!!!!! Please reopen if you find any other problems.
I'm running into something similar again in browser.
var chain = gun.get("foo").on(receiveFn)
// later
chain.off()
chain.get("foo").once(receiveFn) // receiveFn never gets called
Code to reproduce:
const Gun = require("gun");
const gun = Gun({ peers: ["https://notabug.io/gun"] });
const soul = "nab/things/59382d2a08b7d7073415b5b6ae29dfe617690d74";
const chain = gun
.get(soul)
.get("data")
.on(res => console.log("on", res));
chain.off();
gun
.get(soul)
.get("data")
.once(res => console.log("once", res));
Only happens when the .get("data") is present. the once bit never gets logged.
Timing seems to be a factor, this will more reliably produce the issue:
setTimeout(() => {
const chain = gun
.get(soul)
.get("data")
.on(res => console.log("on", res));
setTimeout(() => {
chain.off();
setTimeout(() => {
gun
.get(soul)
.get("data")
.once(res => console.log("once", res));
}, 500);
}, 500);
}, 500);
not working for me neither and found this on stackoverflow
https://stackoverflow.com/questions/56198668/how-to-restart-subscription-on-gundb-node
i've tried looking at the 'on' source code, but its cryptic, @amark if you could write a short description of the logic how 'on' works and what 'off' does to remove it, maybe I could take a look at it.
Sorry all! :( :( I think @rogowski might have "worked around this" in his AXE branch by doing gun.get(soul)._.ack = 0; gun.get(soul).get('data')._.ack = 0.
I just commented about this on the link you mentioned, https://stackoverflow.com/questions/56198668/how-to-restart-subscription-on-gundb-node . @sirpy thanks for volunteering, could you try this quickfix out, see if it works?
I thought it worked once, but I misconfigured the test environment for the workaround. I think simply node._.ack = 0 does nothing here i.e. the callback passed to on before off still called.
:sob:
I'm also willing to attempt a fix at this bug, and I second @sirpy's request for some explaining of the code. I would like to very kindly suggest the adoption of more descriptive variable names and an occasional code comment. Doesn't really pay off to use short variable names when it prevents people from lending a hand.
Is this the right spot?
https://github.com/amark/gun/blob/3a7975a2356c1cd1db81579030ea303204c256c8/gun.js#L1601
@rm-rf-etc sweet, I'd love to do little explainer videos of code - would you be able to record & upload it? If so, let's shoot for Thursday at 5pm CA time?
@amark totally. That time works for me.
awesome, please keep https://gun.eco/docs/javascript in mind as the basis for the code/internals what will be explained.
https://youtu.be/08MUJdIy4XA for future reference.

Most helpful comment
I'm also willing to attempt a fix at this bug, and I second @sirpy's request for some explaining of the code. I would like to very kindly suggest the adoption of more descriptive variable names and an occasional code comment. Doesn't really pay off to use short variable names when it prevents people from lending a hand.
Is this the right spot?
https://github.com/amark/gun/blob/3a7975a2356c1cd1db81579030ea303204c256c8/gun.js#L1601