emit change events for remote storage.

listen for those events being sent via postMessage
This commit is contained in:
ansuz 2016-08-30 18:11:43 +02:00
parent 30ea1b59b7
commit ae116d4772
2 changed files with 32 additions and 0 deletions

View File

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

View File

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