abort-controller-x-rxjs
Version:
Abortable helpers for RxJS
35 lines • 1.1 kB
JavaScript
import { execute } from 'abort-controller-x';
import { EmptyError } from 'rxjs';
/**
* Like original `lastValueFrom` from RxJS, but accepts `AbortSignal`. When that
* signal is aborted, unsubscribes from the observable and throws `AbortError`.
*/
export function lastValueFrom(signal, source, config) {
const hasConfig = typeof config === 'object';
return 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 EmptyError());
}
},
});
return () => {
subscription.unsubscribe();
};
});
}
//# sourceMappingURL=lastValueFrom.js.map