UNPKG

@artinir/nine.js

Version:

A js Event Listener With Stateful Components

69 lines (65 loc) 2.04 kB
export const NINE = { v: () => { console.info("Nine.js version 1.0.0"); }, StatefulObject: function (initalState) { this._state = initalState; this._eventListeners = {}; this._deactivated = new Set(); this.addListener = (name, eventListener) => { this._eventListeners[name] = eventListener; }; this._callListener = (name) => { if (!this._deactivated.has(name)) this._eventListeners[name](this._state); }; this._callListeners = () => { Object.keys(this._eventListeners).forEach((key, index) => { this._callListener(key); }); }; this.setState = (changedState) => { this._state = Object.assign(this._state, changedState); this._callListeners(); }; this.getState = () => { return this._state; }; this.deavtivate = (name) => { this._deactivated.add(name); }; this.avtivate = (name) => { if (this._deactivated.has(name)) { this._deactivated.delete(name); } }; this.removeListener = (name) => { this.avtivateListener(name); this._eventListeners[name] = null; }; }, Subscriber: function (value, resolver) { this.last_value; this._subscription = true; this.unsubscribe = () => { this._subscription = false; }; this.subscribe = () => { this._subscription = true; }; while (this._subscription) { this._last_value = value(); if (this.last_value !== this._last_value) { resolver(this._last_value, () => { this.unsubscribe(); }); } } } }; export default function printMsg() { console.log("Nine.js v.1.0.0 has been installed!!!"); } if (typeof window !== "undefined") { window.NINE = NINE; }