112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.fromPromise = fromPromise;
|
||
|
exports.awaitPromises = awaitPromises;
|
||
|
|
||
|
var _Stream = require('../Stream');
|
||
|
|
||
|
var _Stream2 = _interopRequireDefault(_Stream);
|
||
|
|
||
|
var _fatalError = require('../fatalError');
|
||
|
|
||
|
var _fatalError2 = _interopRequireDefault(_fatalError);
|
||
|
|
||
|
var _core = require('../source/core');
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* Create a stream containing only the promise's fulfillment
|
||
|
* value at the time it fulfills.
|
||
|
* @param {Promise<T>} p promise
|
||
|
* @return {Stream<T>} stream containing promise's fulfillment value.
|
||
|
* If the promise rejects, the stream will error
|
||
|
*/
|
||
|
function fromPromise(p) {
|
||
|
return awaitPromises((0, _core.of)(p));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Turn a Stream<Promise<T>> into Stream<T> by awaiting each promise.
|
||
|
* Event order is preserved.
|
||
|
* @param {Stream<Promise<T>>} stream
|
||
|
* @return {Stream<T>} stream of fulfillment values. The stream will
|
||
|
* error if any promise rejects.
|
||
|
*/
|
||
|
/** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
function awaitPromises(stream) {
|
||
|
return new _Stream2.default(new Await(stream.source));
|
||
|
}
|
||
|
|
||
|
function Await(source) {
|
||
|
this.source = source;
|
||
|
}
|
||
|
|
||
|
Await.prototype.run = function (sink, scheduler) {
|
||
|
return new AwaitSink(this.source, sink, scheduler);
|
||
|
};
|
||
|
|
||
|
function AwaitSink(source, sink, scheduler) {
|
||
|
this.sink = sink;
|
||
|
this.scheduler = scheduler;
|
||
|
this.queue = Promise.resolve();
|
||
|
this.disposable = source.run(this, scheduler);
|
||
|
this.active = true;
|
||
|
var self = this;
|
||
|
|
||
|
// Pre-create closures, to avoid creating them per event
|
||
|
this._eventBound = function (x) {
|
||
|
if (!self.active) { return; }
|
||
|
self.sink.event(self.scheduler.now(), x);
|
||
|
};
|
||
|
|
||
|
this._endBound = function (x) {
|
||
|
if (!self.active) { return; }
|
||
|
self.sink.end(self.scheduler.now(), x);
|
||
|
};
|
||
|
|
||
|
this._errorBound = function (e) {
|
||
|
self.sink.error(self.scheduler.now(), e);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
AwaitSink.prototype.dispose = function () {
|
||
|
this.active = false;
|
||
|
return this.disposable.dispose();
|
||
|
};
|
||
|
|
||
|
AwaitSink.prototype.event = function (t, promise) {
|
||
|
var self = this;
|
||
|
this.queue = this.queue.then(function () {
|
||
|
return self._event(promise);
|
||
|
}).catch(this._errorBound);
|
||
|
};
|
||
|
|
||
|
AwaitSink.prototype.end = function (t, x) {
|
||
|
var self = this;
|
||
|
this.queue = this.queue.then(function () {
|
||
|
return self._end(x);
|
||
|
}).catch(this._errorBound);
|
||
|
};
|
||
|
|
||
|
AwaitSink.prototype.error = function (t, e) {
|
||
|
var self = this;
|
||
|
// Don't resolve error values, propagate directly
|
||
|
this.queue = this.queue.then(function () {
|
||
|
return self._errorBound(e);
|
||
|
}).catch(_fatalError2.default);
|
||
|
};
|
||
|
|
||
|
AwaitSink.prototype._event = function (promise) {
|
||
|
return promise.then(this._eventBound);
|
||
|
};
|
||
|
|
||
|
AwaitSink.prototype._end = function (x) {
|
||
|
return Promise.resolve(x).then(this._endBound);
|
||
|
};
|