-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.methods.ts
More file actions
117 lines (116 loc) · 5.49 KB
/
core.methods.ts
File metadata and controls
117 lines (116 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { with_CoreComputed } from "./core.computed";
import { inject } from "@angular/core";
import { SCore } from "../services/score";
import { patchState, signalStoreFeature, withMethods, withProps } from "@ngrx/signals";
import { setLoading, setLoaded, setError } from "@angular-architects/ngrx-toolkit";
import { tapResponse } from "@ngrx/operators";
import { rxMethod } from "@ngrx/signals/rxjs-interop";
import { pipe, tap, switchMap, delay } from "rxjs";
import { ICore } from "../interfaces/icore";
export function with_CoreMethods() {
return signalStoreFeature(
with_CoreComputed(),
withProps(() => ({
s_Core: inject(SCore)
})),
withMethods((store) => ({
getAll: rxMethod<void>(
pipe(
tap(() => patchState(store, setLoading('cpus'))),
switchMap(() => {
return store.s_Core.getAll().pipe(
delay(2000),
tapResponse({
next: (cpus) => patchState(store, { cpus: cpus }),
error: (err) => patchState(store, setError(err, 'cpus')),
complete: () => patchState(store, setLoaded('cpus')),
finalize: () => console.log(`Final Call State after getAll(): ${
store.cpusError()
? store.cpusError()
: store.cpusCallState()
}`)
})
)
})
)
),
get: rxMethod<number>(
pipe(
tap(() => patchState(store, setLoading('cpus'))),
switchMap((cpuId: number) => {
return store.s_Core.get(cpuId).pipe(
delay(500),
tapResponse({
next: (cpu) => patchState(store, { cpu: cpu }),
error: (err) => patchState(store, setError(err, 'cpus')),
complete: () => patchState(store, setLoaded('cpus')),
finalize: () => console.log(`Final Call States after get():
Cpu Error State: ${store.cpusError()}
Cpu Loading State: ${store.cpusLoading()}
Cpu Loaded State: ${store.cpusLoaded()}
`)
})
)
})
)
),
create: rxMethod<ICore>(
pipe(
tap(() => patchState(store, setLoading('cpus'))),
switchMap((cpu: ICore) => {
return store.s_Core.create(cpu).pipe(
tapResponse({
next: () => patchState(store, { cpu }),
error: (err) => patchState(store, setError(err, 'cpus')),
complete: () => patchState(store, setLoaded('cpus')),
finalize: () => console.log(`Final Call States after create(): ${
store.cpusError()
? store.cpusError()
: store.cpusCallState()
}`)
})
)
})
)
),
update: rxMethod<ICore>(
pipe(
tap(() => patchState(store, setLoading('cpus'))),
switchMap((patch: ICore) => {
return store.s_Core.update(patch).pipe(
tapResponse({
next: () => patchState(store, { cpu: patch }),
error: (err) => patchState(store, setError(err, 'cpus')),
complete: () => patchState(store, setLoaded('cpus')),
finalize: () => console.log(`Final Call States after update(): ${
store.cpusError()
? store.cpusError()
: store.cpusCallState()
}`)
})
)
})
)
),
delete: rxMethod<number>(
pipe(
tap(() => patchState(store, setLoading('cpus'))),
switchMap((cpuId: number) => {
return store.s_Core.delete(cpuId).pipe(
tapResponse({
next: () => patchState(store, {}),
error: (err) => patchState(store, setError(err, 'cpus')),
complete: () => patchState(store, setLoaded('cpus')),
finalize: () => console.log(`Final Call States after delete(): ${
store.cpusError()
? store.cpusError()
: store.cpusCallState()
}`)
})
)
})
)
)
}))
)
}