From 43fcb294908780ace1114384c86a9f3a10cd63a3 Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 30 Aug 2016 18:00:28 +0200 Subject: [PATCH] listen for changes to localStorage and execute callbacks --- customize.dist/store.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/customize.dist/store.js b/customize.dist/store.js index 287b95644..5c55f7eb2 100644 --- a/customize.dist/store.js +++ b/customize.dist/store.js @@ -71,5 +71,23 @@ define(function () { } }; + var changeHandlers = Store.changeHandlers = []; + + Store.change = function (f) { + if (typeof(f) !== 'function') { + throw new Error('[Store.change] callback must be a function'); + } + changeHandlers.push(f); + + if (changeHandlers.length === 1) { + // start listening for changes + window.addEventListener('storage', function (e) { + changeHandlers.forEach(function (f) { + f(e); + }); + }); + } + }; + return Store; });