MCPcopy Index your code
hub / github.com/TanStack/db / createArrayIterationHandler

Function createArrayIterationHandler

packages/db/src/proxy.ts:76–179  ·  view source on GitHub ↗

* Creates a handler for array iteration methods that ensures proxied elements * are passed to callbacks and returned from methods like find/filter.

(
  methodName: string,
  methodFn: (...args: Array<unknown>) => unknown,
  changeTracker: ChangeTracker<T>,
  memoizedCreateChangeProxy: (
    obj: Record<string | symbol, unknown>,
    parent?: {
      tracker: ChangeTracker<Record<string | symbol, unknown>>
      prop: string | symbol
    },
  ) => { proxy: Record<string | symbol, unknown> },
)

Source from the content-addressed store, hash-verified

74 * are passed to callbacks and returned from methods like find/filter.
75 */
76function createArrayIterationHandler<T extends object>(
77 methodName: string,
78 methodFn: (...args: Array<unknown>) => unknown,
79 changeTracker: ChangeTracker<T>,
80 memoizedCreateChangeProxy: (
81 obj: Record<string | symbol, unknown>,
82 parent?: {
83 tracker: ChangeTracker<Record<string | symbol, unknown>>
84 prop: string | symbol
85 },
86 ) => { proxy: Record<string | symbol, unknown> },
87): ((...args: Array<unknown>) => unknown) | undefined {
88 if (!CALLBACK_ITERATION_METHODS.has(methodName)) {
89 return undefined
90 }
91
92 return function (...args: Array<unknown>) {
93 const callback = args[0]
94 if (typeof callback !== `function`) {
95 return methodFn.apply(changeTracker.copy_, args)
96 }
97
98 // Create a helper to get proxied version of an array element
99 const getProxiedElement = (element: unknown, index: number): unknown => {
100 if (isProxiableObject(element)) {
101 const nestedParent = {
102 tracker: changeTracker as unknown as ChangeTracker<
103 Record<string | symbol, unknown>
104 >,
105 prop: String(index),
106 }
107 const { proxy: elementProxy } = memoizedCreateChangeProxy(
108 element,
109 nestedParent,
110 )
111 return elementProxy
112 }
113 return element
114 }
115
116 // Wrap the callback to pass proxied elements
117 const wrappedCallback = function (
118 this: unknown,
119 element: unknown,
120 index: number,
121 array: unknown,
122 ) {
123 const proxiedElement = getProxiedElement(element, index)
124 return callback.call(this, proxiedElement, index, array)
125 }
126
127 // For reduce/reduceRight, the callback signature is different
128 if (methodName === `reduce` || methodName === `reduceRight`) {
129 const reduceCallback = function (
130 this: unknown,
131 accumulator: unknown,
132 element: unknown,
133 index: number,

Callers 1

getFunction · 0.85

Calls 4

getProxiedElementFunction · 0.85
hasMethod · 0.45
indexOfMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…