UNPKG

mongodb-stitch

Version:

[![Join the chat at https://gitter.im/mongodb/stitch](https://badges.gitter.im/mongodb/stitch.svg)](https://gitter.im/mongodb/stitch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

113 lines 4.08 kB
import Event from "./Event"; import StitchEvent from "./StitchEvent"; import StitchError from "../../StitchError"; import StitchRequestError from "../../StitchRequestError"; var BaseEventStream = (function () { function BaseEventStream(reconnecter) { this.reconnecter = reconnecter; this.closed = false; this.events = []; this.listeners = []; this.lastErr = undefined; } BaseEventStream.prototype.reconnect = function (error) { var _this = this; if (!this.reconnecter) { if (!this.closed) { this.closed = true; this.events.push(new Event(StitchEvent.ERROR_EVENT_NAME, "stream closed: " + error)); this.poll(); } return; } this.reconnecter() .then(function (next) { _this.onReconnect(next); }) .catch(function (e) { if (!(e instanceof StitchError) || !(e instanceof StitchRequestError)) { _this.closed = true; _this.events.push(new Event(StitchEvent.ERROR_EVENT_NAME, "stream closed: " + error)); _this.poll(); return; } setTimeout(function () { return _this.reconnect(e); }, BaseEventStream.RETRY_TIMEOUT_MILLIS); }); }; BaseEventStream.prototype.poll = function () { while (this.events.length !== 0) { var event_1 = this.events.pop(); for (var _i = 0, _a = this.listeners; _i < _a.length; _i++) { var listener = _a[_i]; if (listener.onEvent) { listener.onEvent(event_1); } } } }; BaseEventStream.prototype.addListener = function (listener) { var _this = this; if (this.closed) { setTimeout(function () { return listener.onEvent(new Event(StitchEvent.ERROR_EVENT_NAME, "stream closed")); }, 0); return; } if (this.lastErr !== undefined) { setTimeout(function () { return listener.onEvent(new Event(StitchEvent.ERROR_EVENT_NAME, _this.lastErr)); }, 0); return; } this.listeners.push(listener); this.poll(); }; BaseEventStream.prototype.removeListener = function (listener) { var index = this.listeners.indexOf(listener); if (index === -1) { return; } this.listeners.splice(index, 1); }; BaseEventStream.prototype.listenOnce = function (listener) { var _this = this; if (this.closed) { setTimeout(function () { return listener.onEvent(new Event(StitchEvent.ERROR_EVENT_NAME, "stream closed")); }, 0); return; } if (this.lastErr !== undefined) { setTimeout(function () { return listener.onEvent(new Event(StitchEvent.ERROR_EVENT_NAME, _this.lastErr)); }, 0); return; } var wrapper = { onEvent: function (e) { _this.removeListener(wrapper); listener.onEvent(e); } }; this.addListener(wrapper); }; BaseEventStream.prototype.nextEvent = function () { var _this = this; if (this.closed) { return Promise.reject(new Event(StitchEvent.ERROR_EVENT_NAME, "stream closed")); } if (this.lastErr !== undefined) { return Promise.reject(new Event(StitchEvent.ERROR_EVENT_NAME, this.lastErr)); } return new Promise(function (resolve, reject) { _this.listenOnce({ onEvent: function (e) { resolve(e); } }); }); }; BaseEventStream.prototype.close = function () { if (this.closed) { return; } this.closed = true; this.afterClose(); }; BaseEventStream.RETRY_TIMEOUT_MILLIS = 5000; return BaseEventStream; }()); export default BaseEventStream; //# sourceMappingURL=BaseEventStream.js.map