emit change events for remote storage.

listen for those events being sent via postMessage
pull/1/head
ansuz 8 years ago
parent 30ea1b59b7
commit ae116d4772

@ -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;

@ -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,
}
}), '*');
});

Loading…
Cancel
Save