116 lines
3.2 KiB
JavaScript
116 lines
3.2 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.flatMapError = undefined;
|
||
|
exports.recoverWith = recoverWith;
|
||
|
exports.throwError = throwError;
|
||
|
|
||
|
var _Stream = require('../Stream');
|
||
|
|
||
|
var _Stream2 = _interopRequireDefault(_Stream);
|
||
|
|
||
|
var _SafeSink = require('../sink/SafeSink');
|
||
|
|
||
|
var _SafeSink2 = _interopRequireDefault(_SafeSink);
|
||
|
|
||
|
var _dispose = require('../disposable/dispose');
|
||
|
|
||
|
var dispose = _interopRequireWildcard(_dispose);
|
||
|
|
||
|
var _tryEvent = require('../source/tryEvent');
|
||
|
|
||
|
var tryEvent = _interopRequireWildcard(_tryEvent);
|
||
|
|
||
|
var _PropagateTask = require('../scheduler/PropagateTask');
|
||
|
|
||
|
var _PropagateTask2 = _interopRequireDefault(_PropagateTask);
|
||
|
|
||
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* If stream encounters an error, recover and continue with items from stream
|
||
|
* returned by f.
|
||
|
* @param {function(error:*):Stream} f function which returns a new stream
|
||
|
* @param {Stream} stream
|
||
|
* @returns {Stream} new stream which will recover from an error by calling f
|
||
|
*/
|
||
|
function recoverWith(f, stream) {
|
||
|
return new _Stream2.default(new RecoverWith(f, stream.source));
|
||
|
} /** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
var flatMapError = exports.flatMapError = recoverWith;
|
||
|
|
||
|
/**
|
||
|
* Create a stream containing only an error
|
||
|
* @param {*} e error value, preferably an Error or Error subtype
|
||
|
* @returns {Stream} new stream containing only an error
|
||
|
*/
|
||
|
function throwError(e) {
|
||
|
return new _Stream2.default(new ErrorSource(e));
|
||
|
}
|
||
|
|
||
|
function ErrorSource(e) {
|
||
|
this.value = e;
|
||
|
}
|
||
|
|
||
|
ErrorSource.prototype.run = function (sink, scheduler) {
|
||
|
return scheduler.asap(new _PropagateTask2.default(runError, this.value, sink));
|
||
|
};
|
||
|
|
||
|
function runError(t, e, sink) {
|
||
|
sink.error(t, e);
|
||
|
}
|
||
|
|
||
|
function RecoverWith(f, source) {
|
||
|
this.f = f;
|
||
|
this.source = source;
|
||
|
}
|
||
|
|
||
|
RecoverWith.prototype.run = function (sink, scheduler) {
|
||
|
return new RecoverWithSink(this.f, this.source, sink, scheduler);
|
||
|
};
|
||
|
|
||
|
function RecoverWithSink(f, source, sink, scheduler) {
|
||
|
this.f = f;
|
||
|
this.sink = new _SafeSink2.default(sink);
|
||
|
this.scheduler = scheduler;
|
||
|
this.disposable = source.run(this, scheduler);
|
||
|
}
|
||
|
|
||
|
RecoverWithSink.prototype.event = function (t, x) {
|
||
|
tryEvent.tryEvent(t, x, this.sink);
|
||
|
};
|
||
|
|
||
|
RecoverWithSink.prototype.end = function (t, x) {
|
||
|
tryEvent.tryEnd(t, x, this.sink);
|
||
|
};
|
||
|
|
||
|
RecoverWithSink.prototype.error = function (t, e) {
|
||
|
var nextSink = this.sink.disable();
|
||
|
|
||
|
dispose.tryDispose(t, this.disposable, this.sink);
|
||
|
this._startNext(t, e, nextSink);
|
||
|
};
|
||
|
|
||
|
RecoverWithSink.prototype._startNext = function (t, x, sink) {
|
||
|
try {
|
||
|
this.disposable = this._continue(this.f, x, sink);
|
||
|
} catch (e) {
|
||
|
sink.error(t, e);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
RecoverWithSink.prototype._continue = function (f, x, sink) {
|
||
|
var stream = f(x);
|
||
|
return stream.source.run(sink, this.scheduler);
|
||
|
};
|
||
|
|
||
|
RecoverWithSink.prototype.dispose = function () {
|
||
|
return this.disposable.dispose();
|
||
|
};
|