{"version":3,"file":"svelte.mjs","names":[],"sources":["../../src/svelte/miniStore.svelte.ts","../../src/svelte/index.svelte.ts"],"sourcesContent":["import {\n  type ArrayVal,\n  isArray,\n  isPlainObject,\n  objectHasProp,\n  type TweenProps,\n} from \"@thednp/tween\";\n\n/** The marker property name used to flag already-proxied values */\nconst STATE_PROXY = \"_proxy\";\n/** The descriptor used for the non-enumerable `_proxy` marker property */\nconst proxyProps = {\n  value: 1,\n  enumerable: false,\n  configurable: false,\n  writable: false,\n};\n\n/**\n * Defines a reactive proxy for a single array index, with a nested\n * proxy for sub-arrays. Array mutations are tracked with a\n * version-toggle `$state` so the owning getter stays reactive.\n *\n * @template T - The type of the array value\n * @param index - The array index to proxy\n * @param value - The initial value at the given index\n * @param target - The array being built\n * @param sourceLen - The length of the source array\n * @param notifyListeners - The function called when the last element changes\n */\nfunction defineArrayProxy<T extends ArrayVal>(\n  index: number,\n  value: T[number] | ArrayVal,\n  target: T | ArrayVal | ArrayVal[],\n  sourceLen: number,\n  notifyListeners: () => void,\n) {\n  const itemIsLast = index === sourceLen - 1;\n\n  if (isArray(value)) {\n    const subArray: typeof value = [];\n    const valueLen = value.length;\n\n    value.forEach((itm, idx) => {\n      const subItemIsLast = itemIsLast && idx === valueLen - 1;\n      let currentItem = itm;\n\n      Object.defineProperty(subArray, idx, {\n        get: () => currentItem,\n        set: (newValue: typeof itm) => {\n          currentItem = newValue;\n\n          // Only notify on last element to batch updates\n          if (subItemIsLast) {\n            notifyListeners();\n          }\n        },\n        enumerable: true,\n      });\n    });\n    target[index] = subArray;\n  } else {\n    let currentValue = value;\n    const getter = () => currentValue;\n    const setter = (newVal: typeof value) => {\n      currentValue = newVal;\n      if (itemIsLast) {\n        notifyListeners();\n      }\n    };\n    Object.defineProperties(target, {\n      [index]: {\n        get: getter,\n        set: setter,\n        enumerable: true,\n      },\n    });\n  }\n}\n\n/**\n * Defines a reactive getter/setter pair for a state property, backed by\n * a Svelte 5 `$state` primitive. Array values are wrapped with a\n * version-toggle `$state` so element mutations stay reactive without\n * replacing the array.\n *\n * @template T - The type of the state object\n * @param key - The property key to proxy\n * @param value - The initial property value\n * @param target - The object being built\n */\nfunction defineStateProxy<T extends Omit<TweenProps, \"_proxy\">>(\n  key: number | keyof T,\n  value: T[keyof T] | ArrayVal,\n  target: T | ArrayVal,\n) {\n  let state = $state.raw(value);\n  let getter = () => state;\n  let setter;\n\n  if (isArray(value)) {\n    const arrayProxy: typeof value = [];\n    const valLength = value.length;\n    let version = $state.raw(0);\n    const getVersion = () => version;\n    for (let i = 0; i < valLength; i++) {\n      defineArrayProxy(i, (value as ArrayVal)[i], arrayProxy, valLength, () => {\n        version = 1 - version;\n      });\n    }\n    getter = () => {\n      getVersion();\n      return state;\n    };\n\n    state = arrayProxy;\n  } else {\n    setter = (newVal: typeof value) => state = newVal;\n    state = value;\n  }\n\n  Object.defineProperties(target, {\n    [STATE_PROXY]: proxyProps,\n    [key]: {\n      get: getter,\n      set: setter,\n      enumerable: true,\n    },\n  });\n}\n\n/**\n * Recursively builds the reactive state object for a plain object,\n * proxying primitive and array values and delegating plain objects.\n *\n * @template T - The type of the state object\n * @param obj - The source object\n * @param parentReceiver - The object being built\n * @returns The built reactive state object\n */\nfunction createMiniState<T extends TweenProps>(\n  obj: T,\n  parentReceiver: TweenProps | number[] | [string, ...number[]][],\n) {\n  if (objectHasProp(obj, STATE_PROXY)) return obj;\n\n  for (const [key, value] of Object.entries(obj)) {\n    if (isPlainObject(value)) {\n      (parentReceiver as TweenProps)[key] = createMiniState(value, {});\n    } else {\n      defineStateProxy(key, value, parentReceiver);\n    }\n  }\n\n  return parentReceiver as T;\n}\n\n/**\n * Creates a reactive store for the given initial values, where every\n * property is backed by a Svelte 5 `$state` primitive. Reading a property\n * inside a component tracks the state; writing it (e.g. by the tween\n * update loop) triggers reactivity.\n *\n * @template T - The type of the state object\n * @param init - The initial values object\n * @returns The reactive state object\n */\nexport function miniStore<\n  T extends TweenProps,\n>(init: T): T {\n  return createMiniState(init, {}) as T;\n}\n","/**\n * Svelte integration for {@link @thednp/tween}.\n *\n * Provides `createTween` and `createTimeline` primitives backed by a\n * reactive miniStore, plus re-exports of the core {@link Tween} /\n * {@link Timeline} classes and the {@link miniStore} helper.\n *\n * @module\n */\nimport {\n  dummyInstance,\n  isServer,\n  Timeline,\n  Tween,\n  type TweenProps,\n} from \"@thednp/tween\";\nimport { onDestroy } from \"svelte\";\nimport { miniStore } from \"./miniStore.svelte.ts\";\n\n/**\n * Re-exports the core classes and the miniStore helper.\n */\nexport { miniStore, Timeline, Tween };\n\n/**\n * Svelte hook for updating values with Tween.\n *\n * @param initialValues - Initial tween values\n * @returns [store, tween] Tuple of reactive store and Tween instance\n *\n * @example\n * <script lang=\"ts\">\n *    const [state, tween] = createTween({ x: 0, y: 0 })\n *\n *    // configuration is free-form, no re-render ever happens\n *    tween.to({ x: 100, y: 100 })\n *\n *    onMount(() => {\n *      tween.start()\n *    })\n * </script>\n *\n * <div style={{ translate: `${state.x}px ${state.y}px` }} />\n */\nexport function createTween<\n  T extends TweenProps,\n>(initialValues: T): readonly [T, Tween<T>] {\n  if (isServer) {\n    return [initialValues, dummyInstance as unknown as Tween<T>] as const;\n  }\n  const store = miniStore(initialValues);\n  const tween = new Tween(store);\n\n  onDestroy(() => {\n    tween.stop();\n    tween.clear();\n  });\n\n  return [store, tween] as [T, Tween<T>];\n}\n\n/**\n * Svelte hook for sequencing values update with Timeline.\n *\n * @param initialValues - Initial tween values\n * @returns [store, timeline] Tuple of reactive store and Timeline instance\n *\n * @example\n * <script lang=\"ts\">\n *    const [state, timeline] = createTimeline({ x: 0, y: 0 })\n *\n *    // configuration is free-form\n *    timeline.to({ x: 100, y: 100 })\n *\n *    onMount(() => {\n *      timeline.play()\n *    })\n * </script>\n *\n * <div style={{ translate: `${state.x}px ${state.y}px` }} />\n */\nexport function createTimeline<\n  T extends TweenProps,\n>(initialValues: T): readonly [T, Timeline<T>] {\n  if (isServer) {\n    return [initialValues, dummyInstance as unknown as Timeline<T>] as const;\n  }\n  const store = miniStore(initialValues);\n  const timeline = new Timeline(store);\n\n  onDestroy(() => {\n    timeline.stop();\n    timeline.clear();\n  });\n\n  return [store, timeline] as [T, Timeline<T>];\n}\n"],"mappings":";;;;;;;;;;;AASA,MAAM,cAAc;;AAEpB,MAAM,aAAa;CACjB,OAAO;CACP,YAAY;CACZ,cAAc;CACd,UAAU;AACZ;;;;;;;;;;;;;AAcA,SAAS,iBACP,OACA,OACA,QACA,WACA,iBACA;CACA,MAAM,aAAa,UAAU,YAAY;CAEzC,IAAI,QAAQ,KAAK,GAAG;EAClB,MAAM,WAAyB,CAAC;EAChC,MAAM,WAAW,MAAM;EAEvB,MAAM,SAAS,KAAK,QAAQ;GAC1B,MAAM,gBAAgB,cAAc,QAAQ,WAAW;GACvD,IAAI,cAAc;GAElB,OAAO,eAAe,UAAU,KAAK;IACnC,WAAW;IACX,MAAM,aAAyB;KAC7B,cAAc;KAGd,IAAI,eACF,gBAAgB;IAEpB;IACA,YAAY;GACd,CAAC;EACH,CAAC;EACD,OAAO,SAAS;CAClB,OAAO;EACL,IAAI,eAAe;EACnB,MAAM,eAAe;EACrB,MAAM,UAAU,WAAyB;GACvC,eAAe;GACf,IAAI,YACF,gBAAgB;EAEpB;EACA,OAAO,iBAAiB,QAAQ,GAC7B,QAAQ;GACP,KAAK;GACL,KAAK;GACL,YAAY;EACd,EACF,CAAC;CACH;AACF;;;;;;;;;;;;AAaA,SAAS,iBACP,KACA,OACA,QACA;CACA,IAAI,QAAQ,OAAO,IAAI,KAAK;CAC5B,IAAI,eAAe;CACnB,IAAI;CAEJ,IAAI,QAAQ,KAAK,GAAG;EAClB,MAAM,aAA2B,CAAC;EAClC,MAAM,YAAY,MAAM;EACxB,IAAI,UAAU,OAAO,IAAI,CAAC;EAE1B,KAAK,IAAI,IAAI,GAAG,IAAI,WAAW,KAC7B,iBAAiB,GAAI,MAAmB,IAAI,YAAY,iBAAiB;GACvE,UAAU,IAAI;EAChB,CAAC;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