one
Version:
One is a React framework for web, iOS, and Android apps.
1 lines • 2.59 kB
Source Map (JSON)
{"version":3,"file":"useBackgroundComputation.js","names":[],"sources":["esm/background/useBackgroundComputation.js"],"sourcesContent":["import { useCallback, useLayoutEffect, useRef, useState } from \"react\";\nimport { createLatestComputation } from \"./createLatestComputation\";\nfunction useBackgroundComputation(definition, input) {\n const [state, setState] = useState({\n phase: \"idle\",\n revision: 0\n });\n const owner = useRef(\n null\n );\n const submitted = useRef(null);\n const active = input !== null;\n useLayoutEffect(() => {\n if (!active) return;\n const computation = createLatestComputation(definition, (changed) => {\n setState(\n changed.phase === \"ready\" ? { phase: \"ready\", revision: changed.result.revision } : changed\n );\n });\n owner.current = computation;\n return () => {\n owner.current = null;\n submitted.current = null;\n computation.dispose();\n };\n }, [active, definition]);\n useLayoutEffect(() => {\n if (input === null) return;\n submitted.current = { input, factory: definition };\n owner.current?.update(input);\n }, [active, definition, input]);\n const getCurrent = useCallback(() => owner.current?.getCurrent() ?? null, []);\n const matches = active && submitted.current?.input === input && submitted.current.factory === definition;\n if (matches && state.phase === \"failed\") throw state.error;\n const current = owner.current?.getCurrent();\n const result = matches && state.phase === \"ready\" && current && state.revision === current.revision ? current : null;\n return { result, getCurrent };\n}\nexport {\n useBackgroundComputation\n};\n//# sourceMappingURL=useBackgroundComputation.js.map\n"],"mappings":";;;;AAEA,SAAS,yBAAyB,YAAY,OAAO;CACnD,MAAM,CAAC,OAAO,YAAY,SAAS;EACjC,OAAO;EACP,UAAU;CACZ,CAAC;CACD,MAAM,QAAQ,OACZ,IACF;CACA,MAAM,YAAY,OAAO,IAAI;CAC7B,MAAM,SAAS,UAAU;CACzB,sBAAsB;EACpB,IAAI,CAAC,QAAQ;EACb,MAAM,cAAc,wBAAwB,aAAa,YAAY;GACnE,SACE,QAAQ,UAAU,UAAU;IAAE,OAAO;IAAS,UAAU,QAAQ,OAAO;GAAS,IAAI,OACtF;EACF,CAAC;EACD,MAAM,UAAU;EAChB,aAAa;GACX,MAAM,UAAU;GAChB,UAAU,UAAU;GACpB,YAAY,QAAQ;EACtB;CACF,GAAG,CAAC,QAAQ,UAAU,CAAC;CACvB,sBAAsB;EACpB,IAAI,UAAU,MAAM;EACpB,UAAU,UAAU;GAAE;GAAO,SAAS;EAAW;EACjD,MAAM,SAAS,OAAO,KAAK;CAC7B,GAAG;EAAC;EAAQ;EAAY;CAAK,CAAC;CAC9B,MAAM,aAAa,kBAAkB,MAAM,SAAS,WAAW,KAAK,MAAM,CAAC,CAAC;CAC5E,MAAM,UAAU,UAAU,UAAU,SAAS,UAAU,SAAS,UAAU,QAAQ,YAAY;CAC9F,IAAI,WAAW,MAAM,UAAU,UAAU,MAAM,MAAM;CACrD,MAAM,UAAU,MAAM,SAAS,WAAW;CAC1C,MAAM,SAAS,WAAW,MAAM,UAAU,WAAW,WAAW,MAAM,aAAa,QAAQ,WAAW,UAAU;CAChH,OAAO;EAAE;EAAQ;CAAW;AAC9B"}