File tree Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -78,6 +78,27 @@ describe('withReducer', () => {
78
78
expect ( getState ( store ) ) . toEqual ( { count : 2 , count2 : 11 } ) ;
79
79
} ) ;
80
80
81
+ it ( 'has access to the current state' , ( ) => {
82
+ const incrementBy = event ( 'incrementBy' , type < number > ( ) ) ;
83
+
84
+ const CounterStore = signalStore (
85
+ { providedIn : 'root' } ,
86
+ withState ( { count : 10 , count2 : 0 } ) ,
87
+ withReducer (
88
+ on ( incrementBy , ( { payload } , state ) => ( {
89
+ count : state . count + payload ,
90
+ } ) )
91
+ )
92
+ ) ;
93
+
94
+ const store = TestBed . inject ( CounterStore ) ;
95
+ const dispatcher = TestBed . inject ( Dispatcher ) ;
96
+
97
+ dispatcher . dispatch ( incrementBy ( 20 ) ) ;
98
+
99
+ expect ( getState ( store ) ) . toEqual ( { count : 30 , count2 : 0 } ) ;
100
+ } ) ;
101
+
81
102
it ( 'allows listening to multiple events' , ( ) => {
82
103
const set = event ( 'set' , type < number > ( ) ) ;
83
104
const set_ = event ( 'set_' , type < number > ( ) ) ;
Original file line number Diff line number Diff line change @@ -13,7 +13,8 @@ type CaseReducer<
13
13
State extends object ,
14
14
EventCreators extends EventCreator < string , any > [ ]
15
15
> = (
16
- event : { [ K in keyof EventCreators ] : ReturnType < EventCreators [ K ] > } [ number ]
16
+ event : { [ K in keyof EventCreators ] : ReturnType < EventCreators [ K ] > } [ number ] ,
17
+ state : State
17
18
) =>
18
19
| Partial < State >
19
20
| PartialStateUpdater < State >
Original file line number Diff line number Diff line change 1
- import { inject } from '@angular/core' ;
1
+ import { inject , untracked } from '@angular/core' ;
2
2
import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
3
3
import { merge , tap } from 'rxjs' ;
4
4
import {
5
5
EmptyFeatureResult ,
6
+ getState ,
6
7
patchState ,
7
8
SignalStoreFeature ,
8
9
signalStoreFeature ,
@@ -48,7 +49,8 @@ export function withReducer<State extends object>(
48
49
const updates = caseReducers . map ( ( caseReducer ) =>
49
50
events . on ( ...caseReducer . events ) . pipe (
50
51
tap ( ( event ) => {
51
- const result = caseReducer . reducer ( event ) ;
52
+ const state = untracked ( ( ) => getState ( store ) ) ;
53
+ const result = caseReducer . reducer ( event , state ) ;
52
54
const updaters = Array . isArray ( result ) ? result : [ result ] ;
53
55
54
56
patchState ( store , ...updaters ) ;
You can’t perform that action at this time.
0 commit comments