UNPKG

refun

Version:

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

1 lines 3.59 kB
{"version":3,"sources":["map-safe-timeout.ts"],"names":["useRef","useEffect","UNDEFINED","EMPTY_OBJECT","EMPTY_ARRAY","NOOP","mapSafeTimeoutFactory","setTimeoutFn","clearTimeoutFn","propName","props","timerIdsRef","setSafeTimeoutRef","useEffectFnRef","current","Set","cb","delay","timerId","delete","add","forEach","id","clear","mapSafeTimeout","setTimeout","clearTimeout"],"mappings":";AAAA,SAASA,MAAT,EAAiBC,SAAjB,QAAkC,OAAlC;AACA,SAASC,SAAT,EAAoBC,YAApB,EAAkCC,WAAlC,EAA+CC,IAA/C,QAA2D,MAA3D;AAKA,OAAO,IAAMC,qBAAqB,GAAG,SAAxBA,qBAAwB,CAACC,YAAD,EAAyBC,cAAzB;AAAA,SACnC,UAAmBC,QAAnB;AAAA,WAAmC,UAAeC,KAAf,EAAuE;AACxG,UAAMC,WAAW,GAAGX,MAAM,CAAWG,YAAX,CAA1B;AACA,UAAMS,iBAAiB,GAAGZ,MAAM,EAAhC;AACA,UAAMa,cAAc,GAAGb,MAAM,EAA7B;;AAEA,UAAIY,iBAAiB,CAACE,OAAlB,KAA8BZ,SAAlC,EAA6C;AAC3CS,QAAAA,WAAW,CAACG,OAAZ,GAAsB,IAAIC,GAAJ,EAAtB;;AAEAH,QAAAA,iBAAiB,CAACE,OAAlB,GAA4B,UAACE,EAAD,EAAKC,KAAL,EAAe;AACzC;AACA,cAAIN,WAAW,CAACG,OAAZ,KAAwBX,YAA5B,EAA0C;AACxC,mBAAOE,IAAP;AACD;;AAED,cAAMa,OAAO,GAAGX,YAAY,CAAC,YAAM;AACjCI,YAAAA,WAAW,CAACG,OAAZ,CAAoBK,MAApB,CAA2BD,OAA3B;AACAF,YAAAA,EAAE;AACH,WAH2B,EAGzBC,KAHyB,CAA5B;AAKAN,UAAAA,WAAW,CAACG,OAAZ,CAAoBM,GAApB,CAAwBF,OAAxB;AAEA,iBAAO,YAAM;AACXV,YAAAA,cAAc,CAACU,OAAD,CAAd;AACAP,YAAAA,WAAW,CAACG,OAAZ,CAAoBK,MAApB,CAA2BD,OAA3B;AACD,WAHD;AAID,SAjBD;;AAmBAL,QAAAA,cAAc,CAACC,OAAf,GAAyB;AAAA,iBAAM,YAAM;AACnCH,YAAAA,WAAW,CAACG,OAAZ,CAAoBO,OAApB,CAA4B,UAACC,EAAD;AAAA,qBAAQd,cAAc,CAACc,EAAD,CAAtB;AAAA,aAA5B;AACAX,YAAAA,WAAW,CAACG,OAAZ,CAAoBS,KAApB,GAFmC,CAGnC;;AACAZ,YAAAA,WAAW,CAACG,OAAZ,GAAsBX,YAAtB;AACD,WALwB;AAAA,SAAzB;AAMD;;AAEDF,MAAAA,SAAS,CAACY,cAAc,CAACC,OAAhB,EAA0BV,WAA1B,CAAT,CAnCwG,CAqCxG;;AACA,+BACKM,KADL,sBAEGD,QAFH,EAEcG,iBAAiB,CAACE,OAFhC;AAID,KA1CD;AAAA,GADmC;AAAA,CAA9B;AA6CP,OAAO,IAAMU,cAAc,GAAGlB,qBAAqB,CAACmB,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"}