{"version":3,"file":"react.mjs","names":[],"sources":["../../src/react/miniStore.ts","../../src/react/index.ts"],"sourcesContent":["import { useEffect, useRef, useState } from \"react\";\nimport {\n  type ArrayVal,\n  isArray,\n  isPlainObject,\n  objectHasProp,\n  type TweenProps,\n} from \"@thednp/tween\";\n\nconst STATE_PROXY = \"_proxy\";\nconst proxyProps = {\n  value: 1,\n  enumerable: false,\n  configurable: false,\n  writable: false,\n};\n\ntype Listener<T> = (state: T) => void;\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\nfunction defineStateProxy<T extends Omit<TweenProps, \"_proxy\">>(\n  key: number | keyof T,\n  value: T[keyof T],\n  target: T | ArrayVal,\n  notifyListeners: () => void,\n) {\n  const valueIsArray = isArray(value);\n  let currentValue = value as ArrayVal | ArrayVal[];\n\n  const getter = () => currentValue;\n  let setter;\n\n  if (valueIsArray) {\n    // Build array proxy structure\n    const arrayProxy: ArrayVal | ArrayVal[] = [];\n    const valLength = value.length;\n\n    for (let i = 0; i < valLength; i++) {\n      defineArrayProxy(\n        i,\n        (value as ArrayVal)[i],\n        arrayProxy as ArrayVal,\n        valLength,\n        notifyListeners,\n      );\n    }\n    currentValue = arrayProxy;\n  } else {\n    setter = (newValue: typeof currentValue) => {\n      if (currentValue !== newValue) {\n        currentValue = newValue;\n        notifyListeners();\n      }\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\nfunction createMiniState<T extends TweenProps>(\n  obj: T,\n  parentReceiver: TweenProps,\n  notifyListeners: () => void,\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[key] = createMiniState(value, {}, notifyListeners);\n    } else {\n      defineStateProxy(key, value, parentReceiver, notifyListeners);\n    }\n  }\n\n  return parentReceiver as T;\n}\n\nexport function miniStore<T extends TweenProps>(init: T) {\n  const listeners = new Set<Listener<T>>();\n  const notifyListeners = () => {\n    listeners.forEach((listener) => listener(store));\n  };\n\n  const store = createMiniState(init, {}, notifyListeners) as T;\n\n  return {\n    get state() {\n      return store;\n    },\n    subscribe: (listener: Listener<T>) => {\n      listeners.add(listener);\n      return () => {\n        listeners.delete(listener);\n      };\n    },\n  };\n}\n\nexport function useMiniStore<T extends TweenProps>(initialValue: T) {\n  const storeRef = useRef<ReturnType<typeof miniStore<T>>>(null);\n  const [, setVersion] = useState(0);\n\n  // istanbul ignore else @preserve\n  if (!storeRef.current) {\n    storeRef.current = miniStore(initialValue);\n  }\n\n  useEffect(\n    () => storeRef.current!.subscribe(() => setVersion((v) => (v + 1) % 3)),\n    [],\n  );\n\n  return storeRef.current!.state;\n}\n","import { useEffect, useRef } from \"react\";\nimport {\n  dummyInstance,\n  isServer,\n  Timeline,\n  Tween,\n  type TweenProps,\n} from \"@thednp/tween\";\nimport { useMiniStore } from \"./miniStore.ts\";\n\nexport { Timeline, Tween, useMiniStore };\n\n/**\n * Hook for updating values with Tween.\n *\n * **NOTE**: - configuration must be wrapped in `useEffect` or `eventListener`.\n * This has two important aspects: never configure or start update loop in SSR\n * and only configure or start the loop when component is mounted in the client.\n *\n * @param initialValues - Initial tween values\n * @returns [store, tween] Tuple of reactive store and Tween instance\n * @example\n * const App = () => {\n *    const [state, tween] = useTween({ x: 0, y: 0 })\n *\n *    useEffect(() => {\n *      tween.to({ x: 100, y: 100 }).start()\n *    }, [])\n *\n *    return (\n *      <div style={{ translate: `${state.x}px ${state.y}px` }} />\n *    );\n * }\n */\nexport const useTween = <T extends TweenProps>(initialValues: T) => {\n  if (isServer) {\n    return [initialValues, dummyInstance as unknown as Tween<T>] as const;\n  }\n  const tweenRef = useRef<Tween<T> | null>(null);\n  const state = useMiniStore(initialValues);\n\n  // istanbul ignore else @preserve\n  if (!tweenRef.current) {\n    tweenRef.current = new Tween(state);\n  }\n\n  useEffect(() => {\n    return () => {\n      tweenRef.current?.stop();\n      tweenRef.current?.clear();\n    };\n  }, []);\n\n  return [state, tweenRef.current] as [T, Tween<T>];\n};\n\n/**\n * Hook for sequencing values update with Timeline.\n *\n * **NOTE**: - configuration must be wrapped in `useEffect` or `eventListener`.\n * This has two important aspects: never configure or start update loop in SSR\n * and only configure or start the loop when component is mounted in the client.\n *\n * @param initialValues - Initial tween values\n * @returns [store, timeline] Tuple of reactive store and Timeline instance\n * @example\n * const App = () => {\n *    const [state, timeline] = useTimeline({ x: 0, y: 0 })\n *\n *    useEffect(() => {\n *      timeline.to({ x: 100, y: 100 }).play()\n *    }, [])\n *\n *    return (\n *      <div style={{ translate: `${state.x}px ${state.y}px` }} />\n *    );\n * }\n */\nexport function useTimeline<T extends TweenProps>(initialValues: T) {\n  if (isServer) {\n    return [initialValues, dummyInstance as unknown as Timeline<T>] as const;\n  }\n  const timelineRef = useRef<Timeline<T> | null>(null);\n  const state = useMiniStore(initialValues);\n\n  // istanbul ignore else @preserve\n  if (!timelineRef.current) {\n    timelineRef.current = new Timeline(state);\n  }\n\n  useEffect(() => {\n    return () => {\n      timelineRef.current?.clear();\n      timelineRef.current?.stop();\n    };\n  }, []);\n\n  return [state, timelineRef.current] as [T, Timeline<T>];\n}\n"],"mappings":";;;;;;;;;;AASA,MAAM,cAAc;AACpB,MAAM,aAAa;CACjB,OAAO;CACP,YAAY;CACZ,cAAc;CACd,UAAU;CACX;AAID,SAAS,iBACP,OACA,OACA,QACA,WACA,iBACA;CACA,MAAM,aAAa,UAAU,YAAY;AAEzC,KAAI,QAAQ,MAAM,EAAE;EAClB,MAAM,WAAyB,EAAE;EACjC,MAAM,WAAW,MAAM;AAEvB,QAAM,SAAS,KAAK,QAAQ;GAC1B,MAAM,gBAAgB,cAAc,QAAQ,WAAW;GAEvD,IAAI,cAAc;AAClB,UAAO,eAAe,UAAU,KAAK;IACnC,WAAW;IACX,MAAM,aAAyB;AAC7B,mBAAc;AAGd,SAAI,cACF,kBAAiB;;IAGrB,YAAY;IACb,CAAC;IACF;AACF,SAAO,SAAS;QACX;EACL,IAAI,eAAe;EACnB,MAAM,eAAe;EACrB,MAAM,UAAU,WAAyB;AACvC,kBAAe;AACf,OAAI,WACF,kBAAiB;;AAGrB,SAAO,iBAAiB,QAAQ,GAC7B,QAAQ;GACP,KAAK;GACL,KAAK;GACL,YAAY;GACb,EACF,CAAC;;;AAIN,SAAS,iBACP,KACA,OACA,QACA,iBACA;CACA,MAAM,eAAe,QAAQ,MAAM;CACnC,IAAI,eAAe;CAEnB,MAAM,eAAe;CACrB,IAAI;AAEJ,KAAI,cAAc;EAEhB,MAAM,aAAoC,EAAE;EAC5C,MAAM,YAAY,MAAM;AAExB,OAAK,IAAI,IAAI,GAAG,IAAI,WAAW,IAC7B,kBACE,GACC,MAAmB,IACpB,YACA,WACA,gBACD;AAEH,iBAAe;OAEf,WAAU,aAAkC;AAC1C,MAAI,iBAAiB,UAAU;AAC7B,kBAAe;AACf,oBAAiB;;;AAKvB,QAAO,iBAAiB,QAAQ;GAC7B,cAAc;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