UNPKG

refun

Version:

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

1 lines • 4.08 kB
{"version":3,"sources":["on-update-async.ts"],"names":["onUpdateAsync","onUpdateFn","watchKeys","props","useEffectFnRef","NOOP","onUnmountRef","propsRef","EMPTY_OBJECT","watchValuesRef","EMPTY_ARRAY","isMountedRef","createGeneratorRef","idsRef","current","map","k","generatorId","newId","cancelOthers","switchToGenerator","index","isGeneratorRunning"],"mappings":";;;;;AAAA;;AACA;;AACA;;AAYO,IAAMA,aAAa,GAAG,SAAhBA,aAAgB,CAAgBC,UAAhB,EAAuEC,SAAvE;AAAA,SAAkG,UAACC,KAAD,EAAiB;AAC9I,QAAMC,cAAc,GAAG,mBAAmBC,UAAnB,CAAvB;AACA,QAAMC,YAAY,GAAG,mBAAmBD,UAAnB,CAArB;AACA,QAAME,QAAQ,GAAG,mBAAUC,kBAAV,CAAjB;AACA,QAAMC,cAAc,GAAG,mBAAYC,iBAAZ,CAAvB;AACA,QAAMC,YAAY,GAAG,mBAAO,IAAP,CAArB;AACA,QAAMC,kBAAkB,GAAG,mBAAyBP,UAAzB,CAA3B;AACA,QAAMQ,MAAM,GAAG,mBAA8CL,kBAA9C,CAAf;;AAEA,QAAIC,cAAc,CAACK,OAAf,KAA2BJ,iBAA3B,IAA0C,CAAC,+BAAmBH,QAAQ,CAACO,OAA5B,EAAqCX,KAArC,EAA4CD,SAA5C,CAA/C,EAAuG;AACrGO,MAAAA,cAAc,CAACK,OAAf,GAAyBZ,SAAS,CAACa,GAAV,CAAc,UAACC,CAAD;AAAA,eAAOb,KAAK,CAACa,CAAD,CAAZ;AAAA,OAAd,CAAzB;AACD;;AAEDT,IAAAA,QAAQ,CAACO,OAAT,GAAmBX,KAAnB;;AAEA,QAAIC,cAAc,CAACU,OAAf,KAA2BT,UAA/B,EAAqC;AACnCD,MAAAA,cAAc,CAACU,OAAf,GAAyB,YAAM;AAC7B,YAAIF,kBAAkB,CAACE,OAAnB,KAA+BT,UAAnC,EAAgD;AAC9CO,UAAAA,kBAAkB,CAACE,OAAnB,GAA6Bb,UAAU,CAACM,QAAD,CAAvC;AACD;;AAED,YAAIM,MAAM,CAACC,OAAP,KAAmBN,kBAAvB,EAAqC;AACnCK,UAAAA,MAAM,CAACC,OAAP,GAAiB,gCAAjB;AACD;;AAED,YAAMG,WAAW,GAAGJ,MAAM,CAACC,OAAP,CAAeI,KAAf,EAApB;AAEA,oCACEN,kBAAkB,CAACE,OAAnB,CAA2B;AACzBK,UAAAA,YAAY,EAAEN,MAAM,CAACC,OAAP,CAAeM,iBAAf,CAAiCH,WAAjC,CADW;AAEzBI,UAAAA,KAAK,EAAEJ;AAFkB,SAA3B,CADF,EAKE,YAAM;AACJ,iBAAON,YAAY,CAACG,OAAb,IAAwBD,MAAM,CAACC,OAAP,CAAeQ,kBAAf,CAAkCL,WAAlC,CAA/B;AACD,SAPH;AASD,OApBD;AAqBD;;AAED,0BAAUb,cAAc,CAACU,OAAzB,EAAkCL,cAAc,CAACK,OAAjD;;AAEA,QAAIR,YAAY,CAACQ,OAAb,KAAyBT,UAA7B,EAAmC;AACjCC,MAAAA,YAAY,CAACQ,OAAb,GAAuB;AAAA,eAAM,YAAM;AACjCH,UAAAA,YAAY,CAACG,OAAb,GAAuB,KAAvB;AACD,SAFsB;AAAA,OAAvB;AAGD;;AAED,0BAAUR,YAAY,CAACQ,OAAvB,EAAgCJ,iBAAhC;AAEA,WAAOP,KAAP;AACD,GAlD4B;AAAA,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"}