module.exports = function patchGunWithCancel(Gun) { Gun.chain.onWithCancel = (function() { function cancelCallback(data,key,msg,ev) { if (ev && typeof ev.off === 'function') { ev.off(); } } return function(tag, arg, eas, as) { if (typeof tag === 'function') { let callback = tag; const cancelEv = function() { callback = cancelCallback; }; const wrapper = function() { return callback.apply(this, arguments); }; this.on(wrapper, arg, eas, as); return cancelEv; } else { this.on(tag, arg, eas, as); return null; } }; })(); };