rx-xtra.defer-abort
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

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 LinkTreeGithubBuy me a coffee!

Usage

deferAbort<T>(factory:(signal:AbortSignal) => ObservableInput<T>)

  • Parameters
    • factory: (signal:AbortSignal) => ObservableInput<T> Accepts an AbortSignal and returns an ObservableInput<T>, e.g. an Observable<T>, or anything that can be coerced into an Observable<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

Package Sidebar

Install

npm i rx-xtra.defer-abort

Weekly Downloads

0

Version

0.0.0

License

ISC

Unpacked Size

7.2 kB

Total Files

7

Last publish

Collaborators

  • joelcodes