0.5.1 • Published 2 days ago

@jscutlery/rx-computed v0.5.1

Weekly downloads
-
License
-
Repository
-
Last release
2 days ago

@jscutlery/rx-computed

Installation

yarn add @jscutlery/rx-computed

# or

npm install @jscutlery/rx-computed

rxComputed

Usage

import { rxComputed } from '@jscutlery/rx-computed';

@Component({
  ...
  template: `
    <mc-keywords-input (keywordsChange)="keywords.set($event)"/>
    <mc-sort-input (sortChange)="sort.set($event)"/>
    <mc-recipes-list [recipes]="recipes()"/>
  `
})
class MyCmp {
  keywords = signal<string | undefined>(undefined);
  sort = signal<'asc' | 'desc'>('desc');

  recipes = rxComputed(() => repo.getRecipes(keywords(), sort()), {initialValue: []});
}

Custom Injector

rxComputed utilizes effect() underneath so it is required to be invoked within an Injection Context. However, a custom Injector can be passed in and rxComputed will invoke within that Injector context.

class MyCmp {
  injector = inject(Injector);

  ngOnInit() {
    const value = rxComputed(() => of(16), { injector: this.injector });
  }
}

Motivation

Synchronously computed signals in Angular are relatively straightforward. However, when dealing with an asynchronous source of data like an Observable, there is no primitive to derive a signal from it.

There are two common ways of dealing with this:

  1. Using @angular/core/rxjs-interop which requires us to explicitly define the dependencies of the computed signal:
// Signals
const keywords = signal(...);
const sort = signal(...);

// Signals => RxJS
const recipes$ = combineLatest({
  keywords: toObservable(keywords),
  sort: toObservable(sort)
}).pipe(
  switchMap(({ keywords, sort }) => repo.getRecipes(keywords, sort))
);

// RxJS => Signals
const recipes = toSignal(recipes$, []);
  1. Using effect explicitly (which is not the recommended way of using it, cf. https://angular.io/guide/signals#when-not-to-use-effects):
const keywords = signal(...);
const sort = signal(...);

const recipes = signal(...);

effect((onCleanup) => {
  const sub = repo.getRecipes(keywords(), sort())
    .subscribe(_recipes => recipes.set(_recipes));

  onCleanup(() => sub.unsubscribe());
});

FAQ

What about errors?

Cf. Managing RxJS Traffic with Signals and Suspensify

Cf. @jscutlery/operators#suspensify

0.5.1

2 days ago

0.5.0

7 days ago

0.4.1

8 days ago

0.4.0

19 days ago

0.3.0

1 month ago

0.2.6

1 month ago

0.2.5

2 months ago

0.2.4

7 months ago

0.2.2

12 months ago

0.2.1

12 months ago

0.2.0

12 months ago

0.1.0

12 months ago

0.1.1

12 months ago