From 97722c777bc7858f530ae4302bb9474337c26e81 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Sat, 8 Dec 2018 01:06:08 -0800 Subject: [PATCH] fix Nth subscriptions, localStorage on file:// after Chrome deleted it (jerks) --- gun.js | 9 +++++---- test/common.js | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/gun.js b/gun.js index 23055646..a9cb3734 100644 --- a/gun.js +++ b/gun.js @@ -1319,9 +1319,10 @@ msg = obj_to(msg, {put: data = tmp.put}); } } - if((tmp = root.mum) && at.id){ - if(tmp[at.id]){ return } - if(u !== data && !rel.is(data)){ tmp[at.id] = true; } + if((tmp = root.mum) && at.id){ // TODO: can we delete mum entirely now? + var id = at.id + (eve.id || (eve.id = Gun.text.random(9))); + if(tmp[id]){ return } + if(u !== data && !rel.is(data)){ tmp[id] = true; } } as.use(msg, eve); if(eve.stun){ @@ -1778,7 +1779,7 @@ try{store = (Gun.window||noop).localStorage}catch(e){} if(!store){ console.log("Warning: No localStorage exists to persist data to!"); - store = {setItem: noop, removeItem: noop, getItem: noop}; + store = {setItem: function(k,v){this[k]=v}, removeItem: function(k){delete this[k]}, getItem: function(k){return this[k]}}; } /* NOTE: Both `lib/file.js` and `lib/memdisk.js` are based on this design! diff --git a/test/common.js b/test/common.js index d933b469..5b61d1e8 100644 --- a/test/common.js +++ b/test/common.js @@ -6,7 +6,7 @@ describe('Gun', function(){ if(typeof global !== 'undefined'){ env = global } if(typeof window !== 'undefined'){ env = window } root = env.window? env.window : global; - env.window && root.localStorage && root.localStorage.clear(); + try{ env.window && root.localStorage && root.localStorage.clear() }catch(e){} try{ require('fs').unlinkSync('data.json') }catch(e){} //root.Gun = root.Gun || require('../gun'); if(root.Gun){ @@ -3703,6 +3703,28 @@ describe('Gun', function(){ done(); }); }); + + it('Multiple subscribes should trigger', function(done){ + var gun = Gun(); + var check = {}; + gun.get('m/s/key').put({property: 'value'}); + + gun.get('m/s/key').on(function(data, key){ + check['a'+data.property] = 1; + }); + + gun.get('m/s/key').on(function(data, key){ + check['b'+data.property] = 1; + if(check.avalue && check.bvalue && check.anewValue && check.bnewValue){ + if(done.c){ return } done.c = true; + done(); + } + }); + + setTimeout(function(){ + gun.get('m/s/key').put({property: 'newValue'}); + }, 1000); + }); return; it('Nested listener should be called', function(done){