0.6.1 • Published 9 months ago
@jscutlery/rx-computed v0.6.1
@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:
- 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$, []);
- 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?
0.6.1
9 months ago
0.6.0
9 months ago
0.5.12
10 months ago
0.5.13
10 months ago
0.5.10
11 months ago
0.5.11
11 months ago
0.5.8
12 months ago
0.5.7
1 year ago
0.5.9
12 months ago
0.5.4
1 year ago
0.5.3
1 year ago
0.5.6
1 year ago
0.5.5
1 year ago
0.5.2
1 year ago
0.5.1
1 year ago
0.5.0
1 year ago
0.4.1
1 year ago
0.4.0
1 year ago
0.3.0
1 year ago
0.2.6
1 year ago
0.2.5
1 year ago
0.2.4
2 years ago
0.2.2
2 years ago
0.2.1
2 years ago
0.2.0
2 years ago
0.1.0
2 years ago
0.1.1
2 years ago