{"version":3,"file":"vue.mjs","names":[],"sources":["../../src/vue/miniStore.ts","../../src/vue/index.ts"],"sourcesContent":["import { ref } from \"vue\";\nimport {\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 `ref` 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\n      let currentItem = itm;\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 Vue `ref`. Array values are wrapped with a version-toggle `ref` so\n * element mutations stay reactive without 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  const state = ref(value);\n  let getter = () => state.value;\n  let setter;\n\n  if (isArray(value)) {\n    const arrayProxy: typeof value = [];\n    const valLength = value.length;\n    const version = ref(0);\n    for (let i = 0; i < valLength; i++) {\n      defineArrayProxy(i, (value as ArrayVal)[i], arrayProxy, valLength, () => {\n        version.value = 1 - version.value;\n      });\n    }\n    getter = () => {\n      version.value;\n      return state.value;\n    };\n\n    state.value = arrayProxy;\n  } else {\n    setter = (newVal: typeof value) => {\n      state.value = newVal;\n    };\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 Vue `ref`. Reading a property inside a\n * component tracks the ref; writing it (e.g. by the tween update loop)\n * 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 * Vue integration for {@link @thednp/tween}.\n *\n * Provides `useTween` and `useTimeline` hooks backed by a reactive\n * miniStore, plus re-exports of the core {@link Tween} / {@link Timeline}\n * 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 { onUnmounted } from \"vue\";\nimport { miniStore } from \"./miniStore.ts\";\n\n/**\n * Re-exports the core classes and the miniStore helper.\n */\nexport { miniStore, Timeline, Tween };\n\n/**\n * Vue composable 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 setup lang=\"ts\">\n *    const [state, tween] = useTween({ x: 0, y: 0 })\n *\n *    // configuration is free-form, no re-render ever happens\n *    tween.to({ x: 100, y: 100 })\n *\n *    onMounted(() => {\n *      tween.start()\n *    })\n * </script>\n * <template>\n *  <div :style=\"{ translate: `${state.x}px ${state.y}px` }\" />\n * </template>\n */\nexport function useTween<\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  onUnmounted(() => {\n    tween.stop();\n    tween.clear();\n  });\n\n  return [store, tween] as [T, Tween<T>];\n}\n\n/**\n * Vue composable 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 setup lang=\"ts\">\n *    const [state, timeline] = useTimeline({ x: 0, y: 0 })\n *\n *    // configuration is free-form\n *    timeline.to({ x: 100, y: 100 })\n *\n *    onMounted(() => {\n *      timeline.play()\n *    })\n * </script>\n *\n * <template>\n *  <div :style=\"{ translate: `${state.x}px ${state.y}px` }\" />\n * </template>\n */\nexport function useTimeline<\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  onUnmounted(() => {\n    timeline.stop();\n    timeline.clear();\n  });\n\n  return [store, timeline] as [T, Timeline<T>];\n}\n"],"mappings":";;;;;;;;;;;AAUA,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;GAEvD,IAAI,cAAc;GAClB,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;;;;;;;;;;;AAYA,SAAS,iBACP,KACA,OACA,QACA;CACA,MAAM,QAAQ,IAAI,KAAK;CACvB,IAAI,eAAe,MAAM;CACzB,IAAI;CAEJ,IAAI,QAAQ,KAAK,GAAG;EAClB,MAAM,aAA2B,CAAC;EAClC,MAAM,YAAY,MAAM;EACxB,MAAM,UAAU,IAAI,CAAC;EACrB,KAAK,IAAI,IAAI,GAAG,IAAI,WAAW,KAC7B,iBAAiB,GAAI,MAAmB,IAAI,YAAY,iBAAiB;GACvE,QAAQ,QAAQ,I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