{"version":3,"file":"index.umd.cjs","sources":["../src/utils/isStructure.ts","../src/core/metadata.ts","../src/core/traces.ts","../src/utils/isTraced.ts","../src/utils/iterableWeakMap.ts","../src/core/mutationCallback.ts","../src/core/subscribers.ts","../src/types/mutation.ts","../src/utils/disableTracing.ts","../src/utils/withoutAssigning.ts","../src/utils/withoutTracing.ts","../src/applyTrace.ts","../src/deepClone.ts","../src/core/proxy/reflect.ts","../src/core/proxy/getTracedArray.ts","../src/core/proxy/getTracedObject.ts","../src/core/proxy/deepTrace.ts","../src/traceFabric.ts"],"sourcesContent":["import type { JSONObject } from '../types/json';\n\n/**\n * Check if the value is a typeof `object` and not `null`.\n *\n * @param value - value that needs to be checked.\n * @returns `boolean`\n *\n * @example\n * ```typescript\n * console.log(isStructure({ hello: 'world' })); // true\n * console.log(isStructure(['hello', 'world'])); // true\n *\n * console.log(isStructure(1)); // false\n * console.log(isStructure(true)); // false\n * console.log(isStructure('hello')); // false\n * console.log(isStructure(null)); // false\n * console.log(isStructure(undefined)); // false\n * console.log(isStructure(true)); // false\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/is-value-a-structure/isStructure.html Wiki page.}\n *\n * @since 0.2.0\n */\nexport function isStructure<T>(\n  value: T,\n): value is T extends JSONObject ? T : T & JSONObject {\n  return typeof value === 'object' && value !== null;\n}\n","import type { JSONStructure } from '../types/json';\nimport type { TTarget } from '../types/mutation';\nimport { isTracedFabric } from '../utils/isTraced';\n\nexport type TTracedValueMetadata = {\n  parentRef: JSONStructure;\n  key: TTarget;\n};\n\nexport type TWeakTracedValueMetadata = {\n  parentRef: WeakRef<JSONStructure>;\n  key: TTarget;\n};\n\nexport const tracedValuesMetadata = new WeakMap<JSONStructure, TWeakTracedValueMetadata>();\n\n/**\n * Will return the metadata of the target if it exists,\n * no matter if rootRef and parentRef are garbage collected.\n */\nexport function getMetadata(target: JSONStructure): TWeakTracedValueMetadata | undefined {\n  return tracedValuesMetadata.get(target);\n}\n\n/**\n * Will return the metadata of the target if it exists,\n * and if rootRef and parentRef are NOT garbage collected.\n */\nexport function getStrongMetadata(target: JSONStructure): TTracedValueMetadata | undefined {\n  const metadata = getMetadata(target);\n  if (!metadata) return;\n\n  const parentRef = metadata.parentRef.deref();\n  if (!parentRef) return;\n\n  return { parentRef, key: metadata.key };\n}\n\nexport function setMetadata(target: JSONStructure, metadata: Required<TTracedValueMetadata>): void {\n  tracedValuesMetadata.set(target, {\n    parentRef: new WeakRef(metadata.parentRef),\n    key: metadata.key,\n  });\n}\n\nexport function getRootRef(target: JSONStructure): JSONStructure | undefined {\n  if (isTracedFabric(target)) return target;\n\n  const metadata = getStrongMetadata(target);\n  if (!metadata) return undefined;\n\n  return getRootRef(metadata.parentRef);\n}\n\nexport function getTargetChain(target: JSONStructure): TTarget[] {\n  const metadata = getStrongMetadata(target);\n  const parentTargetChain = metadata?.parentRef ? getTargetChain(metadata.parentRef) : [];\n\n  return metadata ? [...parentTargetChain, metadata.key] : [];\n}\n","import type { JSONStructure } from '../types/json';\n\nexport const tracedFabricsTrace = new WeakMap<JSONStructure, any[]>();\n","import { tracedValuesMetadata } from '../core/metadata';\nimport { tracedFabricsTrace } from '../core/traces';\n\n/**\n * Checks if the given value is a `tracedFabric`.\n *\n * @example\n * ```typescript\n * const traced = traceFabric({ // --> tracedFabric value AND traced\n *   innerArray: [1, 2, 3], // --> tracedValue AND traced\n * });\n *\n * isTracedFabric(traced.value); // true\n * isTracedFabric(traced.value.innerArray); // false\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/check-if-value-traced/isTracedFabric.html Wiki page.}\n *\n * @since 0.2.0\n */\nexport function isTracedFabric(value: any): boolean {\n  return tracedFabricsTrace.has(value);\n}\n\n/**\n * Checks if the given value is a `tracedValue`.\n *\n * @example\n * ```typescript\n * const traced = traceFabric({ // --> tracedFabric value AND traced\n *   innerArray: [1, 2, 3], // --> tracedValue AND traced\n * });\n *\n * isTracedValue(traced.value); // false\n * isTracedValue(traced.value.innerArray); // true\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/check-if-value-traced/isTracedValue.html Wiki page.}\n *\n * @since 0.2.0\n */\nexport function isTracedValue(value: any): boolean {\n  return tracedValuesMetadata.has(value);\n}\n\n/**\n * Checks if the given value is a `traced`.\n *\n * @example\n * ```typescript\n * const traced = traceFabric({ // --> tracedFabric value AND traced\n *   innerArray: [1, 2, 3], // --> tracedValue AND traced\n * });\n *\n * isTraced(traced.value); // true\n * isTraced(traced.value.innerArray); // true\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/check-if-value-traced/isTraced.html Wiki page.}\n *\n * @since 0.2.0\n */\nexport function isTraced(value: any): boolean {\n  return isTracedValue(value) || isTracedFabric(value);\n}\n","/**\n * @source [tc39 / proposal weakrefs](https://github.com/tc39/proposal-weakrefs?tab=readme-ov-file#iterable-weakmaps)\n */\nexport class IterableWeakMap<\n  _KEY extends object,\n  _VALUE extends object,\n> {\n  #weakKeySet = new Set<WeakRef<_KEY>>();\n  #weakMap = new WeakMap<\n    _KEY,\n    { value: _VALUE; weakKey: WeakRef<_KEY> }\n  >();\n\n  #cleanupRegistry = new FinalizationRegistry((weakRef: WeakRef<_KEY>) => this.#weakKeySet.delete(weakRef));\n\n  set(key: _KEY, value: _VALUE): this {\n    const entry = this.#weakMap.get(key);\n\n    if (entry) {\n      entry.value = value;\n      return this;\n    }\n\n    const weakKey = new WeakRef(key);\n\n    this.#weakMap.set(key, { value, weakKey });\n    this.#weakKeySet.add(weakKey);\n    this.#cleanupRegistry.register(key, weakKey, weakKey);\n\n    return this;\n  }\n\n  get(key: _KEY): _VALUE | undefined {\n    return this.#weakMap.get(key)?.value;\n  }\n\n  has(key: _KEY): boolean {\n    return this.#weakMap.has(key);\n  }\n\n  delete(key: _KEY): boolean {\n    const entry = this.#weakMap.get(key);\n    if (!entry) return false;\n\n    this.#weakMap.delete(key);\n    this.#weakKeySet.delete(entry.weakKey);\n    this.#cleanupRegistry.unregister(entry.weakKey);\n\n    return true;\n  }\n\n  *[Symbol.iterator](): IterableIterator<[key: _KEY, value: _VALUE]> {\n    for (const ref of this.#weakKeySet) {\n      const key = ref.deref();\n      if (!key) continue;\n\n      const mapValue = this.#weakMap.get(key);\n      if (!mapValue) continue;\n\n      yield [key, mapValue.value];\n    }\n  }\n\n  entries(): IterableIterator<[key: _KEY, value: _VALUE]> {\n    return this[Symbol.iterator]();\n  }\n}\n","import type { JSONStructure } from '../types/json';\nimport type { TMutationCallback } from '../types/mutation';\n\nexport const mutationCallbacks = new WeakMap<JSONStructure, TMutationCallback>();\n","import type { JSONStructure, JSONValue } from '../types/json';\nimport type { TMutation } from '../types/mutation';\nimport { isStructure } from '../utils/isStructure';\nimport { isTracedFabric } from '../utils/isTraced';\nimport { IterableWeakMap } from '../utils/iterableWeakMap';\nimport { getRootRef, getStrongMetadata, getTargetChain, type TTracedValueMetadata, type TWeakTracedValueMetadata } from './metadata';\nimport { mutationCallbacks } from './mutationCallback';\nimport { tracedFabricsTrace } from './traces';\n\nconst tracedFabricSubscribers = new WeakMap<\n  JSONStructure, // the sender of the updates (if update happen, this value will send the updates to receivers)\n  IterableWeakMap<\n    JSONStructure, // receiver of the updates\n    TWeakTracedValueMetadata[] // set of childMetadata (the path to the sender in receiver's value)\n  >\n>();\n\nexport function addTracedSubscriber(\n  changesSender: JSONStructure,\n  metadata: TTracedValueMetadata,\n): void {\n  const subscribers\n    = tracedFabricSubscribers.get(changesSender)\n    ?? tracedFabricSubscribers.set(changesSender, new IterableWeakMap()).get(changesSender)!;\n\n  const rootRef = getRootRef(metadata.parentRef);\n  if (!rootRef) return;\n\n  const receiver = subscribers.get(rootRef) ?? subscribers.set(rootRef, []).get(rootRef)!;\n\n  receiver.push({ parentRef: new WeakRef(metadata.parentRef), key: metadata.key });\n}\n\n/**\n * Will unsubscribe the `receiver` from the `sender`.\n * So after mutating the `sender`, the `receiver` will not receive any updates.\n *\n * Needs to be used to speed up garbage collection.\n *\n * @param changesSender the `sender` of the updates (value that is a part of the receiver).\n * @param changesReceiver `receiver` of the updates.\n *\n * @example\n * ```typescript\n * const globalMessages = traceFabric(['Welcome!']);\n *\n * function userLifecycle(): void {\n *   const user = traceFabric({\n *     globalMessages: globalMessages.value,\n *   });\n *\n *   // The `globalMessages` subscribes to the `user` value to send updates.\n *   // To speed up the gc, remove the subscription, at the end of the `user` lifecycle.\n *   removeTraceSubscription(globalMessages.value, user.value);\n * }\n * ```\n *\n * @since 0.4.0\n */\nexport function removeTraceSubscription(\n  changesSender: JSONStructure,\n  changesReceiver: JSONStructure,\n): void {\n  const subscribers = tracedFabricSubscribers.get(changesSender);\n  if (!subscribers) return;\n\n  subscribers.delete(changesReceiver);\n}\n\nexport function removeTracedSubscriber(\n  changesSender: JSONStructure,\n  metadata: TTracedValueMetadata,\n): void {\n  const subscribers = tracedFabricSubscribers.get(changesSender);\n  if (!subscribers) return;\n\n  const rootRef = getRootRef(metadata.parentRef);\n  if (!rootRef) return;\n\n  const receiver = subscribers.get(rootRef);\n  if (!receiver) return;\n\n  const index = receiver.findIndex(m => m.key === metadata.key && m.parentRef.deref() === metadata.parentRef);\n\n  if (index !== -1) receiver.splice(index, 1);\n}\n\nexport function removeNestedTracedSubscribers(\n  changesSender: JSONStructure,\n  metadata: TTracedValueMetadata,\n): void {\n  const target = (metadata.parentRef as any)[metadata.key] as JSONValue | undefined;\n  if (!isStructure(target)) return;\n\n  if (isTracedFabric(target)) return removeTracedSubscriber(changesSender, metadata);\n\n  for (const key in target) {\n    const maybeIndex = Number.isNaN(+key) ? key : +key;\n    const child = (target as any)[maybeIndex] as JSONValue;\n\n    if (!isStructure(child)) continue;\n\n    const childMetadata = getStrongMetadata(child);\n\n    if (childMetadata) removeNestedTracedSubscribers(target, childMetadata);\n    else if (isTracedFabric(child)) removeTracedSubscriber(child, { parentRef: target, key: maybeIndex });\n  }\n}\n\nexport function updateSubscribers(\n  changesSender: JSONStructure,\n  mutation: TMutation,\n): void {\n  const subscribers = tracedFabricSubscribers.get(changesSender);\n  if (!subscribers) return;\n\n  for (const [receiver, metadata] of subscribers.entries()) {\n    const trace = tracedFabricsTrace.get(receiver);\n    const receiverMutationCallback = mutationCallbacks.get(receiver);\n\n    if (!trace || !receiverMutationCallback) continue;\n\n    for (const trace of metadata) {\n      const parentRef = trace.parentRef.deref();\n\n      const targetChain = (parentRef ? getTargetChain(parentRef) : [])\n        .concat(trace.key)\n        .concat(mutation.targetChain);\n\n      const traceLog = { ...mutation, targetChain };\n\n      receiverMutationCallback(traceLog);\n    }\n  }\n}\n","import type { JSONValue } from './json';\n\nexport enum EMutated {\n  object = 'object',\n  array = 'array',\n}\n\nexport type TTarget = string | number;\n\nexport enum EObjectMutation {\n  set = 'set',\n  delete = 'delete',\n}\n\nexport type TObjectMutation = {\n  mutated: EMutated.object;\n  targetChain: TTarget[];\n} & ({\n  type: EObjectMutation.set;\n  value: JSONValue;\n} | {\n  type: EObjectMutation.delete;\n  value?: never;\n});\n\nexport enum EArrayMutation {\n  set = 'set',\n  delete = 'delete',\n\n  push = 'push',\n  reverse = 'reverse',\n  shift = 'shift',\n  unshift = 'unshift',\n}\n\nexport type TArrayMutation = {\n  mutated: EMutated.array;\n  targetChain: TTarget[];\n} & ({\n  type: EArrayMutation.set;\n  value: JSONValue;\n} | {\n  type:\n    EArrayMutation.push |\n    EArrayMutation.unshift;\n  value: JSONValue[];\n} | {\n  type:\n    EArrayMutation.delete |\n    EArrayMutation.reverse |\n    EArrayMutation.shift;\n  value?: never;\n});\n\nexport type TMutation = TObjectMutation | TArrayMutation;\n\nexport type TMutationCallback = (mutation: TMutation) => void;\n","import type { JSONStructure } from '../types/json';\n\nconst disabledTracingSymbol = Symbol('disabledTracing');\n\n/**\n * Check if the given value has `tracing` enabled.\n *\n * @param value - the value to check.\n * @returns `true` if the value is enabled for tracing, `false` otherwise.\n *\n * @example\n * ```typescript\n * const fabric = traceFabric({\n *   tracedArray: [1, 2, 3],\n *   untracedArray: disableTracing([4, 5, 6]),\n * });\n *\n * console.log(isTracingEnabled(fabric.value.tracedArray)); // true;\n * console.log(isTracingEnabled(fabric.value.untracedArray)); // false;\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/disable-tracing/isTracingEnabled.html Wiki page.}\n *\n * @since 0.10.0\n */\nexport function isTracingEnabled<T extends JSONStructure>(value: T): boolean {\n  return !(disabledTracingSymbol in value);\n}\n\n/**\n * Disable `tracing` for the given value.\n *\n * Primarily used for static values that should not be traced or modified,\n * also to reduce memory usage and improve performance.\n * The value will be modifiable, but the `mutations` will not be recorded.\n *\n * @param value - the value to disable tracing.\n * @returns same as the given **value** with tracing disabled.\n *\n * @example\n * ```typescript\n * const fabric = traceFabric({\n *   tracedArray: [1, 2, 3],\n *   untracedArray: disableTracing([4, 5, 6]),\n * });\n *\n * console.log(isTracingEnabled(fabric.value.tracedArray)); // true;\n * console.log(isTracingEnabled(fabric.value.untracedArray)); // false;\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/disable-tracing/disableTracing.html Wiki page.}\n *\n * @since 0.10.0\n */\nexport function disableTracing<T extends JSONStructure>(value: T): T {\n  (value as any)[disabledTracingSymbol] = true;\n  return value as ReturnType<typeof disableTracing<T>>;\n}\n\n/**\n * Enable `tracing` for the given value.\n *\n * [!NOTE] This function will not enable `mutations` recording for the value that is\n * already part of the `tracedFabric`, because it was not processed to be a `tracedValue`.\n * To do so, please use this approach:\n * ```typescript\n * withoutTracing(() => {\n *   fabric.value.untracedArray = enableTracing(fabric.value.untracedArray);\n * });\n * ```\n *\n * @param value - the value to enable tracing.\n * @returns same as the given **value** with tracing enabled.\n *\n * @example\n * ```typescript\n * const fabric = traceFabric({\n *   tracedArray: [1, 2, 3],\n *   untracedArray: disableTracing([4, 5, 6]),\n * });\n *\n * console.log(isTracingEnabled(fabric.value.tracedArray)); // true;\n * console.log(isTracingEnabled(fabric.value.untracedArray)); // false;\n *\n * withoutTracing(() => {\n *   fabric.value.untracedArray = enableTracing(fabric.value.untracedArray);\n * });\n *\n * console.log(isTracingEnabled(fabric.value.untracedArray)); // true;\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/disable-tracing/enableTracing.html Wiki page.}\n *\n * @since 0.10.0\n */\nexport function enableTracing<T extends JSONStructure>(value: T): T {\n  delete (value as any)[disabledTracingSymbol];\n  return value as ReturnType<typeof enableTracing<T>>;\n}\n","let allowAssigning = true;\n\n/**\n * Check if `assigning` is enabled. If the assigning is enabled, the `traced` object assignment works as usual.\n * If the assigning is disabled, the `traced` object assignment does not work.\n *\n * @returns true if `assigning` is enabled, false otherwise\n *\n * @example\n * ```typescript\n * console.log(isAssigning()); // true\n *\n * withoutAssigning(() => {\n *   console.log(isAssigning()); // false\n * });\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/ignore-assigning/isAssigning.html Wiki page.}\n *\n * @since 0.12.0\n */\nexport function isAssigning(): boolean {\n  return !!allowAssigning;\n}\n\n/**\n * Ignores any modification to a `traced` structure in the given function (deleting, adding, or changing the values).\n * The function should not be async, as it turns off the `assigning` globally.\n *\n * @warn Use with caution, as it can lead to breaking `applyTrace` function, because `traced` items inside of the function will not recorded any modification.\n *\n * @param callback - function in which `assigning` is disabled\n * @returns same as the return value of the given function\n *\n * @example\n * ```typescript\n * const fabric = traceFabric({ season: 'winter' });\n * fabric.value.season = 'spring'; // assignment is recorded\n *\n * withoutAssigning(() => {\n *   fabric.value.season = 'summer'; // assignment is NOT recorded\n * });\n *\n * console.log(fabric.value);\n * // { season: 'spring' }\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/ignore-assigning/withoutAssigning.html Wiki page.}\n *\n * @since 0.12.0\n */\nexport function withoutAssigning<T>(callback: () => T): T {\n  allowAssigning = false;\n  const result = callback();\n  allowAssigning = true;\n\n  return result;\n}\n","let allowTracing = true;\n\n/**\n * Check if `tracing` is enabled. If the tracing is enabled, the `mutations` are recorded in the trace.\n *\n * @returns true if `tracing` is enabled, false otherwise\n *\n * @example\n * ```typescript\n * console.log(isTracing()); // true\n *\n * withoutTracing(() => {\n *   console.log(isTracing()); // false\n * });\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/ignore-tracing/isTracing.html Wiki page.}\n *\n * @since 0.2.0\n */\nexport function isTracing(): boolean {\n  return !!allowTracing;\n}\n\n/**\n * Ignores recording of all `mutations` to `trace` in the given function.\n * The function should not be async, as it turns off the tracing globally.\n *\n * @warn Use with caution, as it can lead to breaking `applyTrace` function, because of the missing mutations.\n *\n * @param callback - function in which `tracing` is disabled\n * @returns same as the return value of the given function\n *\n * @example\n * ```typescript\n * const fabric = traceFabric({ season: 'winter' });\n * fabric.value.season = 'spring'; // adds mutation to the traceLogs\n *\n * withoutTracing(() => {\n *   fabric.value.season = 'summer'; // no mutation is added to the traceLogs\n * });\n *\n * console.log(fabric.trace);\n * // [{\n * //   mutated: \"object\",\n * //   targetChain: [ \"season\" ],\n * //   value: \"spring\",\n * //   type: \"set\",\n * // }]\n * ```\n *\n * @see {@link https://traced-fabric.github.io/core/ignore-tracing/withoutTracing.html Wiki page.}\n *\n * @since 0.2.0\n */\nexport function withoutTracing<T>(callback: () => T): T {\n  allowTracing = false;\n  const result = callback();\n  allowTracing = true;\n\n  return result;\n}\n","import type { JSONArray, JSONObject, JSONStructure } from './types/json';\nimport {\n  EArrayMutation,\n  EMutated,\n  EObjectMutation,\n  type TArrayMutation,\n  type TMutation,\n  type TObjectMutation,\n  type TTarget,\n} from './types/mutation';\n\nfunction applyObjectMutation(value: JSONObject, mutation: TObjectMutation): void {\n  const lastKey = mutation.targetChain.at(-1);\n  if (lastKey === undefined) throw new Error('Invalid target chain');\n\n  if (mutation.type === EObjectMutation.set) {\n    value[lastKey] = mutation.value;\n  }\n  else if (mutation.type === EObjectMutation.delete) {\n    delete value[lastKey.toString()];\n  }\n}\n\nfunction applyArrayMutation(value: JSONArray, mutation: TArrayMutation): void {\n  const lastKey = mutation.targetChain.at(-1);\n  if (lastKey === undefined) throw new Error('Invalid target chain');\n\n  if (mutation.type === EArrayMutation.set) value[lastKey as number] = mutation.value;\n  else if (mutation.type === EArrayMutation.delete) value.splice(lastKey as number, 1);\n  else if (mutation.type === EArrayMutation.push) value.push(...mutation.value);\n  else if (mutation.type === EArrayMutation.reverse) value.reverse();\n  else if (mutation.type === EArrayMutation.shift) value.shift();\n  else if (mutation.type === EArrayMutation.unshift) value.unshift(...mutation.value);\n}\n\nfunction getMutationTargetWithoutLastKey(\n  value: JSONStructure,\n  targetChain: TTarget[],\n): JSONStructure {\n  let target = value;\n\n  for (let i = 0; i < targetChain.length - 1; i++) {\n    if (targetChain[i] in target) target = (target as any)[targetChain[i]];\n    else throw new Error('Invalid target chain');\n  }\n\n  return target;\n}\n\n/**\n * Applies `tracedFabric` `mutations` to the given value.\n * The value should have the same state as the `traceFabric` value before allied mutations.\n *\n * @WARN This function mutates the value directly.\n *\n * @see {@link https://traced-fabric.github.io/core/core-functions/applyTrace.html Wiki page.}\n *\n * @param value - the object to which the **trace** will be directly applied.\n * @param trace - the `trace` (array of `mutations`) to apply to the given **value**.\n *\n * @example\n * ```typescript\n * const fabric = traceFabric({ season: 'winter' });\n * const target = { season: 'winter' };\n *\n * target.season = 'summer';\n *\n * applyTrace(target, fabric.trace);\n *\n * console.log(target); // { season: 'summer' }\n * ```\n *\n * @since 0.0.1\n */\nexport function applyTrace<T extends JSONStructure>(\n  value: T,\n  trace: TMutation[],\n): void {\n  for (const mutation of trace) {\n    const mutationTarget = getMutationTargetWithoutLastKey(value, mutation.targetChain);\n\n    if (mutation.mutated === EMutated.object) applyObjectMutation(mutationTarget as JSONObject, mutation);\n    else if (mutation.mutated === EMutated.array) applyArrayMutation(mutationTarget as JSONArray, mutation);\n  }\n}\n","import { isStructure } from './utils/isStructure';\n\n// For tracedFabric structuredClone can't be used,\n// because it breaks when trying to copy traced values and general slowness.\n\n/**\n * Deep clone an object or array.\n *\n * Primarily used to clone `tracedFabric` and `tracedValues` without inheriting proxy traps,\n * and potentially breaking tracedFabric.\n *\n * @note `deepClone(...)` will not copy symbols\n *\n * @see {@link https://traced-fabric.github.io/core/core-functions/deepClone.html Wiki page.}\n *\n * @param value - The value to clone\n * @returns The cloned value\n *\n * @example\n * ```typescript\n * const fabric = traceFabric({ season: 'winter', bestDays: [2, 7, 16] });\n * const clone = deepClone(fabric.value);\n *\n * console.log(clone); // { season: 'winter', bestDays: [2, 7, 16] }\n * console.log(clone === fabric.value); // false\n * console.log(clone.bestDays === fabric.value.bestDays); // false\n * ```\n *\n * @since 0.0.1\n */\nexport function deepClone<T>(value: T): T {\n  if (!isStructure(value)) return value;\n\n  const newValue = (Array.isArray(value) ? [] : {}) as typeof value;\n  for (const i in value) newValue[i] = isStructure(value[i]) ? deepClone(value[i]) : value[i];\n\n  return newValue;\n}\n","import { isAssigning } from '../../utils/withoutAssigning';\n\nexport const reflect = new Proxy(Reflect, {\n  get(target, k) {\n    const key = k as keyof typeof Reflect;\n\n    if (typeof target[key] === 'function') {\n      return (...args: any[]) => {\n        if (isAssigning()) return (Reflect as any)[key](...args);\n        return true;\n      };\n    }\n\n    return target[key];\n  },\n});\n","import type { JSONArray, JSONValue } from '../../types/json';\nimport { deepClone } from '../../deepClone';\nimport { EArrayMutation, EMutated, type TMutationCallback } from '../../types/mutation';\nimport { isStructure } from '../../utils/isStructure';\nimport { isTracing } from '../../utils/withoutTracing';\nimport { getMetadata, getTargetChain, type TTracedValueMetadata } from '../metadata';\nimport { removeNestedTracedSubscribers } from '../subscribers';\nimport { deepTrace } from './deepTrace';\nimport { reflect } from './reflect';\n\nconst mutated = EMutated.array;\n\nexport function getTracedProxyArray<T extends JSONArray>(\n  value: T,\n  mutationCallback: TMutationCallback,\n): T {\n  const proxy = new WeakRef(new Proxy(value, {\n    get(target, key, receiver) {\n      if (key === EArrayMutation.push) {\n        return (...args: JSONValue[]) => {\n          const initialLength = target.length;\n          const tracedArgs = args.map((arg, i) => deepTrace(arg, mutationCallback, {\n            parentRef: receiver,\n            key: initialLength + i,\n          }));\n\n          if (isTracing()) {\n            if (args.length > 1) {\n              mutationCallback({\n                mutated,\n                targetChain: getTargetChain(receiver),\n                value: deepClone(args),\n                type: key,\n              });\n            }\n            else {\n              mutationCallback({\n                mutated,\n                targetChain: [...getTargetChain(receiver), initialLength],\n                value: deepClone(args[0]),\n                type: EArrayMutation.set,\n              });\n            }\n          }\n\n          return reflect.apply(target.push, target, tracedArgs);\n        };\n      }\n\n      else if (\n        key === EArrayMutation.reverse\n        || key === EArrayMutation.shift\n      ) {\n        // if majority of array items are changed their positions\n        // their target chain should be updated\n        // so if in the future we will reference them, the reference chain will be correct\n        return () => {\n          for (let i = 0; i < target.length; i++) {\n            const item = target[i];\n            if (!isStructure(item)) continue;\n\n            const metadata = getMetadata(item);\n            if (metadata) metadata.key = target.length - i - 1;\n          }\n\n          if (isTracing()) mutationCallback({ mutated, targetChain: getTargetChain(receiver), type: key });\n\n          return reflect.apply(target[key], target, []);\n        };\n      }\n\n      else if (key === EArrayMutation.unshift) {\n        return (...args: JSONValue[]) => {\n          const initialLength = target.length;\n          const tracedArgs = args.map((arg, i) => deepTrace(arg, mutationCallback, {\n            parentRef: receiver,\n            key: initialLength + i,\n          }));\n\n          if (isTracing()) {\n            mutationCallback({\n              mutated,\n              targetChain: getTargetChain(receiver),\n              value: deepClone(args),\n              type: key,\n            });\n          }\n\n          // before unshifting we should update the target chain of all items\n          // that are already nested in the array\n          for (let i = 0; i < target.length; i++) {\n            const item = target[i];\n            if (!isStructure(item)) continue;\n\n            const metadata = getMetadata(item);\n            if (metadata) metadata.key = initialLength + i;\n          }\n\n          return reflect.apply(target.unshift, target, tracedArgs);\n        };\n      }\n\n      return Reflect.get(target, key);\n    },\n\n    set(target, key, value, receiver) {\n      if (typeof key === 'symbol') return reflect.set(target, key, value);\n\n      const index = +key;\n\n      // ignoring non-integer keys (for example 'length')\n      if (Number.isNaN(index)) return reflect.set(target, key, value);\n\n      const childMetadata: TTracedValueMetadata = { parentRef: receiver, key: index };\n\n      // if the value that is overridden and it is a tracedFabric,\n      // we should remove the subscriber from the old value\n      if (isStructure(target[index])) removeNestedTracedSubscribers(target[index], childMetadata);\n\n      if (isTracing()) {\n        mutationCallback({\n          mutated,\n          targetChain: [...getTargetChain(receiver), index],\n          value: deepClone(value),\n          type: EArrayMutation.set,\n        });\n      }\n\n      return reflect.set(target, key, deepTrace(value, mutationCallback, childMetadata));\n    },\n\n    deleteProperty(target, key) {\n      if (typeof key === 'symbol') return reflect.set(target, key, value);\n\n      const index = +key;\n\n      if (Number.isNaN(index)) return reflect.deleteProperty(target, key);\n\n      const ref = proxy.deref();\n      if (!ref) return reflect.deleteProperty(target, key);\n\n      if (isStructure(target[index])) {\n        removeNestedTracedSubscribers(target[index], {\n          parentRef: ref,\n          key: index,\n        });\n      }\n\n      if (isTracing()) {\n        mutationCallback({\n          mutated,\n          targetChain: [...getTargetChain(ref), index],\n          type: EArrayMutation.delete,\n        });\n      }\n\n      return reflect.deleteProperty(target, key);\n    },\n  }));\n\n  return proxy.deref() as T;\n}\n","import type { JSONObject } from '../../types/json';\nimport { deepClone } from '../../deepClone';\nimport { EMutated, EObjectMutation, type TMutationCallback } from '../../types/mutation';\nimport { isStructure } from '../../utils/isStructure';\nimport { isTracing } from '../../utils/withoutTracing';\nimport { getTargetChain, type TTracedValueMetadata } from '../metadata';\nimport { removeNestedTracedSubscribers } from '../subscribers';\nimport { deepTrace } from './deepTrace';\nimport { reflect } from './reflect';\n\nconst mutated = EMutated.object;\n\nexport function getTracedProxyObject<T extends JSONObject>(\n  value: T,\n  mutationCallback: TMutationCallback,\n): T {\n  const proxy = new WeakRef(new Proxy(value, {\n    set(target, key, value, receiver) {\n      if (typeof key === 'symbol') return reflect.set(target, key, value);\n\n      const childMetadata: TTracedValueMetadata = { parentRef: receiver, key };\n\n      // if the value that is overridden and it is a tracedFabric,\n      // we should remove the subscriber from the old value\n      if (isStructure(target[key])) removeNestedTracedSubscribers(target[key], childMetadata);\n\n      if (isTracing()) {\n        mutationCallback({\n          mutated,\n          targetChain: [...getTargetChain(receiver), key],\n          value: deepClone(value),\n          type: EObjectMutation.set,\n        });\n      }\n\n      return reflect.set(target, key, deepTrace(value, mutationCallback, childMetadata));\n    },\n\n    deleteProperty(target, key) {\n      if (typeof key === 'symbol') return reflect.set(target, key, value);\n\n      const ref = proxy.deref();\n      if (!ref) return reflect.deleteProperty(target, key);\n\n      if (isStructure(target[key])) removeNestedTracedSubscribers(target[key], { parentRef: ref, key });\n\n      if (isTracing()) {\n        mutationCallback({\n          mutated,\n          targetChain: [...getTargetChain(ref), key],\n          type: EObjectMutation.delete,\n        });\n      }\n\n      return reflect.deleteProperty(target, key);\n    },\n  }));\n\n  return proxy.deref() as T;\n}\n","import type { JSONValue } from '../../types/json';\nimport type { TMutationCallback } from '../../types/mutation';\nimport { isTracingEnabled } from '../../utils/disableTracing';\nimport { isStructure } from '../../utils/isStructure';\nimport { isTracedFabric } from '../../utils/isTraced';\nimport { setMetadata, type TTracedValueMetadata } from '../metadata';\nimport { mutationCallbacks } from '../mutationCallback';\nimport { addTracedSubscriber } from '../subscribers';\nimport { tracedFabricsTrace } from '../traces';\nimport { getTracedProxyArray } from './getTracedArray';\nimport { getTracedProxyObject } from './getTracedObject';\n\n// what this function should do:\n// * will trace the given value recursively\n// * will set the tracedFabric references\n// * will set the tracedValues metadata\n// * if the value is already a traced fabric, return as is\n// * if the value is not a structure (Object/Array), return as is\nexport function deepTrace<T extends JSONValue>(\n  value: T,\n  mutationCallback: TMutationCallback,\n  metadata?: TTracedValueMetadata,\n): T {\n  // if data type is common and not structure-like, return as is\n  if (!isStructure(value)) return value;\n\n  // if the given value is already disabled for tracing, return as is\n  if (!isTracingEnabled(value)) return value;\n\n  // if the given value is already traced (tracedFabric),\n  // we should subscribe the current value to metadata root(tracedFabric)\n  if (isTracedFabric(value)) {\n    if (metadata) addTracedSubscriber(value, metadata);\n\n    return value;\n  }\n\n  // if we found that the given value is an object-like structure:\n  // 1. apply the tracing behavior to the given value\n  // 2. iterate over the object, to see if keys\n  //    are also objects-like structures and trace them recursively\n  const proxy = Array.isArray(value)\n    ? getTracedProxyArray(value, mutationCallback)\n    : getTracedProxyObject(value, mutationCallback);\n\n  if (metadata) {\n    setMetadata(proxy, metadata);\n  }\n  else {\n    tracedFabricsTrace.set(proxy, []);\n    mutationCallbacks.set(proxy, mutationCallback);\n  }\n\n  for (const key in value) {\n    if (!isStructure(value[key])) continue;\n\n    const maybeNumber = Number.isNaN(+key) ? key : +key;\n    value[key] = deepTrace(value[key], mutationCallback, { parentRef: proxy, key: maybeNumber });\n  }\n\n  return proxy as T;\n}\n","import type { JSONStructure } from './types/json';\nimport type { TMutation, TMutationCallback } from './types/mutation';\nimport type { TOnMutation, TTracedFabric } from './types/tracedFabric';\nimport { deepTrace } from './core/proxy/deepTrace';\nimport { updateSubscribers } from './core/subscribers';\nimport { tracedFabricsTrace } from './core/traces';\nimport { isTracingEnabled } from './utils/disableTracing';\nimport { withoutTracing } from './utils/withoutTracing';\n\n/**\n * Track the mutation of a given JSON-like object or array.\n * Other `tracedFabric` can be nested inside.\n *\n * The `trace` (array of `mutations`), that is produced by the `tracedFabric` on values mutation,\n * can be used to apply them to other objects or arrays, using the `applyTrace(...)` function.\n *\n * @see {@link https://traced-fabric.github.io/core/core-functions/traceFabric.html Wiki page.}\n *\n * @param {JSONStructure} value an object or array that will be deeply tracked. Value is JSON.stringify safe. The type of the value should be the same as JSONStructure.\n * @param {TOnMutation} [onMutation] a function that is triggered when the `traced` value is mutated. The function receives the `mutation` as an argument and should return the `mutation` to be saved to the `trace`. If no modification is needed, return the original mutation; otherwise, return the modified mutation for storage.\n *\n * @returns {TTracedFabric} Object with the following properties:\n * - **value** - the `traced` proxy of the given **value**.\n * - **trace** - the array of `mutations` that are made to the **value**.\n * - **clearTrace** - a function that clears the trace.\n *\n * @example\n * ```typescript\n * const bestDays = traceFabric([2, 7, 16]);\n * const fabric = traceFabric({\n *   season: 'winter',\n *   bestDays: bestDays.value,\n * });\n *\n * fabric.value.season = 'summer';\n * fabric.value.bestDays.push(25);\n * bestDays.value.push(26);\n *\n * console.log(fabric.trace);\n * // [{\n * //   mutated: 'object', type: 'set',\n * //   targetChain: ['season'],\n * //   value: 'summer',\n * // }, {\n * //   mutated: 'array', type: 'set',\n * //   targetChain: ['bestDays', 3],\n * //   value: 25,\n * // }, {\n * //   mutated: 'array', type: 'set',\n * //   targetChain: ['bestDays', 4],\n * //   value: 26,\n * // }]\n * ```\n *\n * @since 0.0.1\n */\nexport function traceFabric<\n  T extends JSONStructure = JSONStructure,\n  _MUTATION = TMutation,\n>(\n  value: T,\n  onMutation?: TOnMutation<_MUTATION>,\n): TTracedFabric<T, _MUTATION> {\n  if (!isTracingEnabled(value)) throw new Error('Cannot create traceFabric when tracing is disabled.');\n\n  let proxyRef: T;\n\n  const trace = (): _MUTATION[] => tracedFabricsTrace.get(proxyRef)!;\n  const setTrace = (newTrace: _MUTATION[]): void => {\n    tracedFabricsTrace.set(proxyRef, newTrace);\n  };\n  const clearTrace = (): void => setTrace([]);\n\n  const mutationCallback: TMutationCallback = (mutation) => {\n    trace()?.push(onMutation?.(mutation) ?? mutation as _MUTATION);\n    updateSubscribers(proxyRef, mutation);\n  };\n\n  proxyRef = withoutTracing(() => deepTrace(value, mutationCallback));\n\n  return {\n    value: proxyRef,\n\n    get trace() { return trace(); },\n    set trace(newTrace: _MUTATION[]) { setTrace(newTrace); },\n\n    clearTrace,\n  };\n}\n"],"names":["trace","EMutated","EObjectMutation","EArrayMutation","mutated","value"],"mappings":";;;;;;;;;;;AAyBO,WAAS,YACd,OACoD;AAC7C,WAAA,OAAO,UAAU,YAAY,UAAU;AAAA,EAChD;ACfa,QAAA,2CAA2B;AAMjC,WAAS,YAAY,QAA6D;AAChF,WAAA,qBAAqB,IAAI,MAAM;AAAA,EACxC;AAMO,WAAS,kBAAkB,QAAyD;AACnF,UAAA,WAAW,YAAY,MAAM;AACnC,QAAI,CAAC,SAAU;AAET,UAAA,YAAY,SAAS,UAAU,MAAM;AAC3C,QAAI,CAAC,UAAW;AAEhB,WAAO,EAAE,WAAW,KAAK,SAAS,IAA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