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

Function createChangeProxy

packages/db/src/proxy.ts:628–1122  ·  view source on GitHub ↗
(
  target: T,
  parent?: {
    tracker: ChangeTracker<Record<string | symbol, unknown>>
    prop: string | symbol
  },
)

Source from the content-addressed store, hash-verified

626 * @returns An object containing the proxy and a function to get the changes
627 */
628export function createChangeProxy<
629 T extends Record<string | symbol, any | undefined>,
630>(
631 target: T,
632 parent?: {
633 tracker: ChangeTracker<Record<string | symbol, unknown>>
634 prop: string | symbol
635 },
636): {
637 proxy: T
638
639 getChanges: () => Record<string | symbol, any>
640} {
641 const changeProxyCache = new Map<object, object>()
642
643 function memoizedCreateChangeProxy<
644 TInner extends Record<string | symbol, any | undefined>,
645 >(
646 innerTarget: TInner,
647 innerParent?: {
648 tracker: ChangeTracker<Record<string | symbol, unknown>>
649 prop: string | symbol
650 },
651 ): {
652 proxy: TInner
653 getChanges: () => Record<string | symbol, any>
654 } {
655 debugLog(`Object ID:`, innerTarget.constructor.name)
656 if (changeProxyCache.has(innerTarget)) {
657 return changeProxyCache.get(innerTarget) as {
658 proxy: TInner
659 getChanges: () => Record<string | symbol, any>
660 }
661 } else {
662 const changeProxy = createChangeProxy(innerTarget, innerParent)
663 changeProxyCache.set(innerTarget, changeProxy)
664 return changeProxy
665 }
666 }
667 // Create a WeakMap to cache proxies for nested objects
668 // This prevents creating multiple proxies for the same object
669 // and handles circular references
670 const proxyCache = new Map<object, object>()
671
672 // Create a change tracker to track changes to the object
673 const changeTracker: ChangeTracker<T> = {
674 copy_: deepClone(target),
675 originalObject: deepClone(target),
676 proxyCount: getProxyCount(),
677 modified: false,
678 assigned_: {},
679 parent,
680 target, // Store reference to the target object
681 }
682
683 debugLog(
684 `createChangeProxy called for target`,
685 target,

Callers 4

proxy.test.tsFile · 0.90
createArrayChangeProxyFunction · 0.85
withChangeTrackingFunction · 0.85

Calls 5

deepCloneFunction · 0.85
getProxyCountFunction · 0.85
debugLogFunction · 0.85
createObjectProxyFunction · 0.85
keysMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…