Path: blob/main/src/core/debug/mutation-detector-proxy.ts
3583 views
/*1* mutation-detector-proxy2*3* Copyright (C) 2025 Posit Software, PBC4*/56export type DeepProxyChange = {7type: "update" | "delete";8path: (string | symbol)[];9oldValue: unknown;10newValue?: unknown;11};1213export type OnChangeCallback = (change: DeepProxyChange) => void;1415export function mutationDetectorProxy<T extends object>(16obj: T,17onChange: OnChangeCallback,18path: (string | symbol)[] = [],19): T {20return new Proxy(obj, {21get(target: T, property: string | symbol): any {22const value = Reflect.get(target, property);23if (value && typeof value === "object") {24return mutationDetectorProxy(25value,26onChange,27[...path, property],28);29}30return value;31},3233set(target: T, property: string | symbol, value: any): boolean {34const oldValue = Reflect.get(target, property);35const result = Reflect.set(target, property, value);3637if (result) {38onChange({39type: "update",40path: [...path, property],41oldValue,42newValue: value,43});44}4546return result;47},4849deleteProperty(target: T, property: string | symbol): boolean {50const oldValue = Reflect.get(target, property);51const result = Reflect.deleteProperty(target, property);5253if (result) {54onChange({55type: "delete",56path: [...path, property],57oldValue,58});59}6061return result;62},63});64}656667