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)

89 lines 2.9 kB
import Event from "./Event"; import StitchEvent from "./StitchEvent"; var Stream = (function () { function Stream(eventStream, decoder) { this.eventStream = eventStream; this.decoder = decoder; this.listeners = []; } Stream.prototype.next = function () { var _this = this; return this.eventStream.nextEvent() .then(function (event) { var se = StitchEvent.fromEvent(event, _this.decoder); if (se.eventName === StitchEvent.ERROR_EVENT_NAME) { throw se.error; } if (se.eventName === Event.MESSAGE_EVENT) { return se.data; } return _this.next(); }); }; Stream.prototype.onNext = function (callback) { var _this = this; var wrapper = { onEvent: function (e) { var se = StitchEvent.fromEvent(e, _this.decoder); if (se.eventName !== Event.MESSAGE_EVENT) { return; } callback(se.data); } }; this.eventStream.addListener(wrapper); }; Stream.prototype.onError = function (callback) { var _this = this; var wrapper = { onEvent: function (e) { var se = StitchEvent.fromEvent(e, _this.decoder); if (se.eventName === StitchEvent.ERROR_EVENT_NAME) { callback(se.error); } } }; this.eventStream.addListener(wrapper); }; Stream.prototype.addListener = function (listener) { var _this = this; var wrapper = { onEvent: function (e) { var se = StitchEvent.fromEvent(e, _this.decoder); if (se.eventName === StitchEvent.ERROR_EVENT_NAME) { if (listener.onError) { listener.onError(se.error); } } else { if (listener.onNext) { listener.onNext(se.data); } } } }; this.listeners.push([listener, wrapper]); this.eventStream.addListener(wrapper); }; Stream.prototype.removeListener = function (listener) { var index = -1; for (var i = 0; i < this.listeners.length; i++) { if (this.listeners[i][0] === listener) { index = i; break; } } if (index === -1) { return; } var wrapper = this.listeners[index][1]; this.listeners.splice(index, 1); this.eventStream.removeListener(wrapper); }; Stream.prototype.close = function () { this.eventStream.close(); }; return Stream; }()); export default Stream; //# sourceMappingURL=Stream.js.map