@tanstack/db 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/collection.cjs +182 -113
- package/dist/cjs/collection.cjs.map +1 -1
- package/dist/cjs/collection.d.cts +43 -15
- package/dist/cjs/index.cjs +1 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/proxy.cjs +87 -248
- package/dist/cjs/proxy.cjs.map +1 -1
- package/dist/cjs/proxy.d.cts +5 -5
- package/dist/cjs/query/compiled-query.cjs +23 -14
- package/dist/cjs/query/compiled-query.cjs.map +1 -1
- package/dist/cjs/query/compiled-query.d.cts +3 -1
- package/dist/cjs/query/evaluators.cjs +35 -20
- package/dist/cjs/query/evaluators.cjs.map +1 -1
- package/dist/cjs/query/evaluators.d.cts +8 -3
- package/dist/cjs/query/extractors.cjs +20 -20
- package/dist/cjs/query/extractors.cjs.map +1 -1
- package/dist/cjs/query/extractors.d.cts +3 -3
- package/dist/cjs/query/group-by.cjs +12 -15
- package/dist/cjs/query/group-by.cjs.map +1 -1
- package/dist/cjs/query/group-by.d.cts +7 -7
- package/dist/cjs/query/joins.cjs +41 -55
- package/dist/cjs/query/joins.cjs.map +1 -1
- package/dist/cjs/query/joins.d.cts +3 -3
- package/dist/cjs/query/order-by.cjs +37 -84
- package/dist/cjs/query/order-by.cjs.map +1 -1
- package/dist/cjs/query/order-by.d.cts +2 -2
- package/dist/cjs/query/pipeline-compiler.cjs +13 -18
- package/dist/cjs/query/pipeline-compiler.cjs.map +1 -1
- package/dist/cjs/query/pipeline-compiler.d.cts +2 -1
- package/dist/cjs/query/query-builder.cjs +22 -29
- package/dist/cjs/query/query-builder.cjs.map +1 -1
- package/dist/cjs/query/query-builder.d.cts +16 -10
- package/dist/cjs/query/schema.d.cts +12 -11
- package/dist/cjs/query/select.cjs +47 -24
- package/dist/cjs/query/select.cjs.map +1 -1
- package/dist/cjs/query/select.d.cts +2 -2
- package/dist/cjs/query/types.d.cts +1 -0
- package/dist/cjs/transactions.cjs +20 -9
- package/dist/cjs/transactions.cjs.map +1 -1
- package/dist/cjs/types.d.cts +66 -7
- package/dist/esm/collection.d.ts +43 -15
- package/dist/esm/collection.js +183 -114
- package/dist/esm/collection.js.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/proxy.d.ts +5 -5
- package/dist/esm/proxy.js +87 -248
- package/dist/esm/proxy.js.map +1 -1
- package/dist/esm/query/compiled-query.d.ts +3 -1
- package/dist/esm/query/compiled-query.js +23 -14
- package/dist/esm/query/compiled-query.js.map +1 -1
- package/dist/esm/query/evaluators.d.ts +8 -3
- package/dist/esm/query/evaluators.js +36 -21
- package/dist/esm/query/evaluators.js.map +1 -1
- package/dist/esm/query/extractors.d.ts +3 -3
- package/dist/esm/query/extractors.js +20 -20
- package/dist/esm/query/extractors.js.map +1 -1
- package/dist/esm/query/group-by.d.ts +7 -7
- package/dist/esm/query/group-by.js +14 -17
- package/dist/esm/query/group-by.js.map +1 -1
- package/dist/esm/query/joins.d.ts +3 -3
- package/dist/esm/query/joins.js +42 -56
- package/dist/esm/query/joins.js.map +1 -1
- package/dist/esm/query/order-by.d.ts +2 -2
- package/dist/esm/query/order-by.js +39 -86
- package/dist/esm/query/order-by.js.map +1 -1
- package/dist/esm/query/pipeline-compiler.d.ts +2 -1
- package/dist/esm/query/pipeline-compiler.js +14 -19
- package/dist/esm/query/pipeline-compiler.js.map +1 -1
- package/dist/esm/query/query-builder.d.ts +16 -10
- package/dist/esm/query/query-builder.js +22 -29
- package/dist/esm/query/query-builder.js.map +1 -1
- package/dist/esm/query/schema.d.ts +12 -11
- package/dist/esm/query/select.d.ts +2 -2
- package/dist/esm/query/select.js +48 -25
- package/dist/esm/query/select.js.map +1 -1
- package/dist/esm/query/types.d.ts +1 -0
- package/dist/esm/transactions.js +20 -9
- package/dist/esm/transactions.js.map +1 -1
- package/dist/esm/types.d.ts +66 -7
- package/package.json +2 -2
- package/src/collection.ts +286 -146
- package/src/proxy.ts +141 -358
- package/src/query/compiled-query.ts +30 -15
- package/src/query/evaluators.ts +49 -21
- package/src/query/extractors.ts +24 -21
- package/src/query/group-by.ts +24 -22
- package/src/query/joins.ts +88 -75
- package/src/query/order-by.ts +56 -106
- package/src/query/pipeline-compiler.ts +34 -37
- package/src/query/query-builder.ts +49 -46
- package/src/query/schema.ts +18 -15
- package/src/query/select.ts +68 -33
- package/src/query/types.ts +1 -0
- package/src/transactions.ts +30 -14
- package/src/types.ts +76 -7
- package/dist/cjs/query/key-by.cjs +0 -43
- package/dist/cjs/query/key-by.cjs.map +0 -1
- package/dist/cjs/query/key-by.d.cts +0 -3
- package/dist/esm/query/key-by.d.ts +0 -3
- package/dist/esm/query/key-by.js +0 -43
- package/dist/esm/query/key-by.js.map +0 -1
- package/src/query/key-by.ts +0 -61
package/dist/esm/proxy.js
CHANGED
|
@@ -2,7 +2,10 @@ function debugLog(...args) {
|
|
|
2
2
|
const isBrowser = typeof window !== `undefined` && typeof localStorage !== `undefined`;
|
|
3
3
|
if (isBrowser && localStorage.getItem(`DEBUG`) === `true`) {
|
|
4
4
|
console.log(`[proxy]`, ...args);
|
|
5
|
-
} else if (
|
|
5
|
+
} else if (
|
|
6
|
+
// true
|
|
7
|
+
!isBrowser && typeof process !== `undefined` && process.env.DEBUG === `true`
|
|
8
|
+
) {
|
|
6
9
|
console.log(`[proxy]`, ...args);
|
|
7
10
|
}
|
|
8
11
|
}
|
|
@@ -129,24 +132,48 @@ function deepEqual(a, b) {
|
|
|
129
132
|
(key) => Object.prototype.hasOwnProperty.call(b, key) && deepEqual(a[key], b[key])
|
|
130
133
|
);
|
|
131
134
|
}
|
|
135
|
+
let count = 0;
|
|
136
|
+
function getProxyCount() {
|
|
137
|
+
count += 1;
|
|
138
|
+
return count;
|
|
139
|
+
}
|
|
132
140
|
function createChangeProxy(target, parent) {
|
|
133
|
-
const
|
|
141
|
+
const changeProxyCache = /* @__PURE__ */ new Map();
|
|
142
|
+
function memoizedCreateChangeProxy(innerTarget, innerParent) {
|
|
143
|
+
debugLog(`Object ID:`, innerTarget.constructor.name);
|
|
144
|
+
if (changeProxyCache.has(innerTarget)) {
|
|
145
|
+
return changeProxyCache.get(innerTarget);
|
|
146
|
+
} else {
|
|
147
|
+
const changeProxy = createChangeProxy(innerTarget, innerParent);
|
|
148
|
+
changeProxyCache.set(innerTarget, changeProxy);
|
|
149
|
+
return changeProxy;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const proxyCache = /* @__PURE__ */ new Map();
|
|
134
153
|
const changeTracker = {
|
|
135
|
-
|
|
154
|
+
copy_: deepClone(target),
|
|
136
155
|
originalObject: deepClone(target),
|
|
137
|
-
|
|
156
|
+
proxyCount: getProxyCount(),
|
|
138
157
|
modified: false,
|
|
139
158
|
assigned_: {},
|
|
140
159
|
parent,
|
|
141
160
|
target
|
|
142
161
|
// Store reference to the target object
|
|
143
162
|
};
|
|
163
|
+
debugLog(
|
|
164
|
+
`createChangeProxy called for target`,
|
|
165
|
+
target,
|
|
166
|
+
changeTracker.proxyCount
|
|
167
|
+
);
|
|
144
168
|
function markChanged(state) {
|
|
145
169
|
if (!state.modified) {
|
|
146
170
|
state.modified = true;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
171
|
+
}
|
|
172
|
+
if (state.parent) {
|
|
173
|
+
debugLog(`propagating change to parent`);
|
|
174
|
+
state.parent.tracker.copy_[state.parent.prop] = state.copy_;
|
|
175
|
+
state.parent.tracker.assigned_[state.parent.prop] = true;
|
|
176
|
+
markChanged(state.parent.tracker);
|
|
150
177
|
}
|
|
151
178
|
}
|
|
152
179
|
function checkIfReverted(state) {
|
|
@@ -160,7 +187,7 @@ function createChangeProxy(target, parent) {
|
|
|
160
187
|
}
|
|
161
188
|
for (const prop in state.assigned_) {
|
|
162
189
|
if (state.assigned_[prop] === true) {
|
|
163
|
-
const currentValue = state.copy_
|
|
190
|
+
const currentValue = state.copy_[prop];
|
|
164
191
|
const originalValue = state.originalObject[prop];
|
|
165
192
|
debugLog(
|
|
166
193
|
`Checking property ${String(prop)}, current:`,
|
|
@@ -179,14 +206,14 @@ function createChangeProxy(target, parent) {
|
|
|
179
206
|
}
|
|
180
207
|
const symbolProps = Object.getOwnPropertySymbols(state.assigned_);
|
|
181
208
|
for (const sym of symbolProps) {
|
|
182
|
-
if (state.assigned_[sym
|
|
183
|
-
const currentValue = state.copy_
|
|
209
|
+
if (state.assigned_[sym] === true) {
|
|
210
|
+
const currentValue = state.copy_[sym];
|
|
184
211
|
const originalValue = state.originalObject[sym];
|
|
185
212
|
if (!deepEqual(currentValue, originalValue)) {
|
|
186
213
|
debugLog(`Symbol property is different, returning false`);
|
|
187
214
|
return false;
|
|
188
215
|
}
|
|
189
|
-
} else if (state.assigned_[sym
|
|
216
|
+
} else if (state.assigned_[sym] === false) {
|
|
190
217
|
debugLog(`Symbol property was deleted, returning false`);
|
|
191
218
|
return false;
|
|
192
219
|
}
|
|
@@ -194,31 +221,6 @@ function createChangeProxy(target, parent) {
|
|
|
194
221
|
debugLog(`All properties match original values, returning true`);
|
|
195
222
|
return true;
|
|
196
223
|
}
|
|
197
|
-
function updateModifiedStatus(state) {
|
|
198
|
-
debugLog(
|
|
199
|
-
`updateModifiedStatus called, assigned keys:`,
|
|
200
|
-
Object.keys(state.assigned_)
|
|
201
|
-
);
|
|
202
|
-
if (Object.keys(state.assigned_).length === 0 && Object.getOwnPropertySymbols(state.assigned_).length === 0) {
|
|
203
|
-
debugLog(`No assigned properties, returning false`);
|
|
204
|
-
return false;
|
|
205
|
-
}
|
|
206
|
-
const isReverted = checkIfReverted(state);
|
|
207
|
-
debugLog(`checkIfReverted returned:`, isReverted);
|
|
208
|
-
if (!isReverted) {
|
|
209
|
-
debugLog(`Object has changes that aren't reverted, returning true`);
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
debugLog(`All changes reverted, clearing tracking`);
|
|
213
|
-
state.modified = false;
|
|
214
|
-
state.changes = {};
|
|
215
|
-
state.assigned_ = {};
|
|
216
|
-
if (state.parent) {
|
|
217
|
-
debugLog(`Checking parent status for prop:`, state.parent.prop);
|
|
218
|
-
checkParentStatus(state.parent.tracker, state.parent.prop);
|
|
219
|
-
}
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
224
|
function checkParentStatus(parentState, childProp) {
|
|
223
225
|
debugLog(`checkParentStatus called for child prop:`, childProp);
|
|
224
226
|
const isReverted = checkIfReverted(parentState);
|
|
@@ -226,7 +228,6 @@ function createChangeProxy(target, parent) {
|
|
|
226
228
|
if (isReverted) {
|
|
227
229
|
debugLog(`Parent is fully reverted, clearing tracking`);
|
|
228
230
|
parentState.modified = false;
|
|
229
|
-
parentState.changes = {};
|
|
230
231
|
parentState.assigned_ = {};
|
|
231
232
|
if (parentState.parent) {
|
|
232
233
|
debugLog(`Continuing up the parent chain`);
|
|
@@ -235,12 +236,17 @@ function createChangeProxy(target, parent) {
|
|
|
235
236
|
}
|
|
236
237
|
}
|
|
237
238
|
function createObjectProxy(obj) {
|
|
239
|
+
debugLog(`createObjectProxy`, obj);
|
|
238
240
|
if (proxyCache.has(obj)) {
|
|
241
|
+
debugLog(`proxyCache found match`);
|
|
239
242
|
return proxyCache.get(obj);
|
|
240
243
|
}
|
|
241
244
|
const proxy2 = new Proxy(obj, {
|
|
242
245
|
get(ptarget, prop) {
|
|
243
|
-
|
|
246
|
+
debugLog(`get`, ptarget, prop);
|
|
247
|
+
const value = changeTracker.copy_[prop] ?? changeTracker.originalObject[prop];
|
|
248
|
+
const originalValue = changeTracker.originalObject[prop];
|
|
249
|
+
debugLog(`value (at top of proxy get)`, value);
|
|
244
250
|
const desc = Object.getOwnPropertyDescriptor(ptarget, prop);
|
|
245
251
|
if (desc == null ? void 0 : desc.get) {
|
|
246
252
|
return value;
|
|
@@ -263,7 +269,7 @@ function createChangeProxy(target, parent) {
|
|
|
263
269
|
]);
|
|
264
270
|
if (modifyingMethods.has(methodName)) {
|
|
265
271
|
return function(...args) {
|
|
266
|
-
const result = value.apply(
|
|
272
|
+
const result = value.apply(changeTracker.copy_, args);
|
|
267
273
|
markChanged(changeTracker);
|
|
268
274
|
return result;
|
|
269
275
|
};
|
|
@@ -277,7 +283,7 @@ function createChangeProxy(target, parent) {
|
|
|
277
283
|
]);
|
|
278
284
|
if (iteratorMethods.has(methodName) || prop === Symbol.iterator) {
|
|
279
285
|
return function(...args) {
|
|
280
|
-
const result = value.apply(
|
|
286
|
+
const result = value.apply(changeTracker.copy_, args);
|
|
281
287
|
if (methodName === `forEach`) {
|
|
282
288
|
const callback = args[0];
|
|
283
289
|
if (typeof callback === `function`) {
|
|
@@ -305,25 +311,19 @@ function createChangeProxy(target, parent) {
|
|
|
305
311
|
if (!nextResult.done && nextResult.value && typeof nextResult.value === `object`) {
|
|
306
312
|
if (methodName === `entries` && Array.isArray(nextResult.value) && nextResult.value.length === 2) {
|
|
307
313
|
if (nextResult.value[1] && typeof nextResult.value[1] === `object`) {
|
|
308
|
-
const { proxy: valueProxy } =
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
prop: typeof nextResult.value[0] === `symbol` ? nextResult.value[0] : String(nextResult.value[0])
|
|
313
|
-
}
|
|
314
|
-
);
|
|
314
|
+
const { proxy: valueProxy } = memoizedCreateChangeProxy(nextResult.value[1], {
|
|
315
|
+
tracker: changeTracker,
|
|
316
|
+
prop: typeof nextResult.value[0] === `symbol` ? nextResult.value[0] : String(nextResult.value[0])
|
|
317
|
+
});
|
|
315
318
|
nextResult.value[1] = valueProxy;
|
|
316
319
|
}
|
|
317
320
|
} else if (methodName === `values` || methodName === Symbol.iterator.toString() || prop === Symbol.iterator) {
|
|
318
321
|
if (typeof nextResult.value === `object` && nextResult.value !== null) {
|
|
319
322
|
const tempKey = Symbol(`iterator-value`);
|
|
320
|
-
const { proxy: valueProxy } =
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
prop: tempKey
|
|
325
|
-
}
|
|
326
|
-
);
|
|
323
|
+
const { proxy: valueProxy } = memoizedCreateChangeProxy(nextResult.value, {
|
|
324
|
+
tracker: changeTracker,
|
|
325
|
+
prop: tempKey
|
|
326
|
+
});
|
|
327
327
|
nextResult.value = valueProxy;
|
|
328
328
|
}
|
|
329
329
|
}
|
|
@@ -346,59 +346,45 @@ function createChangeProxy(target, parent) {
|
|
|
346
346
|
tracker: changeTracker,
|
|
347
347
|
prop: String(prop)
|
|
348
348
|
};
|
|
349
|
-
const { proxy: nestedProxy } =
|
|
349
|
+
const { proxy: nestedProxy } = memoizedCreateChangeProxy(
|
|
350
|
+
originalValue,
|
|
351
|
+
nestedParent
|
|
352
|
+
);
|
|
350
353
|
proxyCache.set(value, nestedProxy);
|
|
351
354
|
return nestedProxy;
|
|
352
355
|
}
|
|
353
356
|
return value;
|
|
354
357
|
},
|
|
355
358
|
set(sobj, prop, value) {
|
|
356
|
-
const currentValue =
|
|
359
|
+
const currentValue = changeTracker.copy_[prop];
|
|
357
360
|
debugLog(
|
|
358
361
|
`set called for property ${String(prop)}, current:`,
|
|
359
362
|
currentValue,
|
|
360
363
|
`new:`,
|
|
361
364
|
value
|
|
362
365
|
);
|
|
363
|
-
if (Array.isArray(sobj) && prop === `length`) {
|
|
364
|
-
const newLength = Number(value);
|
|
365
|
-
const oldLength = sobj.length;
|
|
366
|
-
const newArray = Array.from(
|
|
367
|
-
{ length: newLength },
|
|
368
|
-
(_, i) => i < oldLength ? sobj[i] : void 0
|
|
369
|
-
);
|
|
370
|
-
if (parent) {
|
|
371
|
-
parent.tracker.changes[parent.prop] = newArray;
|
|
372
|
-
parent.tracker.assigned_[parent.prop] = true;
|
|
373
|
-
markChanged(parent.tracker);
|
|
374
|
-
}
|
|
375
|
-
sobj.length = newLength;
|
|
376
|
-
return true;
|
|
377
|
-
}
|
|
378
366
|
if (!deepEqual(currentValue, value)) {
|
|
379
367
|
const originalValue = changeTracker.originalObject[prop];
|
|
380
368
|
const isRevertToOriginal = deepEqual(value, originalValue);
|
|
381
369
|
debugLog(
|
|
382
|
-
`
|
|
370
|
+
`value:`,
|
|
371
|
+
value,
|
|
372
|
+
`original:`,
|
|
383
373
|
originalValue,
|
|
384
374
|
`isRevertToOriginal:`,
|
|
385
375
|
isRevertToOriginal
|
|
386
376
|
);
|
|
387
377
|
if (isRevertToOriginal) {
|
|
388
378
|
debugLog(`Reverting property ${String(prop)} to original value`);
|
|
389
|
-
delete changeTracker.changes[prop.toString()];
|
|
390
379
|
delete changeTracker.assigned_[prop.toString()];
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
changeTracker.copy_[prop] = deepClone(originalValue);
|
|
394
|
-
}
|
|
380
|
+
debugLog(`Updating copy with original value for ${String(prop)}`);
|
|
381
|
+
changeTracker.copy_[prop] = deepClone(originalValue);
|
|
395
382
|
debugLog(`Checking if all properties reverted`);
|
|
396
383
|
const allReverted = checkIfReverted(changeTracker);
|
|
397
384
|
debugLog(`All reverted:`, allReverted);
|
|
398
385
|
if (allReverted) {
|
|
399
386
|
debugLog(`All properties reverted, clearing tracking`);
|
|
400
387
|
changeTracker.modified = false;
|
|
401
|
-
changeTracker.changes = {};
|
|
402
388
|
changeTracker.assigned_ = {};
|
|
403
389
|
if (parent) {
|
|
404
390
|
debugLog(`Updating parent for property:`, parent.prop);
|
|
@@ -410,14 +396,9 @@ function createChangeProxy(target, parent) {
|
|
|
410
396
|
}
|
|
411
397
|
} else {
|
|
412
398
|
debugLog(`Setting new value for property ${String(prop)}`);
|
|
413
|
-
|
|
414
|
-
if (changeTracker.copy_) {
|
|
415
|
-
changeTracker.copy_[prop] = value;
|
|
416
|
-
}
|
|
417
|
-
obj[prop] = value;
|
|
399
|
+
changeTracker.copy_[prop] = value;
|
|
418
400
|
changeTracker.assigned_[prop.toString()] = true;
|
|
419
|
-
|
|
420
|
-
debugLog(`Marking object and ancestors as modified`);
|
|
401
|
+
debugLog(`Marking object and ancestors as modified`, changeTracker);
|
|
421
402
|
markChanged(changeTracker);
|
|
422
403
|
}
|
|
423
404
|
} else {
|
|
@@ -426,30 +407,21 @@ function createChangeProxy(target, parent) {
|
|
|
426
407
|
return true;
|
|
427
408
|
},
|
|
428
409
|
defineProperty(ptarget, prop, descriptor) {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
changeTracker.assigned_[prop.toString()] = true;
|
|
434
|
-
markChanged(changeTracker);
|
|
435
|
-
}
|
|
410
|
+
if (`value` in descriptor) {
|
|
411
|
+
changeTracker.copy_[prop] = deepClone(descriptor.value);
|
|
412
|
+
changeTracker.assigned_[prop.toString()] = true;
|
|
413
|
+
markChanged(changeTracker);
|
|
436
414
|
}
|
|
437
|
-
return
|
|
438
|
-
},
|
|
439
|
-
setPrototypeOf(ptarget, proto) {
|
|
440
|
-
return Object.setPrototypeOf(ptarget, proto);
|
|
415
|
+
return true;
|
|
441
416
|
},
|
|
442
417
|
deleteProperty(dobj, prop) {
|
|
418
|
+
debugLog(`deleteProperty`, dobj, prop);
|
|
443
419
|
const stringProp = typeof prop === `symbol` ? prop.toString() : prop;
|
|
444
420
|
if (stringProp in dobj) {
|
|
445
421
|
const hadPropertyInOriginal = stringProp in changeTracker.originalObject;
|
|
446
|
-
|
|
447
|
-
if (changeTracker.copy_) {
|
|
448
|
-
delete changeTracker.copy_[prop];
|
|
449
|
-
}
|
|
450
|
-
delete dobj[prop];
|
|
422
|
+
delete changeTracker.copy_[prop];
|
|
451
423
|
if (!hadPropertyInOriginal) {
|
|
452
|
-
delete changeTracker.
|
|
424
|
+
delete changeTracker.copy_[stringProp];
|
|
453
425
|
delete changeTracker.assigned_[stringProp];
|
|
454
426
|
if (Object.keys(changeTracker.assigned_).length === 0 && Object.getOwnPropertySymbols(changeTracker.assigned_).length === 0) {
|
|
455
427
|
changeTracker.modified = false;
|
|
@@ -458,7 +430,7 @@ function createChangeProxy(target, parent) {
|
|
|
458
430
|
}
|
|
459
431
|
} else {
|
|
460
432
|
changeTracker.assigned_[stringProp] = false;
|
|
461
|
-
changeTracker.
|
|
433
|
+
changeTracker.copy_[stringProp] = void 0;
|
|
462
434
|
markChanged(changeTracker);
|
|
463
435
|
}
|
|
464
436
|
}
|
|
@@ -472,133 +444,26 @@ function createChangeProxy(target, parent) {
|
|
|
472
444
|
return {
|
|
473
445
|
proxy,
|
|
474
446
|
getChanges: () => {
|
|
475
|
-
debugLog(
|
|
476
|
-
|
|
477
|
-
changeTracker.modified,
|
|
478
|
-
`assigned keys:`,
|
|
479
|
-
Object.keys(changeTracker.assigned_)
|
|
480
|
-
);
|
|
447
|
+
debugLog(`getChanges called, modified:`, changeTracker.modified);
|
|
448
|
+
debugLog(changeTracker);
|
|
481
449
|
if (!changeTracker.modified) {
|
|
482
450
|
debugLog(`Object not modified, returning empty object`);
|
|
483
451
|
return {};
|
|
484
452
|
}
|
|
485
|
-
if (
|
|
486
|
-
|
|
487
|
-
if (changeTracker.copy_) {
|
|
488
|
-
debugLog(`Comparing copy with original`);
|
|
489
|
-
if (deepEqual(changeTracker.copy_, changeTracker.originalObject)) {
|
|
490
|
-
debugLog(`Copy equals original, returning empty object`);
|
|
491
|
-
changeTracker.modified = false;
|
|
492
|
-
return {};
|
|
493
|
-
}
|
|
494
|
-
} else if (deepEqual(target, changeTracker.originalObject)) {
|
|
495
|
-
debugLog(`Target equals original, returning empty object`);
|
|
496
|
-
changeTracker.modified = false;
|
|
497
|
-
changeTracker.changes = {};
|
|
498
|
-
changeTracker.assigned_ = {};
|
|
499
|
-
return {};
|
|
500
|
-
}
|
|
453
|
+
if (typeof changeTracker.copy_ !== `object` || Array.isArray(changeTracker.copy_)) {
|
|
454
|
+
return changeTracker.copy_;
|
|
501
455
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
if (!changeTracker.modified) {
|
|
505
|
-
debugLog(`No longer modified after check, returning empty object`);
|
|
506
|
-
return {};
|
|
456
|
+
if (Object.keys(changeTracker.assigned_).length === 0) {
|
|
457
|
+
return changeTracker.copy_;
|
|
507
458
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
objToCheck,
|
|
513
|
-
changeTracker.originalObject
|
|
514
|
-
);
|
|
515
|
-
if (deepEqual(objToCheck, changeTracker.originalObject)) {
|
|
516
|
-
debugLog(`Object equals original, returning empty object`);
|
|
517
|
-
changeTracker.modified = false;
|
|
518
|
-
changeTracker.changes = {};
|
|
519
|
-
changeTracker.assigned_ = {};
|
|
520
|
-
return {};
|
|
459
|
+
const result = {};
|
|
460
|
+
for (const key in changeTracker.copy_) {
|
|
461
|
+
if (changeTracker.assigned_[key] === true && key in changeTracker.copy_) {
|
|
462
|
+
result[key] = changeTracker.copy_[key];
|
|
521
463
|
}
|
|
522
464
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
const changes = {};
|
|
526
|
-
for (const key in changeTracker.assigned_) {
|
|
527
|
-
if (changeTracker.assigned_[key] === true) {
|
|
528
|
-
changes[key] = deepClone(changeTracker.copy_[key]);
|
|
529
|
-
} else if (changeTracker.assigned_[key] === false) {
|
|
530
|
-
changes[key] = void 0;
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
const symbolProps = Object.getOwnPropertySymbols(
|
|
534
|
-
changeTracker.assigned_
|
|
535
|
-
);
|
|
536
|
-
for (const sym of symbolProps) {
|
|
537
|
-
if (changeTracker.assigned_[sym.toString()] === true) {
|
|
538
|
-
const value = changeTracker.copy_[sym];
|
|
539
|
-
changes[sym.toString()] = deepClone(value);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
return changes;
|
|
543
|
-
}
|
|
544
|
-
return changeTracker.changes;
|
|
545
|
-
}
|
|
546
|
-
if (changeTracker.modified && !parent) {
|
|
547
|
-
debugLog(`Root object with nested changes, checking deep equality`);
|
|
548
|
-
const currentState = changeTracker.copy_ || target;
|
|
549
|
-
debugLog(
|
|
550
|
-
`Comparing current state with original:`,
|
|
551
|
-
currentState,
|
|
552
|
-
changeTracker.originalObject
|
|
553
|
-
);
|
|
554
|
-
if (deepEqual(currentState, changeTracker.originalObject)) {
|
|
555
|
-
debugLog(`Current state equals original, returning empty object`);
|
|
556
|
-
changeTracker.modified = false;
|
|
557
|
-
return {};
|
|
558
|
-
}
|
|
559
|
-
debugLog(
|
|
560
|
-
`Comparing target with original:`,
|
|
561
|
-
target,
|
|
562
|
-
changeTracker.originalObject
|
|
563
|
-
);
|
|
564
|
-
if (deepEqual(target, changeTracker.originalObject)) {
|
|
565
|
-
debugLog(`Target equals original, returning empty object`);
|
|
566
|
-
changeTracker.modified = false;
|
|
567
|
-
changeTracker.changes = {};
|
|
568
|
-
changeTracker.assigned_ = {};
|
|
569
|
-
return {};
|
|
570
|
-
}
|
|
571
|
-
if (typeof target === `object` && target !== null) {
|
|
572
|
-
let allNestedReverted = true;
|
|
573
|
-
for (const key in target) {
|
|
574
|
-
if (Object.prototype.hasOwnProperty.call(target, key)) {
|
|
575
|
-
const currentValue = target[key];
|
|
576
|
-
const originalValue = changeTracker.originalObject[key];
|
|
577
|
-
if (!deepEqual(currentValue, originalValue)) {
|
|
578
|
-
allNestedReverted = false;
|
|
579
|
-
break;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
if (allNestedReverted) {
|
|
584
|
-
debugLog(
|
|
585
|
-
`All nested properties match original values, returning empty object`
|
|
586
|
-
);
|
|
587
|
-
changeTracker.modified = false;
|
|
588
|
-
changeTracker.changes = {};
|
|
589
|
-
changeTracker.assigned_ = {};
|
|
590
|
-
return {};
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
debugLog(
|
|
594
|
-
`Changes detected, returning full object:`,
|
|
595
|
-
changeTracker.copy_ || target
|
|
596
|
-
);
|
|
597
|
-
const result = changeTracker.copy_ || target;
|
|
598
|
-
return result;
|
|
599
|
-
}
|
|
600
|
-
debugLog(`No changes detected, returning empty object`);
|
|
601
|
-
return {};
|
|
465
|
+
debugLog(`Returning copy:`, result);
|
|
466
|
+
return result;
|
|
602
467
|
}
|
|
603
468
|
};
|
|
604
469
|
}
|
|
@@ -619,32 +484,6 @@ function withArrayChangeTracking(targets, callback) {
|
|
|
619
484
|
callback(proxies);
|
|
620
485
|
return getChanges();
|
|
621
486
|
}
|
|
622
|
-
function prepareCopy(state) {
|
|
623
|
-
if (!state.copy_) {
|
|
624
|
-
state.copy_ = shallowCopy(state.originalObject);
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
function shallowCopy(obj) {
|
|
628
|
-
if (Array.isArray(obj)) {
|
|
629
|
-
return [...obj];
|
|
630
|
-
}
|
|
631
|
-
if (obj instanceof Map) {
|
|
632
|
-
return new Map(obj);
|
|
633
|
-
}
|
|
634
|
-
if (obj instanceof Set) {
|
|
635
|
-
return new Set(obj);
|
|
636
|
-
}
|
|
637
|
-
if (obj instanceof Date) {
|
|
638
|
-
return new Date(obj.getTime());
|
|
639
|
-
}
|
|
640
|
-
if (obj instanceof RegExp) {
|
|
641
|
-
return new RegExp(obj.source, obj.flags);
|
|
642
|
-
}
|
|
643
|
-
if (obj !== null && typeof obj === `object`) {
|
|
644
|
-
return { ...obj };
|
|
645
|
-
}
|
|
646
|
-
return obj;
|
|
647
|
-
}
|
|
648
487
|
export {
|
|
649
488
|
createArrayChangeProxy,
|
|
650
489
|
createChangeProxy,
|