Skip to content

Commit d16ea1d

Browse files
committed
feat(ngrx lib): add init function
1 parent df832c6 commit d16ea1d

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

libs/shared/ngrx-callstate-store/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ export type CallState = LoadingState | ErrorState;
7171
7272
## API
7373
74+
### initialization
75+
76+
##### setInitState
77+
78+
The `setInitState` method lets you initialize your custom state if you are not using the constructor.
79+
80+
```typescript
81+
setInitState = (state: T): void
82+
```
83+
7484
### updater
7585
7686
##### startLoading

libs/shared/ngrx-callstate-store/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tomalaforge/ngrx-callstate-store",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Enhance NgRx component-store by providing a loading/error state",
55
"publishConfig": {
66
"access": "public"

libs/shared/ngrx-callstate-store/src/lib/call-state-component-store.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
/* eslint-disable @typescript-eslint/ban-types */
2-
import { inject, Inject, Injectable, InjectionToken } from '@angular/core';
2+
import {
3+
inject,
4+
Inject,
5+
Injectable,
6+
InjectionToken,
7+
Optional,
8+
} from '@angular/core';
39
import { ComponentStore } from '@ngrx/component-store';
410
import { Observable, of, switchMap } from 'rxjs';
511
import {
@@ -28,7 +34,7 @@ export class CallStateComponentStore<
2834
private error = inject(ERROR_TOKEN);
2935
private flickerTime = inject(FLICKER_TIME);
3036

31-
constructor(@Inject(INITIAL_TOKEN) initialState: U) {
37+
constructor(@Inject(INITIAL_TOKEN) @Optional() initialState: U) {
3238
super({ callState: 'INIT', ...initialState } as T);
3339
}
3440

@@ -92,4 +98,8 @@ export class CallStateComponentStore<
9298
} as Partial<T>);
9399
return err;
94100
}
101+
102+
setInitState(initialState: U) {
103+
this.setState({ callState: 'INIT', ...initialState } as T);
104+
}
95105
}

0 commit comments

Comments
 (0)