UNPKG

@aeternity/aepp-sdk

Version:

SDK for the æternity blockchain

169 lines (167 loc) 8.64 kB
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); } function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); } function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } function _classPrivateGetter(s, r, a) { return a(_assertClassBrand(s, r)); } function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; } function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); } function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } /* eslint-disable max-classes-per-file */ import WebSocket from 'isomorphic-ws'; import { BaseError, UnexpectedTsError, InternalError } from './utils/errors.js'; var Source = /*#__PURE__*/function (Source) { Source["Middleware"] = "mdw"; Source["Node"] = "node"; Source["All"] = "all"; return Source; }(Source || {}); export class MiddlewareSubscriberError extends BaseError { constructor(message) { super(message); this.name = 'MiddlewareSubscriberError'; } } export class MiddlewareSubscriberDisconnected extends MiddlewareSubscriberError { constructor(closeEvent) { super('Connection closed'); this.closeEvent = closeEvent; this.name = 'MiddlewareSubscriberDisconnected'; } } /** * @category middleware */ var _subscriptions = /*#__PURE__*/new WeakMap(); var _requestQueue = /*#__PURE__*/new WeakMap(); var _webSocket = /*#__PURE__*/new WeakMap(); var _MiddlewareSubscriber_brand = /*#__PURE__*/new WeakSet(); export default class MiddlewareSubscriber { get webSocket() { return _classPrivateFieldGet(_webSocket, this); } constructor(url) { _classPrivateMethodInitSpec(this, _MiddlewareSubscriber_brand); _classPrivateFieldInitSpec(this, _subscriptions, []); _classPrivateFieldInitSpec(this, _requestQueue, []); _classPrivateFieldInitSpec(this, _webSocket, void 0); this.url = url; } async reconnect() { _assertClassBrand(_MiddlewareSubscriber_brand, this, _disconnect).call(this); _classPrivateFieldSet(_webSocket, this, await new Promise(resolve => { const webSocket = new WebSocket(this.url); Object.assign(webSocket, { onopen: () => resolve(webSocket), onerror: errorEvent => { _assertClassBrand(_MiddlewareSubscriber_brand, this, _emit).call(this, () => true, undefined, errorEvent.error); }, onmessage: event => { if (typeof event.data !== 'string') { throw new InternalError(`Unknown incoming message type: ${typeof event.data}`); } _assertClassBrand(_MiddlewareSubscriber_brand, this, _messageHandler).call(this, JSON.parse(event.data)); }, onclose: event => { _assertClassBrand(_MiddlewareSubscriber_brand, this, _emit).call(this, () => true, undefined, new MiddlewareSubscriberDisconnected(event)); _assertClassBrand(_MiddlewareSubscriber_brand, this, _disconnect).call(this, true); } }); })); await Promise.all([..._classPrivateGetter(_MiddlewareSubscriber_brand, this, _get_targets)].map(target => _assertClassBrand(_MiddlewareSubscriber_brand, this, _sendSubscribe).call(this, true, target))); } // TODO: replace p?: any with a proper type definition subscribeKeyBlocks(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'KeyBlocks', Source.Middleware, cb); } subscribeKeyBlocksNode(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'KeyBlocks', Source.Node, cb); } subscribeKeyBlocksAll(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'KeyBlocks', Source.All, cb); } subscribeMicroBlocks(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'MicroBlocks', Source.Middleware, cb); } subscribeMicroBlocksNode(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'MicroBlocks', Source.Node, cb); } subscribeMicroBlocksAll(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'MicroBlocks', Source.All, cb); } subscribeTransactions(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'Transactions', Source.Middleware, cb); } subscribeTransactionsNode(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'Transactions', Source.Node, cb); } subscribeTransactionsAll(cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, 'Transactions', Source.All, cb); } subscribeObject(target, cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, target, Source.Middleware, cb); } subscribeObjectNode(target, cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, target, Source.Node, cb); } subscribeObjectAll(target, cb) { return _assertClassBrand(_MiddlewareSubscriber_brand, this, _subscribe).call(this, target, Source.All, cb); } } function _get_targets(_this) { return new Set(_classPrivateFieldGet(_subscriptions, _this).map(([target]) => target)); } function _sendMessage(message) { if (_classPrivateFieldGet(_webSocket, this) == null) throw new UnexpectedTsError(); _classPrivateFieldGet(_webSocket, this).send(JSON.stringify(message)); } function _sendSubscribe(isSubscribe, target) { if (_classPrivateFieldGet(_webSocket, this) == null) return; const payload = ['KeyBlocks', 'MicroBlocks', 'Transactions'].includes(target) ? target : 'Object'; _assertClassBrand(_MiddlewareSubscriber_brand, this, _sendMessage).call(this, { op: isSubscribe ? 'Subscribe' : 'Unsubscribe', payload, ...(payload === 'Object' && { target }) }); _classPrivateFieldGet(_requestQueue, this).push([isSubscribe, target]); } function _emit(condition, p, e) { _classPrivateFieldGet(_subscriptions, this).filter(([target, source]) => condition(target, source)).forEach(([,, cb]) => cb(p, e)); } function _disconnect(onlyReset = false) { if (_classPrivateFieldGet(_webSocket, this) == null) return; if (!onlyReset) _classPrivateFieldGet(_webSocket, this).close(); Object.assign(_classPrivateFieldGet(_webSocket, this), { onopen: undefined, onerror: undefined, onmessage: undefined }); _classPrivateFieldSet(_webSocket, this, undefined); } function _messageHandler(message) { if (typeof message === 'string' || Array.isArray(message)) { const request = _classPrivateFieldGet(_requestQueue, this).shift(); if (request == null) throw new InternalError('Request queue is empty'); const [isSubscribe, target] = request; let error; if (typeof message === 'string') error = new MiddlewareSubscriberError(message); if (message.includes(target) !== isSubscribe) { error = new InternalError(`Expected ${target} to be${isSubscribe ? '' : ' not'} included into ${message}`); } if (error != null) _assertClassBrand(_MiddlewareSubscriber_brand, this, _emit).call(this, t => target === t, undefined, error); return; } _assertClassBrand(_MiddlewareSubscriber_brand, this, _emit).call(this, (target, source) => (target === message.subscription || target === message.target) && (source === message.source || source === Source.All), message.payload); } function _subscribe(target, source, cb) { const subscription = [target, source, cb]; if (_classPrivateGetter(_MiddlewareSubscriber_brand, this, _get_targets).size === 0) this.reconnect(); if (!_classPrivateGetter(_MiddlewareSubscriber_brand, this, _get_targets).has(target)) _assertClassBrand(_MiddlewareSubscriber_brand, this, _sendSubscribe).call(this, true, target); _classPrivateFieldGet(_subscriptions, this).push(subscription); return () => { _classPrivateFieldSet(_subscriptions, this, _classPrivateFieldGet(_subscriptions, this).filter(item => item !== subscription)); if (!_classPrivateGetter(_MiddlewareSubscriber_brand, this, _get_targets).has(target)) _assertClassBrand(_MiddlewareSubscriber_brand, this, _sendSubscribe).call(this, false, target); if (_classPrivateGetter(_MiddlewareSubscriber_brand, this, _get_targets).size === 0) _assertClassBrand(_MiddlewareSubscriber_brand, this, _disconnect).call(this); }; } //# sourceMappingURL=MiddlewareSubscriber.js.map