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