ng-simple-state 18.0.2 → 19.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,9 +19,9 @@ See the demos:
19
19
  npm i ng-simple-state
20
20
  ```
21
21
 
22
- ### Step 2: Import `NgSimpleStateModule` into your `AppModule`
22
+ ### Step 2: Import `provideNgSimpleState` into your providers
23
23
 
24
- `NgSimpleStateModule` has some global optional config defined by `NgSimpleStateConfig` interface:
24
+ `provideNgSimpleState` has some global optional config defined by `NgSimpleStateConfig` interface:
25
25
 
26
26
  | Option | Description | Default |
27
27
  | -------------------- | ----------------------------------------------------------------------------------------------- | ---------- |
@@ -33,26 +33,21 @@ npm i ng-simple-state
33
33
  _Side note: each store can be override the global configuration implementing `storeConfig()` method (see "Override global config")._
34
34
 
35
35
  ```ts
36
- import { BrowserModule } from '@angular/platform-browser';
37
- import { NgModule } from '@angular/core';
36
+ import { isDevMode } from '@angular/core';
37
+ import { bootstrapApplication } from '@angular/platform-browser';
38
+
38
39
  import { AppComponent } from './app.component';
39
- import { CommonModule } from '@angular/common';
40
- import { NgSimpleStateModule } from 'ng-simple-state';
41
- import { environment } from '../environments/environment';
42
-
43
- @NgModule({
44
- declarations: [AppComponent],
45
- imports: [
46
- BrowserModule,
47
- CommonModule,
48
- NgSimpleStateModule.forRoot({
49
- enableDevTool: !environment.production, // Enable Redux DevTools only in development mode
50
- enableLocalStorage: false // Local storage state persistence is globally disabled
51
- })
52
- ],
53
- bootstrap: [AppComponent],
54
- })
55
- export class AppModule {}
40
+ import { provideNgSimpleState } from 'ng-simple-state';
41
+
42
+ bootstrapApplication(AppComponent, {
43
+ providers: [
44
+ provideNgSimpleState({
45
+ enableDevTool: isDevMode(),
46
+ enableLocalStorage: true,
47
+ persistentStorage: 'local'
48
+ })
49
+ ]
50
+ });
56
51
  ```
57
52
 
58
53
  ### Step 3: Chose your store
@@ -191,31 +186,19 @@ export class CounterStore extends NgSimpleStateBaseRxjsStore<CounterState> {
191
186
  }
192
187
  ```
193
188
 
194
- #### Step 3: Inject your store into the providers of the module you want (or the providers of component), eg.:
189
+ #### Step 3: Inject your store into the providers, eg.:
195
190
 
196
191
  ```ts
197
- import { BrowserModule } from '@angular/platform-browser';
198
- import { NgModule } from '@angular/core';
199
- import { AppComponent } from './app.component';
200
- import { CommonModule } from '@angular/common';
201
- import { NgSimpleStateModule } from 'ng-simple-state';
202
- import { environment } from '../environments/environment';
192
+ import { Component } from '@angular/core';
203
193
  import { CounterStore } from './counter-store';
204
194
 
205
- @NgModule({
206
- declarations: [AppComponent],
207
- imports: [
208
- BrowserModule,
209
- CommonModule,
210
- NgSimpleStateModule.forRoot({
211
- enableDevTool: !environment.production, // Enable Redux DevTools only in developing
212
- enableLocalStorage: false // Local storage state persistence is globally disabled
213
- })
214
- ],
215
- bootstrap: [AppComponent],
216
- providers: [CounterStore] // The CounterStore state is shared at AppModule level
195
+ @Component({
196
+ selector: 'app-root',
197
+ imports: [CounterStore]
217
198
  })
218
- export class AppModule {}
199
+ export class AppComponent {
200
+
201
+ }
219
202
  ```
220
203
 
221
204
  #### Step 4: Use your store into the components, eg.:
@@ -227,6 +210,7 @@ import { CounterStore } from './counter-store';
227
210
 
