Skip to content

Commit 245e871

Browse files
committed
feat: use resource snapshots
1 parent 5b5dc82 commit 245e871

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
linkedSignal,
3+
Resource,
4+
resourceFromSnapshots,
5+
ResourceSnapshot,
6+
Signal,
7+
} from '@angular/core';
8+
9+
import { Luggage } from './luggage';
10+
11+
export function withMinWeight(
12+
input: Resource<Luggage[]>,
13+
minWeight: Signal<number>,
14+
): Resource<Luggage[]> {
15+
const derived = linkedSignal<
16+
{ snap: ResourceSnapshot<Luggage[]>; min: number },
17+
ResourceSnapshot<Luggage[]>
18+
>({
19+
source: () => ({
20+
snap: input.snapshot(),
21+
min: minWeight(),
22+
}),
23+
computation: ({ snap, min }) => {
24+
if (snap.status === 'resolved') {
25+
return {
26+
...snap,
27+
value: snap.value.filter((item) => item.weight >= min),
28+
};
29+
}
30+
return snap;
31+
},
32+
});
33+
34+
return resourceFromSnapshots(derived);
35+
}

src/app/domains/luggage/feature-luggage/luggage-overview/simple-luggage-store.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { inject, Service, signal } from '@angular/core';
22

3+
import { withPreviousValue } from '../../../shared/util-common/with-previous-value';
34
import { LuggageClient } from '../../data/luggage-client';
45

56
@Service({ autoProvided: false })
67
export class SimpleLuggageStore {
78
private luggageClient = inject(LuggageClient);
89

9-
private readonly luggageResource = this.luggageClient.findLuggage();
10+
private readonly luggageResource = withPreviousValue(
11+
this.luggageClient.findLuggage(),
12+
);
1013
readonly luggage = this.luggageResource.value;
1114
readonly isLoading = this.luggageResource.isLoading;
1215
readonly error = this.luggageResource.error;

0 commit comments

Comments
 (0)