5
5
withComputed ,
6
6
withHooks ,
7
7
withMethods ,
8
- EmptyFeatureResult , SignalStoreFeatureResult
8
+ EmptyFeatureResult ,
9
+ SignalStoreFeatureResult ,
9
10
} from '@ngrx/signals' ;
10
11
import { Signal , effect , signal , untracked , isSignal } from '@angular/core' ;
11
12
import { capitalize } from './with-data-service' ;
@@ -16,7 +17,7 @@ export type NormalizedUndoRedoOptions = {
16
17
maxStackSize : number ;
17
18
collections ?: string [ ] ;
18
19
keys : string [ ] ;
19
- skip : number ,
20
+ skip : number ;
20
21
} ;
21
22
22
23
const defaultOptions : NormalizedUndoRedoOptions = {
@@ -41,29 +42,35 @@ type NonNever<T> = T extends never ? never : T;
41
42
42
43
type ExtractEntityCollection < T > = T extends `${infer U } Entities` ? U : never ;
43
44
44
- type ExtractEntityCollections < Store extends SignalStoreFeatureResult > = NonNever < {
45
- [ K in keyof Store [ 'props' ] ] : ExtractEntityCollection < K > ;
46
- } [ keyof Store [ 'props' ] ] > ;
45
+ type ExtractEntityCollections < Store extends SignalStoreFeatureResult > =
46
+ NonNever <
47
+ {
48
+ [ K in keyof Store [ 'props' ] ] : ExtractEntityCollection < K > ;
49
+ } [ keyof Store [ 'props' ] ]
50
+ > ;
47
51
48
- type OptionsForState < Store extends SignalStoreFeatureResult > = Partial < Omit < NormalizedUndoRedoOptions , 'collections' | 'keys' > > & {
52
+ type OptionsForState < Store extends SignalStoreFeatureResult > = Partial <
53
+ Omit < NormalizedUndoRedoOptions , 'collections' | 'keys' >
54
+ > & {
49
55
collections ?: ExtractEntityCollections < Store > [ ] ;
50
56
keys ?: ( keyof Store [ 'state' ] ) [ ] ;
51
57
} ;
52
58
53
- export function withUndoRedo <
54
- Input extends EmptyFeatureResult > ( options ?: OptionsForState < Input > ) : SignalStoreFeature <
59
+ export function withUndoRedo < Input extends EmptyFeatureResult > (
60
+ options ?: OptionsForState < Input >
61
+ ) : SignalStoreFeature <
55
62
Input ,
56
63
EmptyFeatureResult & {
57
- props : {
58
- canUndo : Signal < boolean > ;
59
- canRedo : Signal < boolean > ;
60
- } ;
61
- methods : {
62
- undo : ( ) => void ;
63
- redo : ( ) => void ;
64
- clearStack : ( ) => void ;
65
- } ;
66
- }
64
+ props : {
65
+ canUndo : Signal < boolean > ;
66
+ canRedo : Signal < boolean > ;
67
+ } ;
68
+ methods : {
69
+ undo : ( ) => void ;
70
+ redo : ( ) => void ;
71
+ clearStack : ( ) => void ;
72
+ } ;
73
+ }
67
74
> {
68
75
let previous : StackItem | null = null ;
69
76
let skipOnce = false ;
@@ -130,14 +137,19 @@ export function withUndoRedo<
130
137
clearStack ( ) : void {
131
138
undoStack . splice ( 0 ) ;
132
139
redoStack . splice ( 0 ) ;
140
+
141
+ previous = null ;
142
+
133
143
updateInternal ( ) ;
134
144
} ,
135
145
} ) ) ,
136
146
withHooks ( {
137
147
onInit ( store ) {
138
148
effect ( ( ) => {
139
149
const cand = keys . reduce ( ( acc , key ) => {
140
- const s = ( store as Record < string | keyof Input [ 'state' ] , unknown > ) [ key ] ;
150
+ const s = ( store as Record < string | keyof Input [ 'state' ] , unknown > ) [
151
+ key
152
+ ] ;
141
153
if ( s && isSignal ( s ) ) {
142
154
return {
143
155
...acc ,
0 commit comments