UNPKG

@panyam/tsutils

Version:

Some basic TS utils for personal use

209 lines 5.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StateMachine = exports.EventHub = exports.EventEmitter = exports.State = exports.TEvent = void 0; class TEvent { constructor(name, source, payload) { this.uuid = TEvent.counter++; this._spawnedFrom = null; this.sourceState = null; this.cancelled = false; this.timeStamp = -1; this.children = []; this.name = name; this.source = source; this.payload = payload; } get spawnedFrom() { return this._spawnedFrom; } setSpawnedFrom(msg) { this._spawnedFrom = msg; if (msg == null) this._rootEvent = this; else this._rootEvent = msg.rootEvent; } spawn(name, source, payload) { const child = new TEvent(name, source, payload); child.setSpawnedFrom(this); this.children.push(child); return child; } get rootEvent() { return this._rootEvent; } } exports.TEvent = TEvent; TEvent.counter = 0; class State { constructor() { this.stateData = null; this.id = State.counter++; } get name() { return this.constructor.name; } enter(data) { this.stateData = data; } handle(event) { } } exports.State = State; State.counter = 0; class EventEmitter { constructor() { this._eventHub = new EventHub(); } get eventHub() { return this._eventHub; } set eventHub(hub) { const oldHub = this._eventHub; this._eventHub = hub; this.eventHubChanged(oldHub); } eventHubChanged(oldHub) { console.log("WARNING - EventHub Change Listener not implemented: ", this.constructor.name); } } exports.EventEmitter = EventEmitter; class EventHub { constructor() { this._handlers = {}; this._events = []; this._inBatchMode = false; } on(names, callback) { return this._addHandler(names, this._handlers, callback); } removeOn(names, callback) { return this._removeHandler(names, this._handlers, callback); } _ensurestrings(names) { if (typeof names === "string") { names = names.split(","); } return names.map(function (v) { return v.trim(); }); } _addHandler(names, handlerlist, handler) { this._ensurestrings(names).forEach(function (name) { handlerlist[name] = handlerlist[name] || []; handlerlist[name].push(handler); }); return this; } _removeHandler(names, handlerlist, handler) { this._ensurestrings(names).forEach(function (name) { const evHandlers = handlerlist[name] || []; for (let i = 0; i < evHandlers.length; i++) { if (evHandlers[i] == handler) { evHandlers.splice(i, 1); break; } } }); return this; } emit(name, source, payload) { const evt = new TEvent(name, source, payload); if (this._inBatchMode) { this._events.push(evt); return true; } else { return this.dispatchEvent(evt); } } dispatchEvent(event) { const evtCallbacks = this._handlers[event.name] || []; for (const callback of evtCallbacks) { callback(event); if (event.cancelled) return false; } return true; } startBatchMode() { if (!this._inBatchMode) { this._inBatchMode = true; } return this; } cancelBatch() { this._inBatchMode = false; this._events = []; } commitBatch() { this._inBatchMode = false; this.emit(EventHub.BATCH_EVENTS, this, this._events); this._events = []; } } exports.EventHub = EventHub; EventHub.BATCH_EVENTS = "BatchEvents"; class StateMachine { constructor() { this._states = {}; this._rootState = null; this._currentState = null; this._states = {}; this._rootState = null; this._currentState = null; } set rootState(name) { this._rootState = this.getState(name); if (this._currentState == null) { this._currentState = this._rootState; } } enter(state, data = null) { if (state == "") { this._currentState = this._rootState; } else { this._currentState = this.getState(state); } if (this._currentState != null) { this._currentState.enter(data); } } getState(name) { if (!(name in this._states)) { throw Error("State '" + name + "' not yet registered."); } return this._states[name]; } registerState(state, isRoot = false) { const name = state.name; if (name in this._states) { throw Error("State '" + name + "' already registered."); } this._states[name] = state; if (isRoot || false) { this.rootState = state.name; } } handle(event) { if (this._currentState == null) return; const nextState = this._currentState.handle(event); if (nextState != null) { if (nextState == "") { if (this._rootState != null) { this.enter(this._rootState.name); } else { throw new Error("Root state has not been set"); } } else { this.enter(nextState); } } } } exports.StateMachine = StateMachine; //# sourceMappingURL=events.js.map