rx-xtra.defer-abort
v0.0.0
Published
RxJS AbortController-compatible defer operator
Downloads
3
Maintainers
Readme
Rx Xtra: Defer Abort
deferAbort
takes functions that use the AbortController API and coerce them into Observables.
Want to go the other way, making an Observable work with the AbortController? Check out rx-xtra.with-abort
!
rx-xtra.defer-abort
is part of Rx Xtra
, a collection of RxJS utilities.
Created by Joel Shinness LinkTree • Github • Buy me a coffee!
Usage
deferAbort<T>(factory:(signal:AbortSignal) => ObservableInput<T>)
- Parameters
factory
:(signal:AbortSignal) => ObservableInput<T>
Accepts anAbortSignal
and returns anObservableInput<T>
, e.g. anObservable<T>
, or anything that can be coerced into anObservable<T>
, such as an Array, Promise, Iterable, or AsyncIterable.
- Returns
Abortable allows functions and API's that use the AbortController API for cleanup to receive that signal when the subscription ends.
Examples
// This represents some API that uses an AbortSignal to perform cleanup
// In this case, it ends the loop.
async function *countUntilAbort(signal:AbortSignal):AsyncIterable<number>{
for(let i = 1; true; i++){
await new Promise(resolve => setTimeout(resolve, 1000));
if(signal.aborted) {
console.log('Aborted');
break;
}
yield i;
}
}
deferAbort(countUntilAbort)
.pipe(take(3))
.subscribe({
next(val){ console.log('Next', val); },
complete(){ console.log('Complete'); },
error(err){ console.log('Error', err); }
});
// CONSOLE:
// Next 1
// Next 2
// Next 3
// Complete
// Aborted