Author: smaisonneuve Date: 2015-07-09 19:37:32 +0200 (Thu, 09 Jul 2015) New Revision: 1795 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1795 Log: [Utils] Adding a storage utility to share data into localStorage Added: oipf/lib/js/utils/Storage.js Modified: oipf/lib/js/impl/VideoBroadcastObject.js oipf/lib/paths.json Modified: oipf/lib/js/impl/VideoBroadcastObject.js =================================================================== --- oipf/lib/js/impl/VideoBroadcastObject.js 2015-07-09 13:51:59 UTC (rev 1794) +++ oipf/lib/js/impl/VideoBroadcastObject.js 2015-07-09 17:37:32 UTC (rev 1795) @@ -159,11 +159,18 @@ this._timerManager = new TimerManager(); this._timeout = this._timerManager.createTimer.bind(this._timerManager, 0); + this._storage = new Storage(); } createdCallback() { console.log("[INFO] createdCallback called."); this.init(); + + // If multiple video/bcast lives in different windows they should be kept in sync + var changePlayState = this.changePlayState.bind(this); + this._storage.addListener("playState", function(event) { + changePlayState(parseInt(event.newValue)); + }) } _setChannelService() { @@ -239,7 +246,8 @@ * • Number state: * the new state of the video/broadcast object. Valid values are given in the definition of the playState property above. * - * • Number error – if the state has changed due to an error, this field contains an error code detailing the type of error. See the definition of * onChannelChangeError above for valid values. If no error has occurred, this argument SHALL take the value undefined . + * • Number error – if the state has changed due to an error, this field contains an error code detailing the type of error. + * See the definition of onChannelChangeError above for valid values. If no error has occurred, this argument SHALL take the value undefined . */ get onPlayStateChange() { return this._getCallback("PlayStateChange"); Added: oipf/lib/js/utils/Storage.js =================================================================== --- oipf/lib/js/utils/Storage.js (rev 0) +++ oipf/lib/js/utils/Storage.js 2015-07-09 17:37:32 UTC (rev 1795) @@ -0,0 +1,54 @@ +/** + * This class is meant to handle the localStorage data source + **/ +class Storage { + + constructor() { + let listeners = this.listeners = []; + window.addEventListener("storage", function(event) { + let listenersOnKey = listeners[event.key]; + if (listenersOnKey) { + for (let listener of listenersOnKey) { + listener(event); + } + } + }); + } + + /** + * Add a storage listener when a specific key is modified within the localStorage + * @param {String} key the key to listen for modifications + * @param {Function} callback the callback to call on when a modification has been detected + */ + addListener(key, callback) { + this.listeners[key] = this.listeners[key] || []; + this.listeners[key].push(callback); + } + + removeListener(key, callback) { + let listeners = this.listeners[key]; + let index = listeners.indexOf(callback); + if (index >= 0) { + listeners.splice(index, 1); + } + } + + set(key, value) { + // Let's stringify the value to be able to store objects + let string = JSON.stringify(value); + localStorage.setItem(key, string); + } + + get(key) { + let value = localStorage.getItem(key); + return JSON.parse(value); + } + + remove(key) { + localStorage.removeItem(key); + } + + clear() { + localStorage.clear(); + } +} Modified: oipf/lib/paths.json =================================================================== --- oipf/lib/paths.json 2015-07-09 13:51:59 UTC (rev 1794) +++ oipf/lib/paths.json 2015-07-09 17:37:32 UTC (rev 1795) @@ -22,6 +22,7 @@ "js/utils/XmlToJson.js", "js/utils/ProgrammeHelper.js", "js/utils/ComplexPromise.js", + "js/utils/Storage.js", "configuration/ConfigDefaultProperties.js", "configuration/CapabilitiesProperties.js", "configuration/OipfConfiguration.js",
participants (1)
-
smaisonneuve@users.nuiton.org