abort-controller-x-rxjs
Version:
Abortable helpers for RxJS
39 lines • 1.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.lastValueFrom = void 0;
const abort_controller_x_1 = require("abort-controller-x");
const rxjs_1 = require("rxjs");
/**
* Like original `lastValueFrom` from RxJS, but accepts `AbortSignal`. When that
* signal is aborted, unsubscribes from the observable and throws `AbortError`.
*/
function lastValueFrom(signal, source, config) {
const hasConfig = typeof config === 'object';
return abort_controller_x_1.execute(signal, (resolve, reject) => {
let _hasValue = false;
let _value;
const subscription = source.subscribe({
next: value => {
_value = value;
_hasValue = true;
},
error: reject,
complete: () => {
if (_hasValue) {
resolve(_value);
}
else if (hasConfig) {
resolve(config.defaultValue);
}
else {
reject(new rxjs_1.EmptyError());
}
},
});
return () => {
subscription.unsubscribe();
};
});
}
exports.lastValueFrom = lastValueFrom;
//# sourceMappingURL=lastValueFrom.js.map
;