228
211
  @Component({
229
212
  selector: 'app-root',
213
+ imports: [CounterStore],
230
214
  template: `
231
215
  <h1>Counter: {{ counter$ | async }}</h1>
232
216
  <button (click)="counterStore.decrement()">Decrement</button>
@@ -252,8 +236,7 @@ export class AppComponent {
252
236
  If you want manage just a component state without make a new service, your component can extend directly `NgSimpleStateBaseRxjsStore`:
253
237
 
254
238
  ```ts
255
- import { Component, Injector } from '@angular/core';
256
- import { CommonModule } from '@angular/common';
239
+ import { Component } from '@angular/core';
257
240
  import { NgSimpleStateBaseRxjsStore } from 'ng-simple-state';
258
241
  import { Observable } from 'rxjs';
259
242
 
@@ -273,10 +256,6 @@ export class CounterComponent extends NgSimpleStateBaseRxjsStore<CounterState> {
273
256
 
274
257
  public counter$: Observable<number> = this.selectState(state => state.count);
275
258
 
276
- constructor(injector: Injector) {
277
- super(injector);
278
- }
279
-
280
259
  storeConfig(): NgSimpleStateStoreConfig {
281
260
  return {
282
261
  storeName: 'CounterComponent'
@@ -299,36 +278,9 @@ export class CounterComponent extends NgSimpleStateBaseRxjsStore<CounterState> {
299
278
  }
300
279
  ```
301
280
 
302
- ### Store's dependency injection
303
-
304
- If you need to inject something into your store (eg. `HttpClient`), you need to also inject the Angular `Injector` service to the super, eg.:
305
-
306
- ```ts
307
- import { Injectable, Injector } from '@angular/core';
308
- import { HttpClient } from '@angular/common/http';
309
- import { NgSimpleStateBaseRxjsStore } from 'ng-simple-state';
310
-
311
- @Injectable()
312
- export class CounterStore extends NgSimpleStateBaseRxjsStore<CounterState> {
313
-
314
- constructor(injector: Injector, private http: HttpClient) {
315
- super(injector);
316
- }
317
-
318
- increment(increment: number = 1): void {
319
- this.http.post<CounterState>('https://localhost:300/api/increment', { increment }).subscribe(response => {
320
- // setState() from default use parent function name as action name for Redux DevTools.
321
- // In this case we provide a second parameter `actionName` because the parent function is anonymous function
322
- this.setState(() => ({ count: response.count }), 'increment');
323
- });
324
- }
325
-
326
- }
327
- ```
328
-
329
281
  ### Override global config
330
282
 
331
- If you need to override the global module configuration provided by `NgSimpleStateModule.forRoot()` you can implement `storeConfig()` and return a specific configuration for the single store, eg.:
283
+ If you need to override the global configuration provided by `provideNgSimpleState()` you can implement `storeConfig()` and return a specific configuration for the single store, eg.:
332
284
 
333
285
  ```ts
334
286
  import { Injectable } from '@angular/core';
@@ -365,7 +317,7 @@ The options are defined by `NgSimpleStateStoreConfig` interface:
365
317
 
366
318
  ```ts
367
319
  import { TestBed } from '@angular/core/testing';
368
- import { NgSimpleStateModule } from 'ng-simple-state';
320
+ import { provideNgSimpleState } from 'ng-simple-state';
369
321
  import { CounterStore } from './counter-store';
370
322
 
371
323
  describe('CounterStore', () => {
@@ -374,15 +326,16 @@ describe('CounterStore', () => {
374
326
 
375
327
  beforeEach(() => {
376
328
  TestBed.configureTestingModule({
377
- imports: [
378
- NgSimpleStateModule.forRoot({
329
+ providers: [
330
+ provideNgSimpleState({
379
331
  enableDevTool: false,
380
332
  enableLocalStorage: false
381
- })
333
+ }),
334
+ CounterStore
382
335
  ]
383
336
  });
384
337
 
385
- counterStore = new CounterStore(TestBed);
338
+ counterStore = TestBed.inject(CounterStore);
386
339
  });
387
340
 
388
341
  it('initialState', () => {
@@ -498,8 +451,6 @@ export abstract class NgSimpleStateBaseRxjsStore<S extends object | Array<any>>
498
451
  */
499
452
  public get state(): BehaviorSubject<S>;
500
453
 
501
- constructor(@Inject(Injector) injector: Injector);
502
-
503
454
  /**
504
455
  * When you override this method, you have to call the `super.ngOnDestroy()` method in your `ngOnDestroy()` method.
505
456
  */
@@ -525,10 +476,9 @@ export abstract class NgSimpleStateBaseRxjsStore<S extends object | Array<any>>
525
476
 
526
477
  /**
527
478
  * Set into the store the initial state
528
- * @param injector current Injector
529
479
  * @returns The state object
530
480
  */
531
- initialState(injector?: Injector): S;
481
+ initialState(): S;
532
482
 
533
483
  /**
534
484
  * Select a store state
@@ -688,31 +638,19 @@ export class CounterStore extends NgSimpleStateBaseSignalStore<CounterState> {
688
638
  }
689
639
  ```
690
640
 
691
- #### Step 3: Inject your store into the providers of the module you want (or the providers of component), eg.:
641
+ #### Step 3: Inject your store into the providers, eg.:
692
642
 
693
643
  ```ts
694
- import { BrowserModule } from '@angular/platform-browser';
695
- import { NgModule } from '@angular/core';
696
- import { AppComponent } from './app.component';
697
- import { CommonModule } from '@angular/common';
698
- import { NgSimpleStateModule } from 'ng-simple-state';
699
- import { environment } from '../environments/environment';
644
+ import { Component } from '@angular/core';
700
645
  import { CounterStore } from './counter-store';
701
646
 
702
- @NgModule({
703
- declarations: [AppComponent],
704
- imports: [
705
- BrowserModule,
706
- CommonModule,
707
- NgSimpleStateModule.forRoot({
708
- enableDevTool: !environment.production, // Enable Redux DevTools only in developing
709
- enableLocalStorage: false // Local storage state persistence is globally disabled
710
- })
711
- ],
712
- bootstrap: [AppComponent],
713
- providers: [CounterStore] // The CounterStore state is shared at AppModule level
647
+ @Component({
648
+ selector: 'app-root',
649
+ imports: [CounterStore]
714
650
  })
715
- export class AppModule {}
651
+ export class AppComponent {
652
+
653
+ }
716
654
  ```
717
655
 
718
656
  #### Step 4: Use your store into the components, eg.:
@@ -748,8 +686,7 @@ export class AppComponent {
748
686
  If you want manage just a component state without make a new service, your component can extend directly `NgSimpleStateBaseSignalStore`:
749
687
 
750
688
  ```ts
751
- import { Component, Injector, Signal } from '@angular/core';
752
- import { CommonModule } from '@angular/common';
689
+ import { Component, Signal } from '@angular/core';
753
690
  import { NgSimpleStateBaseSignalStore } from 'ng-simple-state';
754
691
 
755
692
  export interface CounterState {
@@ -768,10 +705,6 @@ export class CounterComponent extends NgSimpleStateBaseSignalStore<CounterState>
768
705
 
769
706
  public counterSig: Signal<number> = this.selectState(state => state.count);
770
707
 
771
- constructor(injector: Injector) {
772
- super(injector);
773
- }
774
-
775
708
  storeConfig(): NgSimpleStateStoreConfig {
776
709
  return {
777
710
  storeName: 'CounterComponent'
@@ -794,36 +727,9 @@ export class CounterComponent extends NgSimpleStateBaseSignalStore<CounterState>
794
727
  }
795
728
  ```
796
729
 
797
- ### Store's dependency injection
798
-
799
- If you need to inject something into your store (eg. `HttpClient`), you need to also inject the Angular `Injector` service to the super, eg.:
800
-
801
- ```ts
802
- import { Injectable, Injector } from '@angular/core';
803
- import { HttpClient } from '@angular/common/http';
804
- import { NgSimpleStateBaseSignalStore } from 'ng-simple-state';
805
-
806
- @Injectable()
807
- export class CounterStore extends NgSimpleStateBaseSignalStore<CounterState> {
808
-
809
- constructor(injector: Injector, private http: HttpClient) {
810
- super(injector);
811
- }
812
-
813
- increment(increment: number = 1): void {
814
- this.http.post<CounterState>('https://localhost:300/api/increment', { increment }).subscribe(response => {
815
- // setState() from default use parent function name as action name for Redux DevTools.
816
- // In this case we provide a second parameter `actionName` because the parent function is anonymous function
817
- this.setState(() => ({ count: response.count }), 'increment');
818
- });
819
- }
820
-
821
- }
822
- ```
823
-
824
730
  ### Override global config
825
731
 
826
- If you need to override the global module configuration provided by `NgSimpleStateModule.forRoot()` you can implement `storeConfig()` and return a specific configuration for the single store, eg.:
732
+ If you need to override the global configuration provided by `provideNgSimpleState()` you can implement `storeConfig()` and return a specific configuration for the single store, eg.:
827
733
 
828
734
  ```ts
829
735
  import { Injectable } from '@angular/core';
@@ -859,7 +765,7 @@ The options are defined by `NgSimpleStateStoreConfig` interface:
859
765
 
860
766
  ```ts
861
767
  import { TestBed } from '@angular/core/testing';
862
- import { NgSimpleStateModule } from 'ng-simple-state';
768
+ import { provideNgSimpleState } from 'ng-simple-state';
863
769
  import { CounterStore } from './counter-store';
864
770
 
865
771
  describe('CounterStore', () => {
@@ -868,15 +774,16 @@ describe('CounterStore', () => {
868
774
 
869
775
  beforeEach(() => {
870
776
  TestBed.configureTestingModule({
871
- imports: [
872
- NgSimpleStateModule.forRoot({
777
+ providers: [
778
+ provideNgSimpleState({
873
779
  enableDevTool: false,
874
780
  enableLocalStorage: false
875
- })
781
+ }),
782
+ CounterStore
876
783
  ]
877
784
  });
878
785
 
879
- counterStore = new CounterStore(TestBed);
786
+ counterStore = TestBed.inject(CounterStore);
880
787
  });
881
788
 
882
789
  it('initialState', () => {
@@ -988,8 +895,6 @@ export abstract class NgSimpleStateBaseSignalStore<S extends object | Array<any>
988
895
  */
989
896
  public get state(): Signal<S>;
990
897
 
991
- constructor(@Inject(Injector) injector: Injector);
992
-
993
898
  /**
994
899
  * When you override this method, you have to call the `super.ngOnDestroy()` method in your `ngOnDestroy()` method.
995
900
  */
@@ -1015,10 +920,9 @@ export abstract class NgSimpleStateBaseSignalStore<S extends object | Array<any>
1015
920
 
1016
921
  /**
1017
922
  * Set into the store the initial state
1018
- * @param injector current Injector
1019
923
  * @returns The state object
1020
924
  */
1021
- initialState(injector?: Injector): S;
925
+ initialState(): S;
1022
926
 
1023
927
  /**
1024
928
  * Select a store state