From ae116d47729ff1a59bd689071b8709f4d5aa32af Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 30 Aug 2016 18:11:43 +0200 Subject: [PATCH] emit change events for remote storage. listen for those events being sent via postMessage --- customize.dist/share/frame.js | 22 ++++++++++++++++++++++ customize.dist/share/respond.js | 10 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/customize.dist/share/frame.js b/customize.dist/share/frame.js index 0e32593c8..86147ca4b 100644 --- a/customize.dist/share/frame.js +++ b/customize.dist/share/frame.js @@ -49,6 +49,15 @@ }); }; + var changeHandlers = frame.changeHandlers = []; + + var change = frame.change = function (f) { + if (typeof(f) !== 'function') { + throw new Error('[Frame.change] expected callback'); + } + changeHandlers.push(f); + }; + var _listener = function (e) { if (!frame.accepts(e.origin)) { console.log("message from %s rejected!", e.origin); @@ -63,6 +72,14 @@ console.log("No uid!"); return; } + + if (uid === 'change' && changeHandlers.length) { + changeHandlers.forEach(function (f) { + f(data); + }); + return; + } + if (timeouts[uid]) { window.clearTimeout(timeouts[uid]); } @@ -89,6 +106,11 @@ }; var id = req._uid = uid(); + // uid must not equal 'change' + while(id === 'change') { + id = req._uid = uid(); + } + if (typeof(cb) === 'function') { //console.log("setting callback!"); listeners[id] = cb; diff --git a/customize.dist/share/respond.js b/customize.dist/share/respond.js index b5ae4671e..7001ed16e 100644 --- a/customize.dist/share/respond.js +++ b/customize.dist/share/respond.js @@ -81,3 +81,13 @@ window.addEventListener('message', function(e) { } }); +window.addEventListener('storage', function (ev) { + parent.postMessage(JSON.stringify({ + _uid: 'change', + data: { + key: ev.key, + oldValue: ev.oldValue, + newValue: ev.newValue, + } + }), '*'); +});