Created
March 4, 2020 02:58
-
-
Save ronisaha/6ee39ba2969fa0be4a47f913653b2ec3 to your computer and use it in GitHub Desktop.
Inter tab messaging library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| var WindowController = (function () { | |
| function WindowController() { | |
| this.id = Math.random(); | |
| this.callbacks = {}; | |
| window.addEventListener('storage', this, false); | |
| window.addEventListener('unload', this, false); | |
| } | |
| WindowController.prototype.handleEvent = function (event) { | |
| if (event.type === 'unload') { | |
| this.destroy(); | |
| } | |
| else if (event.key === 'broadcast') { | |
| try { | |
| var data_1 = JSON.parse(event.newValue); | |
| if (data_1.id !== this.id) { | |
| if (typeof this[data_1.type] === 'function') { | |
| this[data_1.type](data_1); | |
| return; | |
| } | |
| if (this.callbacks[data_1.type]) { | |
| this.callbacks[data_1.type].forEach(function (fn) { return fn(data_1); }); | |
| } | |
| } | |
| } | |
| catch (error) { | |
| } | |
| } | |
| }; | |
| WindowController.prototype.destroy = function () { | |
| window.removeEventListener('storage', this, false); | |
| window.removeEventListener('unload', this, false); | |
| }; | |
| WindowController.prototype._broadcast = function (type, data) { | |
| var event = { | |
| id: this.id, | |
| type: type | |
| }; | |
| if (data) { | |
| event['data'] = data; | |
| } | |
| try { | |
| localStorage.setItem('broadcast', JSON.stringify(event)); | |
| } | |
| catch (error) { | |
| } | |
| }; | |
| WindowController.prototype.broadcast = function (type, data) { | |
| if (typeof this[type] === 'function') { | |
| throw Error('Reserved events'); | |
| } | |
| this._broadcast(type, data); | |
| }; | |
| WindowController.prototype.on = function (name, callback) { | |
| if (typeof this[name] === 'function') { | |
| throw new Error('Reserved callback'); | |
| } | |
| if (typeof this.callbacks[name] === 'undefined') { | |
| this.callbacks[name] = []; | |
| } | |
| this.callbacks[name][this.callbacks[name].length] = callback; | |
| return this; | |
| }; | |
| WindowController.prototype.off = function (name, callback) { | |
| if (this.callbacks[name] === undefined) { | |
| return; | |
| } | |
| if (callback === undefined || callback === null) { | |
| delete this.callbacks[name]; | |
| } | |
| else { | |
| var index = this.callbacks[name].indexOf(callback, 0); | |
| if (index > -1) { | |
| this.callbacks[name].splice(index, 1); | |
| } | |
| } | |
| }; | |
| return WindowController; | |
| }()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| class WindowController implements EventListenerObject { | |
| private readonly id = Math.random(); | |
| private callbacks: { [key: string]: Array<(data) => {}> } = {}; | |
| constructor() { | |
| window.addEventListener('storage', this, false); | |
| window.addEventListener('unload', this, false); | |
| } | |
| handleEvent(event) { | |
| if (event.type === 'unload') { | |
| this.destroy(); | |
| } else if (event.key === 'broadcast') { | |
| try { | |
| const data = JSON.parse(event.newValue); | |
| if (data.id !== this.id) { | |
| if (typeof this[data.type] === 'function') { | |
| this[data.type](data); | |
| return; | |
| } | |
| if (this.callbacks[data.type]) { | |
| this.callbacks[data.type].forEach(fn => fn(data)); | |
| } | |
| } | |
| } catch (error) { | |
| } | |
| } | |
| } | |
| destroy() { | |
| window.removeEventListener('storage', this, false); | |
| window.removeEventListener('unload', this, false); | |
| } | |
| private _broadcast(type, data?) { | |
| let event = { | |
| id: this.id, | |
| type: type | |
| }; | |
| if (data) { | |
| event['data'] = data; | |
| } | |
| try { | |
| localStorage.setItem('broadcast', JSON.stringify(event)); | |
| } catch (error) { | |
| } | |
| } | |
| broadcast(type, data?) { | |
| if (typeof this[type] === 'function') { | |
| throw Error('Reserved events'); | |
| } | |
| this._broadcast(type, data); | |
| } | |
| on(name: string, callback: (data) => {}) { | |
| if (typeof this[name] === 'function') { | |
| throw new Error('Reserved callback'); | |
| } | |
| if (typeof this.callbacks[name] === 'undefined') { | |
| this.callbacks[name] = []; | |
| } | |
| this.callbacks[name][this.callbacks[name].length] = callback; | |
| return this; | |
| } | |
| off(name: string, callback?: (data) => {}) { | |
| if (this.callbacks[name] === undefined) { | |
| return; | |
| } | |
| if (callback === undefined || callback === null) { | |
| delete this.callbacks[name]; | |
| } else { | |
| const index = this.callbacks[name].indexOf(callback, 0); | |
| if (index > -1) { | |
| this.callbacks[name].splice(index, 1); | |
| } | |
| } | |
| } | |
| } | |
| export default new WindowController(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment