refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
1 lines • 3.42 kB
Source Map (JSON)
{"version":3,"sources":["map-safe-timeout.ts"],"names":["mapSafeTimeoutFactory","setTimeoutFn","clearTimeoutFn","propName","props","timerIdsRef","EMPTY_OBJECT","setSafeTimeoutRef","useEffectFnRef","current","UNDEFINED","Set","cb","delay","NOOP","timerId","delete","add","forEach","id","clear","EMPTY_ARRAY","mapSafeTimeout","setTimeout","clearTimeout"],"mappings":";;;;;;;;;;;AAAA;;AACA;;AAKO,IAAMA,qBAAqB,GAAG,SAAxBA,qBAAwB,CAACC,YAAD,EAAyBC,cAAzB;AAAA,SACnC,UAAmBC,QAAnB;AAAA,WAAmC,UAAeC,KAAf,EAAuE;AACxG,UAAMC,WAAW,GAAG,mBAAiBC,kBAAjB,CAApB;AACA,UAAMC,iBAAiB,GAAG,oBAA1B;AACA,UAAMC,cAAc,GAAG,oBAAvB;;AAEA,UAAID,iBAAiB,CAACE,OAAlB,KAA8BC,eAAlC,EAA6C;AAC3CL,QAAAA,WAAW,CAACI,OAAZ,GAAsB,IAAIE,GAAJ,EAAtB;;AAEAJ,QAAAA,iBAAiB,CAACE,OAAlB,GAA4B,UAACG,EAAD,EAAKC,KAAL,EAAe;AAEzC,cAAIR,WAAW,CAACI,OAAZ,KAAwBH,kBAA5B,EAA0C;AACxC,mBAAOQ,UAAP;AACD;;AAED,cAAMC,OAAO,GAAGd,YAAY,CAAC,YAAM;AACjCI,YAAAA,WAAW,CAACI,OAAZ,CAAoBO,MAApB,CAA2BD,OAA3B;AACAH,YAAAA,EAAE;AACH,WAH2B,EAGzBC,KAHyB,CAA5B;AAKAR,UAAAA,WAAW,CAACI,OAAZ,CAAoBQ,GAApB,CAAwBF,OAAxB;AAEA,iBAAO,YAAM;AACXb,YAAAA,cAAc,CAACa,OAAD,CAAd;AACAV,YAAAA,WAAW,CAACI,OAAZ,CAAoBO,MAApB,CAA2BD,OAA3B;AACD,WAHD;AAID,SAjBD;;AAmBAP,QAAAA,cAAc,CAACC,OAAf,GAAyB;AAAA,iBAAM,YAAM;AACnCJ,YAAAA,WAAW,CAACI,OAAZ,CAAoBS,OAApB,CAA4B,UAACC,EAAD;AAAA,qBAAQjB,cAAc,CAACiB,EAAD,CAAtB;AAAA,aAA5B;AACAd,YAAAA,WAAW,CAACI,OAAZ,CAAoBW,KAApB;AAEAf,YAAAA,WAAW,CAACI,OAAZ,GAAsBH,kBAAtB;AACD,WALwB;AAAA,SAAzB;AAMD;;AAED,4BAAUE,cAAc,CAACC,OAAzB,EAAmCY,iBAAnC;AAGA,wCACKjB,KADL,oCAEGD,QAFH,EAEcI,iBAAiB,CAACE,OAFhC;AAID,KA1CD;AAAA,GADmC;AAAA,CAA9B;;;AA6CA,IAAMa,cAAc,GAAGtB,qBAAqB,CAACuB,UAAD,EAAaC,YAAb,CAA5C","sourcesContent":["import { useRef, useEffect } from 'react'\nimport { UNDEFINED, EMPTY_OBJECT, EMPTY_ARRAY, NOOP } from 'tsfn'\nimport type { TExtend } from 'tsfn'\n\nexport type TSetSafeTimeout = (cb: () => void, delay: number) => () => void\n\nexport const mapSafeTimeoutFactory = (setTimeoutFn: Function, clearTimeoutFn: Function) =>\n <K extends string>(propName: K) => <P extends {}>(props: P): TExtend<P, { [k in K]: TSetSafeTimeout }> => {\n const timerIdsRef = useRef<Set<any>>(EMPTY_OBJECT)\n const setSafeTimeoutRef = useRef<TSetSafeTimeout>()\n const useEffectFnRef = useRef<() => () => void>()\n\n if (setSafeTimeoutRef.current === UNDEFINED) {\n timerIdsRef.current = new Set()\n\n setSafeTimeoutRef.current = (cb, delay) => {\n // check if component has been unmounted\n if (timerIdsRef.current === EMPTY_OBJECT) {\n return NOOP\n }\n\n const timerId = setTimeoutFn(() => {\n timerIdsRef.current.delete(timerId)\n cb()\n }, delay)\n\n timerIdsRef.current.add(timerId)\n\n return () => {\n clearTimeoutFn(timerId)\n timerIdsRef.current.delete(timerId)\n }\n }\n\n useEffectFnRef.current = () => () => {\n timerIdsRef.current.forEach((id) => clearTimeoutFn(id))\n timerIdsRef.current.clear()\n // indicates that component has been unmounted\n timerIdsRef.current = EMPTY_OBJECT\n }\n }\n\n useEffect(useEffectFnRef.current!, EMPTY_ARRAY)\n\n // FIXME https://github.com/microsoft/TypeScript/issues/13948\n return {\n ...props,\n [propName]: setSafeTimeoutRef.current,\n } as any\n }\n\nexport const mapSafeTimeout = mapSafeTimeoutFactory(setTimeout, clearTimeout)\n"],"file":"map-safe-timeout.js"}