UNPKG

refun

Version:

A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:

1 lines 4.39 kB
{"version":3,"sources":["on-update-async.ts"],"names":["useRef","useEffect","EMPTY_OBJECT","NOOP","EMPTY_ARRAY","shallowEqualByKeys","unwindGenerator","generatorIdFactory","onUpdateAsync","onUpdateFn","watchKeys","props","useEffectFnRef","onUnmountRef","propsRef","watchValuesRef","isMountedRef","createGeneratorRef","idsRef","current","map","k","generatorId","newId","cancelOthers","switchToGenerator","index","isGeneratorRunning"],"mappings":"AAAA,SAASA,MAAT,EAAiBC,SAAjB,QAAkC,OAAlC;AACA,SAASC,YAAT,EAAuBC,IAAvB,EAA6BC,WAA7B,QAAgD,MAAhD;AACA,SAASC,kBAAT,EAA6BC,eAA7B,EAA8CC,kBAA9C,QAAwE,SAAxE;AAYA,OAAO,IAAMC,aAAa,GAAG,SAAhBA,aAAgB,CAAgBC,UAAhB,EAAuEC,SAAvE;AAAA,SAAkG,UAACC,KAAD,EAAiB;AAC9I,QAAMC,cAAc,GAAGZ,MAAM,CAAaG,IAAb,CAA7B;AACA,QAAMU,YAAY,GAAGb,MAAM,CAAaG,IAAb,CAA3B;AACA,QAAMW,QAAQ,GAAGd,MAAM,CAAIE,YAAJ,CAAvB;AACA,QAAMa,cAAc,GAAGf,MAAM,CAAMI,WAAN,CAA7B;AACA,QAAMY,YAAY,GAAGhB,MAAM,CAAC,IAAD,CAA3B;AACA,QAAMiB,kBAAkB,GAAGjB,MAAM,CAAmBG,IAAnB,CAAjC;AACA,QAAMe,MAAM,GAAGlB,MAAM,CAAwCE,YAAxC,CAArB;;AAEA,QAAIa,cAAc,CAACI,OAAf,KAA2Bf,WAA3B,IAA0C,CAACC,kBAAkB,CAACS,QAAQ,CAACK,OAAV,EAAmBR,KAAnB,EAA0BD,SAA1B,CAAjE,EAAuG;AACrGK,MAAAA,cAAc,CAACI,OAAf,GAAyBT,SAAS,CAACU,GAAV,CAAc,UAACC,CAAD;AAAA,eAAOV,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A,CAAtB","sourcesContent":["import { useRef, useEffect } from 'react'\nimport { EMPTY_OBJECT, NOOP, EMPTY_ARRAY } from 'tsfn'\nimport { shallowEqualByKeys, unwindGenerator, generatorIdFactory } from './utils'\n\ntype TCoroutineProps = {\n cancelOthers: () => void,\n index: number,\n}\n\ntype GeneratorFactory = (props: TCoroutineProps) => Generator<Promise<any>, void, any>\ntype RefObject<T> = {\n current: T,\n}\n\nexport const onUpdateAsync = <P extends {}> (onUpdateFn: (props: RefObject<P>) => GeneratorFactory, watchKeys: (keyof P)[]) => (props: P): P => {\n const useEffectFnRef = useRef<() => void>(NOOP)\n const onUnmountRef = useRef<() => void>(NOOP)\n const propsRef = useRef<P>(EMPTY_OBJECT)\n const watchValuesRef = useRef<any>(EMPTY_ARRAY)\n const isMountedRef = useRef(true)\n const createGeneratorRef = useRef<GeneratorFactory>(NOOP as any)\n const idsRef = useRef<ReturnType<typeof generatorIdFactory>>(EMPTY_OBJECT)\n\n if (watchValuesRef.current === EMPTY_ARRAY || !shallowEqualByKeys(propsRef.current, props, watchKeys)) {\n watchValuesRef.current = watchKeys.map((k) => props[k])\n }\n\n propsRef.current = props\n\n if (useEffectFnRef.current === NOOP) {\n useEffectFnRef.current = () => {\n if (createGeneratorRef.current === NOOP as any) {\n createGeneratorRef.current = onUpdateFn(propsRef)\n }\n\n if (idsRef.current === EMPTY_OBJECT) {\n idsRef.current = generatorIdFactory()\n }\n\n const generatorId = idsRef.current.newId()\n\n unwindGenerator(\n createGeneratorRef.current({\n cancelOthers: idsRef.current.switchToGenerator(generatorId),\n index: generatorId,\n }),\n () => {\n return isMountedRef.current && idsRef.current.isGeneratorRunning(generatorId)\n }\n )\n }\n }\n\n useEffect(useEffectFnRef.current, watchValuesRef.current)\n\n if (onUnmountRef.current === NOOP) {\n onUnmountRef.current = () => () => {\n isMountedRef.current = false\n }\n }\n\n useEffect(onUnmountRef.current, EMPTY_ARRAY)\n\n return props\n}\n"],"file":"on-update-async.js"}