@@ -7,7 +7,7 @@ import { SourceOfTruth } from "./source-of-truth";
7
7
export type MutationVariable = ReturnType < ReturnType < typeof createMutationVariable > > ;
8
8
9
9
export const createMutationVariable =
10
- ( operate : ( operation : Operation ) => void , getValue : ( path : string [ ] ) => unknown ) =>
10
+ ( operate : OperationRequest , getValue : ( path : string [ ] ) => unknown ) =>
11
11
( path : string [ ] ) => {
12
12
const _data : {
13
13
value : unknown ;
@@ -19,10 +19,10 @@ export const createMutationVariable =
19
19
operator : Operator . Set ,
20
20
} ;
21
21
22
- const apply = ( operator : Operator , value ?: any ) => {
22
+ const apply = ( operator : Operator , value ?: any , silentUpdate = false ) => {
23
23
_data . operator = operator ;
24
24
_data . value = value ;
25
- operate ( _data ) ;
25
+ operate ( _data , x => x , silentUpdate ) ;
26
26
} ;
27
27
28
28
const fn = function ( value ?: unknown ) {
@@ -69,11 +69,50 @@ export const createMutationVariable =
69
69
* Reset the value of the property to the default.
70
70
*/
71
71
toggle : ( ) => apply ( Operator . Toggle ) ,
72
+
73
+ // silent update variants
74
+
75
+ $set : ( value : any ) => apply ( Operator . Set , value , true ) ,
76
+
77
+ /**
78
+ * Increase the value of the property.
79
+ */
80
+ $inc : ( ) => apply ( Operator . Increase , null , true ) ,
81
+ /**
82
+ * Increase the value of the property. Loop around if the value is greater than the maximum.
83
+ */
84
+ $incCycle : ( ) => apply ( Operator . IncreaseCycle , null , true ) ,
85
+ /**
86
+ * Decrease the value of the property.
87
+ */
88
+ $dec : ( ) => apply ( Operator . Decrease , null , true ) ,
89
+ /**
90
+ * Decrease the value of the property. Loop around if the value is less than the minimum.
91
+ */
92
+ $decCycle : ( ) => apply ( Operator . DecreaseCycle , null , true ) ,
93
+ /**
94
+ * Set the value of the property to the minimum.
95
+ */
96
+ $min : ( ) => apply ( Operator . Min , null , true ) ,
97
+ /**
98
+ * Set the value of the property to the maximum.
99
+ */
100
+ $max : ( ) => apply ( Operator . Max , null , true ) ,
101
+ /**
102
+ * Reset the value of the property to the default.
103
+ */
104
+ $reset : ( ) => apply ( Operator . SetToDefault , null , true ) ,
105
+ /**
106
+ * Reset the value of the property to the default.
107
+ */
108
+ $toggle : ( ) => apply ( Operator . Toggle , null , true ) ,
72
109
} ) ;
73
110
} ;
74
111
112
+ type OperationRequest = ( action : Operation , transformPath ?: ( path : string [ ] ) => string [ ] , silentUpdate ?: boolean ) => void ;
113
+
75
114
export type OperatableStore < T extends object > = DeepStore < T > & {
76
- operate : ( action : Operation , transformPath ?: ( path : string [ ] ) => string [ ] ) => void ;
115
+ operate : OperationRequest ;
77
116
createVariable : ( path : string [ ] ) => MutationVariable ;
78
117
sourceOfTruth : SourceOfTruth < T > ;
79
118
} ;
@@ -98,7 +137,9 @@ export function createOperatableStore<T extends object>(
98
137
99
138
const operate = (
100
139
operation : Operation ,
101
- transformPath : ( path : string [ ] ) => string [ ] = ( x ) => x
140
+ // this should probably belong to the store itself
141
+ transformPath : ( path : string [ ] ) => string [ ] = ( x ) => x ,
142
+ silentUpdate = false
102
143
) => {
103
144
const path = transformPath ( operation . path ) ;
104
145
@@ -112,7 +153,8 @@ export function createOperatableStore<T extends object>(
112
153
field ,
113
154
operation . value ,
114
155
sourceOfTruth . getValue ( path )
115
- )
156
+ ) ,
157
+ silentUpdate
116
158
) ;
117
159
} else {
118
160
log . warn ( "Session field is no found." ) ;
0 commit comments