@tanstack/db 0.5.5 → 0.5.7

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/esm/proxy.js CHANGED
@@ -1,4 +1,259 @@
1
1
  import { isTemporal, deepEquals } from "./utils.js";
2
+ const CALLBACK_ITERATION_METHODS = /* @__PURE__ */ new Set([
3
+ `find`,
4
+ `findLast`,
5
+ `findIndex`,
6
+ `findLastIndex`,
7
+ `filter`,
8
+ `map`,
9
+ `flatMap`,
10
+ `forEach`,
11
+ `some`,
12
+ `every`,
13
+ `reduce`,
14
+ `reduceRight`
15
+ ]);
16
+ const ARRAY_MODIFYING_METHODS = /* @__PURE__ */ new Set([
17
+ `pop`,
18
+ `push`,
19
+ `shift`,
20
+ `unshift`,
21
+ `splice`,
22
+ `sort`,
23
+ `reverse`,
24
+ `fill`,
25
+ `copyWithin`
26
+ ]);
27
+ const MAP_SET_MODIFYING_METHODS = /* @__PURE__ */ new Set([`set`, `delete`, `clear`, `add`]);
28
+ const MAP_SET_ITERATOR_METHODS = /* @__PURE__ */ new Set([
29
+ `entries`,
30
+ `keys`,
31
+ `values`,
32
+ `forEach`
33
+ ]);
34
+ function isProxiableObject(value) {
35
+ return value !== null && typeof value === `object` && !(value instanceof Date) && !(value instanceof RegExp) && !isTemporal(value);
36
+ }
37
+ function createArrayIterationHandler(methodName, methodFn, changeTracker, memoizedCreateChangeProxy) {
38
+ if (!CALLBACK_ITERATION_METHODS.has(methodName)) {
39
+ return void 0;
40
+ }
41
+ return function(...args) {
42
+ const callback = args[0];
43
+ if (typeof callback !== `function`) {
44
+ return methodFn.apply(changeTracker.copy_, args);
45
+ }
46
+ const getProxiedElement = (element, index) => {
47
+ if (isProxiableObject(element)) {
48
+ const nestedParent = {
49
+ tracker: changeTracker,
50
+ prop: String(index)
51
+ };
52
+ const { proxy: elementProxy } = memoizedCreateChangeProxy(
53
+ element,
54
+ nestedParent
55
+ );
56
+ return elementProxy;
57
+ }
58
+ return element;
59
+ };
60
+ const wrappedCallback = function(element, index, array) {
61
+ const proxiedElement = getProxiedElement(element, index);
62
+ return callback.call(this, proxiedElement, index, array);
63
+ };
64
+ if (methodName === `reduce` || methodName === `reduceRight`) {
65
+ const reduceCallback = function(accumulator, element, index, array) {
66
+ const proxiedElement = getProxiedElement(element, index);
67
+ return callback.call(this, accumulator, proxiedElement, index, array);
68
+ };
69
+ return methodFn.apply(changeTracker.copy_, [
70
+ reduceCallback,
71
+ ...args.slice(1)
72
+ ]);
73
+ }
74
+ const result = methodFn.apply(changeTracker.copy_, [
75
+ wrappedCallback,
76
+ ...args.slice(1)
77
+ ]);
78
+ if ((methodName === `find` || methodName === `findLast`) && result && typeof result === `object`) {
79
+ const foundIndex = changeTracker.copy_.indexOf(result);
80
+ if (foundIndex !== -1) {
81
+ return getProxiedElement(result, foundIndex);
82
+ }
83
+ }
84
+ if (methodName === `filter` && Array.isArray(result)) {
85
+ return result.map((element) => {
86
+ const originalIndex = changeTracker.copy_.indexOf(element);
87
+ if (originalIndex !== -1) {
88
+ return getProxiedElement(element, originalIndex);
89
+ }
90
+ return element;
91
+ });
92
+ }
93
+ return result;
94
+ };
95
+ }
96
+ function createArrayIteratorHandler(changeTracker, memoizedCreateChangeProxy) {
97
+ return function() {
98
+ const array = changeTracker.copy_;
99
+ let index = 0;
100
+ return {
101
+ next() {
102
+ if (index >= array.length) {
103
+ return { done: true, value: void 0 };
104
+ }
105
+ const element = array[index];
106
+ let proxiedElement = element;
107
+ if (isProxiableObject(element)) {
108
+ const nestedParent = {
109
+ tracker: changeTracker,
110
+ prop: String(index)
111
+ };
112
+ const { proxy: elementProxy } = memoizedCreateChangeProxy(
113
+ element,
114
+ nestedParent
115
+ );
116
+ proxiedElement = elementProxy;
117
+ }
118
+ index++;
119
+ return { done: false, value: proxiedElement };
120
+ },
121
+ [Symbol.iterator]() {
122
+ return this;
123
+ }
124
+ };
125
+ };
126
+ }
127
+ function createModifyingMethodHandler(methodFn, changeTracker, markChanged) {
128
+ return function(...args) {
129
+ const result = methodFn.apply(changeTracker.copy_, args);
130
+ markChanged(changeTracker);
131
+ return result;
132
+ };
133
+ }
134
+ function createMapSetIteratorHandler(methodName, prop, methodFn, target, changeTracker, memoizedCreateChangeProxy, markChanged) {
135
+ const isIteratorMethod = MAP_SET_ITERATOR_METHODS.has(methodName) || prop === Symbol.iterator;
136
+ if (!isIteratorMethod) {
137
+ return void 0;
138
+ }
139
+ return function(...args) {
140
+ const result = methodFn.apply(changeTracker.copy_, args);
141
+ if (methodName === `forEach`) {
142
+ const callback = args[0];
143
+ if (typeof callback === `function`) {
144
+ const wrappedCallback = function(value, key, collection) {
145
+ const cbresult = callback.call(this, value, key, collection);
146
+ markChanged(changeTracker);
147
+ return cbresult;
148
+ };
149
+ return methodFn.apply(target, [wrappedCallback, ...args.slice(1)]);
150
+ }
151
+ }
152
+ const isValueIterator = methodName === `entries` || methodName === `values` || methodName === Symbol.iterator.toString() || prop === Symbol.iterator;
153
+ if (isValueIterator) {
154
+ const originalIterator = result;
155
+ const valueToKeyMap = /* @__PURE__ */ new Map();
156
+ if (methodName === `values` && target instanceof Map) {
157
+ for (const [key, mapValue] of changeTracker.copy_.entries()) {
158
+ valueToKeyMap.set(mapValue, key);
159
+ }
160
+ }
161
+ const originalToModifiedMap = /* @__PURE__ */ new Map();
162
+ if (target instanceof Set) {
163
+ for (const setValue of changeTracker.copy_.values()) {
164
+ originalToModifiedMap.set(setValue, setValue);
165
+ }
166
+ }
167
+ return {
168
+ next() {
169
+ const nextResult = originalIterator.next();
170
+ if (!nextResult.done && nextResult.value && typeof nextResult.value === `object`) {
171
+ if (methodName === `entries` && Array.isArray(nextResult.value) && nextResult.value.length === 2) {
172
+ if (nextResult.value[1] && typeof nextResult.value[1] === `object`) {
173
+ const mapKey = nextResult.value[0];
174
+ const mapParent = {
175
+ tracker: changeTracker,
176
+ prop: mapKey,
177
+ updateMap: (newValue) => {
178
+ if (changeTracker.copy_ instanceof Map) {
179
+ changeTracker.copy_.set(
180
+ mapKey,
181
+ newValue
182
+ );
183
+ }
184
+ }
185
+ };
186
+ const { proxy: valueProxy } = memoizedCreateChangeProxy(
187
+ nextResult.value[1],
188
+ mapParent
189
+ );
190
+ nextResult.value[1] = valueProxy;
191
+ }
192
+ } else if (methodName === `values` || methodName === Symbol.iterator.toString() || prop === Symbol.iterator) {
193
+ if (methodName === `values` && target instanceof Map) {
194
+ const mapKey = valueToKeyMap.get(nextResult.value);
195
+ if (mapKey !== void 0) {
196
+ const mapParent = {
197
+ tracker: changeTracker,
198
+ prop: mapKey,
199
+ updateMap: (newValue) => {
200
+ if (changeTracker.copy_ instanceof Map) {
201
+ changeTracker.copy_.set(
202
+ mapKey,
203
+ newValue
204
+ );
205
+ }
206
+ }
207
+ };
208
+ const { proxy: valueProxy } = memoizedCreateChangeProxy(
209
+ nextResult.value,
210
+ mapParent
211
+ );
212
+ nextResult.value = valueProxy;
213
+ }
214
+ } else if (target instanceof Set) {
215
+ const setOriginalValue = nextResult.value;
216
+ const setParent = {
217
+ tracker: changeTracker,
218
+ prop: setOriginalValue,
219
+ updateSet: (newValue) => {
220
+ if (changeTracker.copy_ instanceof Set) {
221
+ changeTracker.copy_.delete(
222
+ setOriginalValue
223
+ );
224
+ changeTracker.copy_.add(newValue);
225
+ originalToModifiedMap.set(setOriginalValue, newValue);
226
+ }
227
+ }
228
+ };
229
+ const { proxy: valueProxy } = memoizedCreateChangeProxy(
230
+ nextResult.value,
231
+ setParent
232
+ );
233
+ nextResult.value = valueProxy;
234
+ } else {
235
+ const tempKey = Symbol(`iterator-value`);
236
+ const { proxy: valueProxy } = memoizedCreateChangeProxy(
237
+ nextResult.value,
238
+ {
239
+ tracker: changeTracker,
240
+ prop: tempKey
241
+ }
242
+ );
243
+ nextResult.value = valueProxy;
244
+ }
245
+ }
246
+ }
247
+ return nextResult;
248
+ },
249
+ [Symbol.iterator]() {
250
+ return this;
251
+ }
252
+ };
253
+ }
254
+ return result;
255
+ };
256
+ }
2
257
  function debugLog(...args) {
3
258
  const isBrowser = typeof window !== `undefined` && typeof localStorage !== `undefined`;
4
259
  if (isBrowser && localStorage.getItem(`DEBUG`) === `true`) {
@@ -211,182 +466,54 @@ function createChangeProxy(target, parent) {
211
466
  if (typeof value === `function`) {
212
467
  if (Array.isArray(ptarget)) {
213
468
  const methodName = prop.toString();
214
- const modifyingMethods = /* @__PURE__ */ new Set([
215
- `pop`,
216
- `push`,
217
- `shift`,
218
- `unshift`,
219
- `splice`,
220
- `sort`,
221
- `reverse`,
222
- `fill`,
223
- `copyWithin`
224
- ]);
225
- if (modifyingMethods.has(methodName)) {
226
- return function(...args) {
227
- const result = value.apply(changeTracker.copy_, args);
228
- markChanged(changeTracker);
229
- return result;
230
- };
469
+ if (ARRAY_MODIFYING_METHODS.has(methodName)) {
470
+ return createModifyingMethodHandler(
471
+ value,
472
+ changeTracker,
473
+ markChanged
474
+ );
475
+ }
476
+ const iterationHandler = createArrayIterationHandler(
477
+ methodName,
478
+ value,
479
+ changeTracker,
480
+ memoizedCreateChangeProxy
481
+ );
482
+ if (iterationHandler) {
483
+ return iterationHandler;
484
+ }
485
+ if (prop === Symbol.iterator) {
486
+ return createArrayIteratorHandler(
487
+ changeTracker,
488
+ memoizedCreateChangeProxy
489
+ );
231
490
  }
232
491
  }
233
492
  if (ptarget instanceof Map || ptarget instanceof Set) {
234
493
  const methodName = prop.toString();
235
- const modifyingMethods = /* @__PURE__ */ new Set([
236
- `set`,
237
- `delete`,
238
- `clear`,
239
- `add`,
240
- `pop`,
241
- `push`,
242
- `shift`,
243
- `unshift`,
244
- `splice`,
245
- `sort`,
246
- `reverse`
247
- ]);
248
- if (modifyingMethods.has(methodName)) {
249
- return function(...args) {
250
- const result = value.apply(changeTracker.copy_, args);
251
- markChanged(changeTracker);
252
- return result;
253
- };
494
+ if (MAP_SET_MODIFYING_METHODS.has(methodName)) {
495
+ return createModifyingMethodHandler(
496
+ value,
497
+ changeTracker,
498
+ markChanged
499
+ );
254
500
  }
255
- const iteratorMethods = /* @__PURE__ */ new Set([
256
- `entries`,
257
- `keys`,
258
- `values`,
259
- `forEach`,
260
- Symbol.iterator
261
- ]);
262
- if (iteratorMethods.has(methodName) || prop === Symbol.iterator) {
263
- return function(...args) {
264
- const result = value.apply(changeTracker.copy_, args);
265
- if (methodName === `forEach`) {
266
- const callback = args[0];
267
- if (typeof callback === `function`) {
268
- const wrappedCallback = function(value2, key, collection) {
269
- const cbresult = callback.call(
270
- this,
271
- value2,
272
- key,
273
- collection
274
- );
275
- markChanged(changeTracker);
276
- return cbresult;
277
- };
278
- return value.apply(ptarget, [
279
- wrappedCallback,
280
- ...args.slice(1)
281
- ]);
282
- }
283
- }
284
- if (methodName === `entries` || methodName === `values` || methodName === Symbol.iterator.toString() || prop === Symbol.iterator) {
285
- const originalIterator = result;
286
- const valueToKeyMap = /* @__PURE__ */ new Map();
287
- if (methodName === `values` && ptarget instanceof Map) {
288
- for (const [
289
- key,
290
- mapValue
291
- ] of changeTracker.copy_.entries()) {
292
- valueToKeyMap.set(mapValue, key);
293
- }
294
- }
295
- const originalToModifiedMap = /* @__PURE__ */ new Map();
296
- if (ptarget instanceof Set) {
297
- for (const setValue of changeTracker.copy_.values()) {
298
- originalToModifiedMap.set(setValue, setValue);
299
- }
300
- }
301
- return {
302
- next() {
303
- const nextResult = originalIterator.next();
304
- if (!nextResult.done && nextResult.value && typeof nextResult.value === `object`) {
305
- if (methodName === `entries` && Array.isArray(nextResult.value) && nextResult.value.length === 2) {
306
- if (nextResult.value[1] && typeof nextResult.value[1] === `object`) {
307
- const mapKey = nextResult.value[0];
308
- const mapParent = {
309
- tracker: changeTracker,
310
- prop: mapKey,
311
- updateMap: (newValue) => {
312
- if (changeTracker.copy_ instanceof Map) {
313
- changeTracker.copy_.set(mapKey, newValue);
314
- }
315
- }
316
- };
317
- const { proxy: valueProxy } = memoizedCreateChangeProxy(
318
- nextResult.value[1],
319
- mapParent
320
- );
321
- nextResult.value[1] = valueProxy;
322
- }
323
- } else if (methodName === `values` || methodName === Symbol.iterator.toString() || prop === Symbol.iterator) {
324
- if (typeof nextResult.value === `object` && nextResult.value !== null) {
325
- if (methodName === `values` && ptarget instanceof Map) {
326
- const mapKey = valueToKeyMap.get(nextResult.value);
327
- if (mapKey !== void 0) {
328
- const mapParent = {
329
- tracker: changeTracker,
330
- prop: mapKey,
331
- updateMap: (newValue) => {
332
- if (changeTracker.copy_ instanceof Map) {
333
- changeTracker.copy_.set(mapKey, newValue);
334
- }
335
- }
336
- };
337
- const { proxy: valueProxy } = memoizedCreateChangeProxy(
338
- nextResult.value,
339
- mapParent
340
- );
341
- nextResult.value = valueProxy;
342
- }
343
- } else if (ptarget instanceof Set) {
344
- const setOriginalValue = nextResult.value;
345
- const setParent = {
346
- tracker: changeTracker,
347
- prop: setOriginalValue,
348
- // Use the original value as the prop
349
- updateSet: (newValue) => {
350
- if (changeTracker.copy_ instanceof Set) {
351
- changeTracker.copy_.delete(setOriginalValue);
352
- changeTracker.copy_.add(newValue);
353
- originalToModifiedMap.set(
354
- setOriginalValue,
355
- newValue
356
- );
357
- }
358
- }
359
- };
360
- const { proxy: valueProxy } = memoizedCreateChangeProxy(
361
- nextResult.value,
362
- setParent
363
- );
364
- nextResult.value = valueProxy;
365
- } else {
366
- const tempKey = Symbol(`iterator-value`);
367
- const { proxy: valueProxy } = memoizedCreateChangeProxy(nextResult.value, {
368
- tracker: changeTracker,
369
- prop: tempKey
370
- });
371
- nextResult.value = valueProxy;
372
- }
373
- }
374
- }
375
- }
376
- return nextResult;
377
- },
378
- [Symbol.iterator]() {
379
- return this;
380
- }
381
- };
382
- }
383
- return result;
384
- };
501
+ const iteratorHandler = createMapSetIteratorHandler(
502
+ methodName,
503
+ prop,
504
+ value,
505
+ ptarget,
506
+ changeTracker,
507
+ memoizedCreateChangeProxy,
508
+ markChanged
509
+ );
510
+ if (iteratorHandler) {
511
+ return iteratorHandler;
385
512
  }
386
513
  }
387
514
  return value.bind(ptarget);
388
515
  }
389
- if (value && typeof value === `object` && !(value instanceof Date) && !(value instanceof RegExp) && !isTemporal(value)) {
516
+ if (isProxiableObject(value)) {
390
517
  const nestedParent = {
391
518
  tracker: changeTracker,
392
519
  prop: String(prop)