-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
component-store.ts
524 lines (483 loc) · 16.4 KB
/
component-store.ts
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import {
isObservable,
Observable,
of,
ReplaySubject,
Subscription,
throwError,
combineLatest,
Subject,
queueScheduler,
scheduled,
asapScheduler,
EMPTY,
ObservedValueOf,
} from 'rxjs';
import {
takeUntil,
withLatestFrom,
map,
distinctUntilChanged,
shareReplay,
take,
tap,
catchError,
observeOn,
} from 'rxjs/operators';
import { debounceSync } from './debounce-sync';
import {
Injectable,
OnDestroy,
Optional,
InjectionToken,
Inject,
isDevMode,
Signal,
computed,
type ValueEqualityFn,
type CreateComputedOptions,
} from '@angular/core';
import { isOnStateInitDefined, isOnStoreInitDefined } from './lifecycle_hooks';
import { toSignal } from '@angular/core/rxjs-interop';
export interface SelectConfig {
debounce?: boolean;
}
export const INITIAL_STATE_TOKEN = new InjectionToken(
'@ngrx/component-store Initial State'
);
export type SelectorResults<Selectors extends Observable<unknown>[]> = {
[Key in keyof Selectors]: Selectors[Key] extends Observable<infer U>
? U
: never;
};
export type Projector<Selectors extends Observable<unknown>[], Result> = (
...args: SelectorResults<Selectors>
) => Result;
type SignalsProjector<Signals extends Signal<unknown>[], Result> = (
...values: {
[Key in keyof Signals]: Signals[Key] extends Signal<infer Value>
? Value
: never;
}
) => Result;
interface SelectSignalOptions<T> {
/**
* A comparison function which defines equality for select results.
*/
equal?: ValueEqualityFn<T>;
}
@Injectable()
export class ComponentStore<T extends object> implements OnDestroy {
// Should be used only in ngOnDestroy.
private readonly destroySubject$ = new ReplaySubject<void>(1);
// Exposed to any extending Store to be used for the teardown.
readonly destroy$ = this.destroySubject$.asObservable();
private readonly stateSubject$ = new ReplaySubject<T>(1);
private isInitialized = false;
// Needs to be after destroy$ is declared because it's used in select.
readonly state$: Observable<T> = this.select((s) => s);
readonly state: Signal<T> = toSignal(
this.stateSubject$.pipe(takeUntil(this.destroy$)),
{ requireSync: false, manualCleanup: true }
) as Signal<T>;
private ɵhasProvider = false;
constructor(@Optional() @Inject(INITIAL_STATE_TOKEN) defaultState?: T) {
// State can be initialized either through constructor or setState.
if (defaultState) {
this.initState(defaultState);
}
this.checkProviderForHooks();
}
/** Completes all relevant Observable streams. */
ngOnDestroy() {
this.stateSubject$.complete();
this.destroySubject$.next();
}
/**
* Creates an updater.
*
* Throws an error if updater is called with synchronous values (either
* imperative value or Observable that is synchronous) before ComponentStore
* is initialized. If called with async Observable before initialization then
* state will not be updated and subscription would be closed.
*
* @param updaterFn A static updater function that takes 2 parameters (the
* current state and an argument object) and returns a new instance of the
* state.
* @return A function that accepts one argument which is forwarded as the
* second argument to `updaterFn`. Every time this function is called
* subscribers will be notified of the state change.
*/
updater<
// Allow to force-provide the type
ProvidedType = void,
// This type is derived from the `value` property, defaulting to void if it's missing
OriginType = ProvidedType,
// The Value type is assigned from the Origin
ValueType = OriginType,
// Return either an empty callback or a function requiring specific types as inputs
ReturnType = OriginType extends void
? () => void
: (observableOrValue: ValueType | Observable<ValueType>) => Subscription
>(updaterFn: (state: T, value: OriginType) => T): ReturnType {
return ((
observableOrValue?: OriginType | Observable<OriginType>
): Subscription => {
// We need to explicitly throw an error if a synchronous error occurs.
// This is necessary to make synchronous errors catchable.
let isSyncUpdate = true;
let syncError: unknown;
// We can receive either the value or an observable. In case it's a
// simple value, we'll wrap it with `of` operator to turn it into
// Observable.
const observable$ = isObservable(observableOrValue)
? observableOrValue
: of(observableOrValue);
const subscription = observable$
.pipe(
// Push the value into queueScheduler
observeOn(queueScheduler),
// If the state is not initialized yet, we'll throw an error.
tap(() => this.assertStateIsInitialized()),
withLatestFrom(this.stateSubject$),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
map(([value, currentState]) => updaterFn(currentState, value!)),
tap((newState) => this.stateSubject$.next(newState)),
catchError((error: unknown) => {
if (isSyncUpdate) {
syncError = error;
return EMPTY;
}
return throwError(error);
}),
takeUntil(this.destroy$)
)
.subscribe();
if (syncError) {
throw syncError;
}
isSyncUpdate = false;
return subscription;
}) as unknown as ReturnType;
}
/**
* Initializes state. If it was already initialized then it resets the
* state.
*/
private initState(state: T): void {
scheduled([state], queueScheduler).subscribe((s) => {
this.isInitialized = true;
this.stateSubject$.next(s);
});
}
/**
* Sets the state specific value.
* @param stateOrUpdaterFn object of the same type as the state or an
* updaterFn, returning such object.
*/
setState(stateOrUpdaterFn: T | ((state: T) => T)): void {
if (typeof stateOrUpdaterFn !== 'function') {
this.initState(stateOrUpdaterFn);
} else {
this.updater(stateOrUpdaterFn as (state: T) => T)();
}
}
/**
* Patches the state with provided partial state.
*
* @param partialStateOrUpdaterFn a partial state or a partial updater
* function that accepts the state and returns the partial state.
* @throws Error if the state is not initialized.
*/
patchState(
partialStateOrUpdaterFn:
| Partial<T>
| Observable<Partial<T>>
| ((state: T) => Partial<T>)
): void {
const patchedState =
typeof partialStateOrUpdaterFn === 'function'
? partialStateOrUpdaterFn(this.get())
: partialStateOrUpdaterFn;
this.updater((state, partialState: Partial<T>) => ({
...state,
...partialState,
}))(patchedState);
}
protected get(): T;
protected get<R>(projector: (s: T) => R): R;
protected get<R>(projector?: (s: T) => R): R | T {
this.assertStateIsInitialized();
let value: R | T;
this.stateSubject$.pipe(take(1)).subscribe((state) => {
value = projector ? projector(state) : state;
});
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return value!;
}
/**
* Creates a selector.
*
* @param projector A pure projection function that takes the current state and
* returns some new slice/projection of that state.
* @param config SelectConfig that changes the behavior of selector, including
* the debouncing of the values until the state is settled.
* @return An observable of the projector results.
*/
select<Result>(
projector: (s: T) => Result,
config?: SelectConfig
): Observable<Result>;
select<SelectorsObject extends Record<string, Observable<unknown>>>(
selectorsObject: SelectorsObject,
config?: SelectConfig
): Observable<{
[K in keyof SelectorsObject]: ObservedValueOf<SelectorsObject[K]>;
}>;
select<Selectors extends Observable<unknown>[], Result>(
...selectorsWithProjector: [
...selectors: Selectors,
projector: Projector<Selectors, Result>
]
): Observable<Result>;
select<Selectors extends Observable<unknown>[], Result>(
...selectorsWithProjectorAndConfig: [
...selectors: Selectors,
projector: Projector<Selectors, Result>,
config: SelectConfig
]
): Observable<Result>;
select<
Selectors extends Array<
Observable<unknown> | SelectConfig | ProjectorFn | SelectorsObject
>,
Result,
ProjectorFn extends (...a: unknown[]) => Result,
SelectorsObject extends Record<string, Observable<unknown>>
>(...args: Selectors): Observable<Result> {
const { observablesOrSelectorsObject, projector, config } =
processSelectorArgs<Selectors, Result, ProjectorFn, SelectorsObject>(
args
);
const source$ = hasProjectFnOnly(observablesOrSelectorsObject, projector)
? this.stateSubject$
: combineLatest(observablesOrSelectorsObject as any);
return source$.pipe(
config.debounce ? debounceSync() : noopOperator(),
(projector
? map((projectorArgs) =>
// projectorArgs could be an Array in case where the entire state is an Array, so adding this check
(observablesOrSelectorsObject as Observable<unknown>[]).length >
0 && Array.isArray(projectorArgs)
? projector(...projectorArgs)
: projector(projectorArgs)
)
: noopOperator()) as () => Observable<Result>,
distinctUntilChanged(),
shareReplay({
refCount: true,
bufferSize: 1,
}),
takeUntil(this.destroy$)
);
}
/**
* Creates a signal from the provided state projector function.
*/
selectSignal<Result>(
projector: (state: T) => Result,
options?: SelectSignalOptions<Result>
): Signal<Result>;
/**
* Creates a signal by combining provided signals.
*/
selectSignal<Signals extends Signal<unknown>[], Result>(
...args: [...signals: Signals, projector: SignalsProjector<Signals, Result>]
): Signal<Result>;
/**
* Creates a signal by combining provided signals.
*/
selectSignal<Signals extends Signal<unknown>[], Result>(
...args: [
...signals: Signals,
projector: SignalsProjector<Signals, Result>,
options: SelectSignalOptions<Result>
]
): Signal<Result>;
selectSignal(
...args:
| [(state: T) => unknown, SelectSignalOptions<unknown>?]
| [
...signals: Signal<unknown>[],
projector: (...values: unknown[]) => unknown
]
| [
...signals: Signal<unknown>[],
projector: (...values: unknown[]) => unknown,
options: SelectSignalOptions<unknown>
]
): Signal<unknown> {
const selectSignalArgs = [...args];
const defaultEqualityFn: ValueEqualityFn<unknown> = (previous, current) =>
previous === current;
const options: CreateComputedOptions<unknown> =
typeof selectSignalArgs[args.length - 1] === 'object'
? {
equal:
(selectSignalArgs.pop() as SelectSignalOptions<unknown>).equal ||
defaultEqualityFn,
}
: { equal: defaultEqualityFn };
const projector = selectSignalArgs.pop() as (
...values: unknown[]
) => unknown;
const signals = selectSignalArgs as Signal<unknown>[];
const computation =
signals.length === 0
? () => projector(this.state())
: () => {
const values = signals.map((signal) => signal());
return projector(...values);
};
return computed(computation, options);
}
/**
* Creates an effect.
*
* This effect is subscribed to throughout the lifecycle of the ComponentStore.
* @param generator A function that takes an origin Observable input and
* returns an Observable. The Observable that is returned will be
* subscribed to for the life of the component.
* @return A function that, when called, will trigger the origin Observable.
*/
effect<
// This type quickly became part of effect 'API'
ProvidedType = void,
// The actual origin$ type, which could be unknown, when not specified
OriginType extends
| Observable<ProvidedType>
| unknown = Observable<ProvidedType>,
// Unwrapped actual type of the origin$ Observable, after default was applied
ObservableType = OriginType extends Observable<infer A> ? A : never,
// Return either an optional callback or a function requiring specific types as inputs
ReturnType = ProvidedType | ObservableType extends void
? (
observableOrValue?: ObservableType | Observable<ObservableType>
) => Subscription
: (
observableOrValue: ObservableType | Observable<ObservableType>
) => Subscription
>(generator: (origin$: OriginType) => Observable<unknown>): ReturnType {
const origin$ = new Subject<ObservableType>();
generator(origin$ as OriginType)
// tied to the lifecycle 👇 of ComponentStore
.pipe(takeUntil(this.destroy$))
.subscribe();
return ((
observableOrValue?: ObservableType | Observable<ObservableType>
): Subscription => {
const observable$ = isObservable(observableOrValue)
? observableOrValue
: of(observableOrValue);
return observable$.pipe(takeUntil(this.destroy$)).subscribe((value) => {
// any new 👇 value is pushed into a stream
origin$.next(value as ObservableType);
});
}) as unknown as ReturnType;
}
/**
* Used to check if lifecycle hooks are defined
* but not used with provideComponentStore()
*/
private checkProviderForHooks() {
asapScheduler.schedule(() => {
if (
isDevMode() &&
(isOnStoreInitDefined(this) || isOnStateInitDefined(this)) &&
!this.ɵhasProvider
) {
const warnings = [
isOnStoreInitDefined(this) ? 'OnStoreInit' : '',
isOnStateInitDefined(this) ? 'OnStateInit' : '',
].filter((defined) => defined);
console.warn(
`@ngrx/component-store: ${
this.constructor.name
} has the ${warnings.join(' and ')} ` +
'lifecycle hook(s) implemented without being provided using the ' +
`provideComponentStore(${this.constructor.name}) function. ` +
`To resolve this, provide the component store via provideComponentStore(${this.constructor.name})`
);
}
});
}
private assertStateIsInitialized(): void {
if (!this.isInitialized) {
throw new Error(
`${this.constructor.name} has not been initialized yet. ` +
`Please make sure it is initialized before updating/getting.`
);
}
}
}
function processSelectorArgs<
Selectors extends Array<
Observable<unknown> | SelectConfig | ProjectorFn | SelectorsObject
>,
Result,
ProjectorFn extends (...a: unknown[]) => Result,
SelectorsObject extends Record<string, Observable<unknown>>
>(
args: Selectors
):
| {
observablesOrSelectorsObject: Observable<unknown>[];
projector: ProjectorFn;
config: Required<SelectConfig>;
}
| {
observablesOrSelectorsObject: SelectorsObject;
projector: undefined;
config: Required<SelectConfig>;
} {
const selectorArgs = Array.from(args);
// Assign default values.
let config: Required<SelectConfig> = { debounce: false };
// Last argument is either config or projector or selectorsObject
if (isSelectConfig(selectorArgs[selectorArgs.length - 1])) {
config = { ...config, ...selectorArgs.pop() };
}
// At this point selectorArgs is either projector, selectors with projector or selectorsObject
if (selectorArgs.length === 1 && typeof selectorArgs[0] !== 'function') {
// this is a selectorsObject
return {
observablesOrSelectorsObject: selectorArgs[0] as SelectorsObject,
projector: undefined,
config,
};
}
const projector = selectorArgs.pop() as ProjectorFn;
// The Observables to combine, if there are any left.
const observables = selectorArgs as Observable<unknown>[];
return {
observablesOrSelectorsObject: observables,
projector,
config,
};
}
function isSelectConfig(arg: SelectConfig | unknown): arg is SelectConfig {
return typeof (arg as SelectConfig).debounce !== 'undefined';
}
function hasProjectFnOnly(
observablesOrSelectorsObject: unknown[] | Record<string, unknown>,
projector: unknown
) {
return (
Array.isArray(observablesOrSelectorsObject) &&
observablesOrSelectorsObject.length === 0 &&
projector
);
}
function noopOperator(): <T>(source$: Observable<T>) => typeof source$ {
return (source$) => source$;
}