feat(ngrx lib): add init function

This commit is contained in:
thomas laforge
2022-12-17 20:10:34 +01:00
parent df832c6850
commit d16ea1dcd0
3 changed files with 23 additions and 3 deletions

View File

@@ -71,6 +71,16 @@ export type CallState = LoadingState | ErrorState;
## API
### initialization
##### setInitState
The `setInitState` method lets you initialize your custom state if you are not using the constructor.
```typescript
setInitState = (state: T): void
```
### updater
##### startLoading

View File

@@ -1,6 +1,6 @@
{
"name": "@tomalaforge/ngrx-callstate-store",
"version": "0.0.3",
"version": "0.0.4",
"description": "Enhance NgRx component-store by providing a loading/error state",
"publishConfig": {
"access": "public"

View File

@@ -1,5 +1,11 @@
/* eslint-disable @typescript-eslint/ban-types */
import { inject, Inject, Injectable, InjectionToken } from '@angular/core';
import {
inject,
Inject,
Injectable,
InjectionToken,
Optional,
} from '@angular/core';
import { ComponentStore } from '@ngrx/component-store';
import { Observable, of, switchMap } from 'rxjs';
import {
@@ -28,7 +34,7 @@ export class CallStateComponentStore<
private error = inject(ERROR_TOKEN);
private flickerTime = inject(FLICKER_TIME);
constructor(@Inject(INITIAL_TOKEN) initialState: U) {
constructor(@Inject(INITIAL_TOKEN) @Optional() initialState: U) {
super({ callState: 'INIT', ...initialState } as T);
}
@@ -92,4 +98,8 @@ export class CallStateComponentStore<
} as Partial<T>);
return err;
}
setInitState(initialState: U) {
this.setState({ callState: 'INIT', ...initialState } as T);
}
}