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