{"version":3,"file":"use-transient-updates.mjs","sources":["../src/index.ts"],"sourcesContent":["import React, { useMemo, useRef, createRef } from 'react';\nimport { useRender, applyProps } from 'react-three-fiber';\n\nexport function useTransientData<T>(dataSource: T, mapDataSource: (source: T) => object) {\n  const bind = useRef<any>();\n  useRender(() => applyProps(bind.current, mapDataSource(dataSource)), false);\n  return bind;\n}\n\nexport function useTransientDataList<T>(\n  dataSources: T[],\n  mapDataSource: (source: T) => object,\n  amount: number = dataSources.length,\n) {\n  const refsRef = useRef<Array<React.MutableRefObject<any>>>([]);\n  // update refs array only when \"amount\" changed\n  refsRef.current = useMemo(() => {\n    const elementRefs: Array<React.MutableRefObject<any>> = [];\n    for (let count = 0; count < amount; count += 1) {\n      elementRefs.push(createRef<any>());\n    }\n    return elementRefs;\n  }, [amount]);\n  useRender(\n    () => {\n      refsRef.current.forEach((ref, index) => {\n        // after this ref attached to the element, and data is prepared\n        if (ref.current && dataSources[index]) {\n          const props = mapDataSource(dataSources[index]);\n          applyProps(ref.current, props);\n        }\n      });\n    },\n    false,\n  );\n  return refsRef.current;\n}\n"],"names":["useTransientData","dataSource","mapDataSource","bind","useRef","useRender","applyProps","current","useTransientDataList","dataSources","amount","length","refsRef","useMemo","elementRefs","count","push","createRef","forEach","ref","index","props"],"mappings":"kIAGgBA,EAAoBC,EAAeC,OAC3CC,EAAOC,WACbC,oBAAgBC,EAAWH,EAAKI,QAASL,EAAcD,MAAc,GAC9DE,EAGT,SAAgBK,EACdC,EACAP,EACAQ,kBAAiBD,EAAYE,YAEvBC,EAAUR,EAA2C,WAE3DQ,EAAQL,QAAUM,qBACVC,EAAkD,GAC/CC,EAAQ,EAAGA,EAAQL,EAAQK,GAAS,EAC3CD,EAAYE,KAAKC,YAEZH,GACN,CAACJ,IACJL,aAEIO,EAAQL,QAAQW,iBAASC,EAAKC,MAExBD,EAAIZ,SAAWE,EAAYW,GAAQ,KAC/BC,EAAQnB,EAAcO,EAAYW,IACxCd,EAAWa,EAAIZ,QAASc,QAI9B,GAEKT,EAAQL"}