ix
Version:
The Interactive Extensions for JavaScript
54 lines (52 loc) • 2.11 kB
JavaScript
import { __asyncGenerator, __asyncValues, __await } from "tslib";
import { AsyncIterableX } from '../asynciterablex.mjs';
import { wrapWithAbort } from './withabort.mjs';
import { throwIfAborted } from '../../aborterror.mjs';
/** @ignore */
export class FinallyAsyncIterable extends AsyncIterableX {
constructor(source, action) {
super();
this._source = source;
this._action = action;
}
[Symbol.asyncIterator](signal) {
return __asyncGenerator(this, arguments, function* _a() {
var _b, e_1, _c, _d;
throwIfAborted(signal);
try {
try {
for (var _e = true, _f = __asyncValues(wrapWithAbort(this._source, signal)), _g; _g = yield __await(_f.next()), _b = _g.done, !_b; _e = true) {
_d = _g.value;
_e = false;
const item = _d;
yield yield __await(item);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_e && !_b && (_c = _f.return)) yield __await(_c.call(_f));
}
finally { if (e_1) throw e_1.error; }
}
}
finally {
yield __await(this._action());
}
});
}
}
/**
* Invokes a specified asynchronous action after the source async-iterable sequence terminates gracefully or exceptionally.
*
* @template TSource The type of the elements in the source sequence.
* @param {(() => void | Promise<void>)} action Action to invoke and await asynchronously after the source async-iterable sequence terminates
* @returns {MonoTypeOperatorAsyncFunction<TSource>} An operator that returns the source sequence with the
* action-invoking termination behavior applied.
*/
export function finalize(action) {
return function finalizeOperatorFunction(source) {
return new FinallyAsyncIterable(source, action);
};
}
//# sourceMappingURL=finalize.mjs.map