Skip to content

Commit ddcfc61

Browse files
committed
A little DRYer setTrap
1 parent e116658 commit ddcfc61

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/jsonpatcherproxy.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,23 @@ const JSONPatcherProxy = (function() {
109109
}
110110
if (typeof newValue == 'undefined') {
111111
let reflectionResult;
112+
let operation;
112113
if (target.hasOwnProperty(key)) {
113114
// when array element is set to `undefined`, should generate replace to `null`
114115
if (Array.isArray(target)) {
115116
reflectionResult = Reflect.set(target, key, newValue);
116117
//undefined array elements are JSON.stringified to `null`
117-
instance.defaultCallback({
118+
operation = {
118119
op: 'replace',
119120
path: destinationPropKey,
120121
value: null
121-
});
122+
};
122123
} else {
123124
reflectionResult = Reflect.set(target, key, newValue);
124-
instance.defaultCallback({
125+
operation = {
125126
op: 'remove',
126127
path: destinationPropKey
127-
});
128+
};
128129
}
129130
const oldValue = instance.proxifiedObjectsMap.get(target[key]);
130131
// was the deleted a proxified object?
@@ -133,6 +134,7 @@ const JSONPatcherProxy = (function() {
133134
instance.disableTrapsForProxy(oldValue);
134135
instance.proxifiedObjectsMap.delete(oldValue);
135136
}
137+
instance.defaultCallback(operation);
136138
return reflectionResult;
137139
} else if (!Array.isArray(target)) {
138140
return Reflect.set(target, key, newValue);
@@ -143,40 +145,46 @@ const JSONPatcherProxy = (function() {
143145
return Reflect.set(target, key, newValue);
144146
}
145147
let reflectionResult;
148+
let operation;
146149
if (target.hasOwnProperty(key)) {
147150
if (typeof target[key] == 'undefined') {
148151
if (Array.isArray(target)) {
149152
reflectionResult = Reflect.set(target, key, newValue);
150-
instance.defaultCallback({
153+
operation = {
151154
op: 'replace',
152155
path: destinationPropKey,
153156
value: newValue
154-
});
157+
};
155158
} else {
156159
reflectionResult = Reflect.set(target, key, newValue);
157-
instance.defaultCallback({
160+
operation = {
158161
op: 'add',
159162
path: destinationPropKey,
160163
value: newValue
161-
});
164+
};
162165
}
166+
instance.defaultCallback(operation);
163167
return reflectionResult;
164168
} else {
165169
reflectionResult = Reflect.set(target, key, newValue);
166-
instance.defaultCallback({
167-
op: 'replace',
168-
path: destinationPropKey,
169-
value: newValue
170-
});
170+
instance.defaultCallback(
171+
/* operation = */ {
172+
op: 'replace',
173+
path: destinationPropKey,
174+
value: newValue
175+
}
176+
);
171177
return reflectionResult;
172178
}
173179
} else {
174180
reflectionResult = Reflect.set(target, key, newValue);
175-
instance.defaultCallback({
176-
op: 'add',
177-
path: destinationPropKey,
178-
value: newValue
179-
});
181+
instance.defaultCallback(
182+
/* operation = */ {
183+
op: 'add',
184+
path: destinationPropKey,
185+
value: newValue
186+
}
187+
);
180188
return reflectionResult;
181189
}
182190
}

0 commit comments

Comments
 (0)