@reactivex/ix-es2015-cjs
Version:
The Interactive Extensions for JavaScript
59 lines (57 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultIfEmpty = exports.DefaultIfEmptyAsyncIterable = void 0;
const tslib_1 = require("tslib");
const asynciterablex_js_1 = require("../asynciterablex.js");
const withabort_js_1 = require("./withabort.js");
const aborterror_js_1 = require("../../aborterror.js");
/** @ignore */
class DefaultIfEmptyAsyncIterable extends asynciterablex_js_1.AsyncIterableX {
constructor(source, defaultValue) {
super();
this._source = source;
this._defaultValue = defaultValue;
}
[Symbol.asyncIterator](signal) {
return tslib_1.__asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
(0, aborterror_js_1.throwIfAborted)(signal);
let state = 1;
try {
for (var _e = true, _f = tslib_1.__asyncValues((0, withabort_js_1.wrapWithAbort)(this._source, signal)), _g; _g = yield tslib_1.__await(_f.next()), _b = _g.done, !_b; _e = true) {
_d = _g.value;
_e = false;
const item = _d;
state = 2;
yield yield tslib_1.__await(item);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_e && !_b && (_c = _f.return)) yield tslib_1.__await(_c.call(_f));
}
finally { if (e_1) throw e_1.error; }
}
if (state === 1) {
yield yield tslib_1.__await(this._defaultValue);
}
});
}
}
exports.DefaultIfEmptyAsyncIterable = DefaultIfEmptyAsyncIterable;
/**
* Returns the elements of the specified sequence or the default value in a singleton sequence
* if the sequence is empty.
*
* @template T The type of elements in the source sequence.
* @param {T} defaultValue The value to return if the sequence is empty.
* @returns {MonoTypeOperatorAsyncFunction<T>} An operator which returns the elements of the source sequence or the default value as a singleton.
*/
function defaultIfEmpty(defaultValue) {
return function defaultIfEmptyOperatorFunction(source) {
return new DefaultIfEmptyAsyncIterable(source, defaultValue);
};
}
exports.defaultIfEmpty = defaultIfEmpty;
//# sourceMappingURL=defaultifempty.js.map