@@ -44,6 +44,143 @@ function getLocationPath(location: LocationIndex[], obj: any) {
44
44
return current ;
45
45
}
46
46
47
+ export class PatchBuilder {
48
+ private operations : PatchOperation [ ] = [ ] ;
49
+
50
+ assign ( location : LocationIndex [ ] , value : any ) {
51
+ this . operations . push ( {
52
+ operation : 'Assign' ,
53
+ location,
54
+ params : { value }
55
+ } ) ;
56
+ return this ;
57
+ }
58
+
59
+ merge ( location : LocationIndex [ ] , value : any ) {
60
+ this . operations . push ( {
61
+ operation : 'Merge' ,
62
+ location,
63
+ params : { value }
64
+ } ) ;
65
+ return this ;
66
+ }
67
+
68
+ extend ( location : LocationIndex [ ] , value : any ) {
69
+ this . operations . push ( {
70
+ operation : 'Extend' ,
71
+ location,
72
+ params : { value }
73
+ } ) ;
74
+ return this ;
75
+ }
76
+
77
+ delete ( location : LocationIndex [ ] ) {
78
+ this . operations . push ( {
79
+ operation : 'Delete' ,
80
+ location,
81
+ params : { }
82
+ } ) ;
83
+ return this ;
84
+ }
85
+
86
+ insert ( location : LocationIndex [ ] , index : number , value : any ) {
87
+ this . operations . push ( {
88
+ operation : 'Insert' ,
89
+ location,
90
+ params : { index, value }
91
+ } ) ;
92
+ return this ;
93
+ }
94
+
95
+ append ( location : LocationIndex [ ] , value : any ) {
96
+ this . operations . push ( {
97
+ operation : 'Append' ,
98
+ location,
99
+ params : { value }
100
+ } ) ;
101
+ return this ;
102
+ }
103
+
104
+ prepend ( location : LocationIndex [ ] , value : any ) {
105
+ this . operations . push ( {
106
+ operation : 'Prepend' ,
107
+ location,
108
+ params : { value }
109
+ } ) ;
110
+ return this ;
111
+ }
112
+
113
+ add ( location : LocationIndex [ ] , value : any ) {
114
+ this . operations . push ( {
115
+ operation : 'Add' ,
116
+ location,
117
+ params : { value }
118
+ } ) ;
119
+ return this ;
120
+ }
121
+
122
+ sub ( location : LocationIndex [ ] , value : any ) {
123
+ this . operations . push ( {
124
+ operation : 'Sub' ,
125
+ location,
126
+ params : { value }
127
+ } ) ;
128
+ return this ;
129
+ }
130
+
131
+ mul ( location : LocationIndex [ ] , value : any ) {
132
+ this . operations . push ( {
133
+ operation : 'Mul' ,
134
+ location,
135
+ params : { value }
136
+ } ) ;
137
+ return this ;
138
+ }
139
+
140
+ div ( location : LocationIndex [ ] , value : any ) {
141
+ this . operations . push ( {
142
+ operation : 'Div' ,
143
+ location,
144
+ params : { value }
145
+ } ) ;
146
+ return this ;
147
+ }
148
+
149
+ clear ( location : LocationIndex [ ] ) {
150
+ this . operations . push ( {
151
+ operation : 'Clear' ,
152
+ location,
153
+ params : { }
154
+ } ) ;
155
+ return this ;
156
+ }
157
+
158
+ reverse ( location : LocationIndex [ ] ) {
159
+ this . operations . push ( {
160
+ operation : 'Reverse' ,
161
+ location,
162
+ params : { }
163
+ } ) ;
164
+ return this ;
165
+ }
166
+
167
+ remove ( location : LocationIndex [ ] , value : any ) {
168
+ this . operations . push ( {
169
+ operation : 'Remove' ,
170
+ location,
171
+ params : { value }
172
+ } ) ;
173
+ return this ;
174
+ }
175
+
176
+ build ( ) {
177
+ return {
178
+ __dash_patch_update : true ,
179
+ operations : this . operations
180
+ } ;
181
+ }
182
+ }
183
+
47
184
const patchHandlers : { [ k : string ] : PatchHandler } = {
48
185
Assign : ( previous , patchOperation ) => {
49
186
const { params, location} = patchOperation ;
@@ -166,3 +303,26 @@ export function handlePatch<T>(previousValue: T, patchValue: any): T {
166
303
167
304
return reducedValue ;
168
305
}
306
+
307
+ export function parsePatchProps ( props : any , previousProps : any ) : { } {
308
+ if ( ! is ( Object , props ) ) {
309
+ return props ;
310
+ }
311
+
312
+ let patchedProps : any = { } ;
313
+
314
+ for ( const key of Object . keys ( props ) ) {
315
+ const val = props [ key ] ;
316
+ if ( isPatch ( val ) ) {
317
+ const previousValue = previousProps [ key ] ;
318
+ if ( previousValue === undefined ) {
319
+ throw new Error ( 'Cannot patch undefined' ) ;
320
+ }
321
+ patchedProps [ key ] = handlePatch ( previousValue , val ) ;
322
+ } else {
323
+ patchedProps [ key ] = val ;
324
+ }
325
+ }
326
+
327
+ return patchedProps ;
328
+ }
0 commit comments