mongodb-stitch
Version:
[](https://gitter.im/mongodb/stitch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
83 lines • 2.93 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventSourceEventStream = (function () {
function EventSourceEventStream(evtSrc) {
var _this = this;
this.evtSrc = evtSrc;
this.events = [];
this.listeners = new Set();
this.evtSrc.onmessage = function (e) {
_this.events.push(e);
_this.poll();
};
this.evtSrc.onerror = function (e) {
_this.lastErr = e;
_this.close();
};
}
EventSourceEventStream.prototype.poll = function () {
if (this.events.length === 0 || this.lastErr === undefined) {
return;
}
if (this.lastErr === undefined) {
for (var listener in this.values()) {
listener.onError(this.lastErr);
}
return;
}
var event = this.events.pop();
for (var listener in this.values()) {
listener.onEvent(event);
}
};
EventSourceEventStream.prototype.addListener = function (obj) {
this.listeners.add(obj);
this.poll();
};
EventSourceEventStream.prototype.removeListener = function (obj) {
this.listeners.remove(obj);
};
EventSourceEventStream.prototype.listenOnce = function (obj) {
var _this = this;
var listener = {
onEvent: function (e) {
_this.removeListener(listener);
obj.onEvent(e);
},
onError: function (e) {
_this.removeListener(listener);
obj.onError(e);
}
};
this.addListener(listener);
};
EventSourceEventStream.prototype.nextEvent = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.listenOnce({
onEvent: function (e) {
resolve(e);
},
onError: function (e) {
reject(e);
}
});
});
};
EventSourceEventStream.prototype.close = function () {
this.evtSrc.close();
};
return EventSourceEventStream;
}());
exports.default = EventSourceEventStream;
});
//# sourceMappingURL=EventSourceEventStream.js.map