UNPKG

ts-event-bus

Version:
67 lines (51 loc) 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GenericChannel = void 0; const DEFAULT_TIMEOUT = 5000; class GenericChannel { constructor(_timeout = DEFAULT_TIMEOUT) { this._timeout = _timeout; this._onMessageCallbacks = []; this._onConnectCallbacks = []; this._onDisconnectCallbacks = []; this._onErrorCallbacks = []; this._ready = false; } get timeout() { return this._timeout; } onData(cb) { if (this._onMessageCallbacks.indexOf(cb) === -1) { this._onMessageCallbacks.push(cb); } } onConnect(cb) { if (this._ready) { cb(); } this._onConnectCallbacks.push(cb); } onDisconnect(cb) { this._onDisconnectCallbacks.push(cb); } onError(cb) { this._onErrorCallbacks.push(cb); } _messageReceived(message) { this._onMessageCallbacks.forEach(cb => cb(message)); } _error(error) { this._onErrorCallbacks.forEach(cb => cb(error)); } _connected() { this._ready = true; this._onConnectCallbacks.forEach(cb => cb()); } _disconnected() { this._ready = false; this._onDisconnectCallbacks.forEach(cb => cb()); } } exports.GenericChannel = GenericChannel;