UNPKG

refun

Version:

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

1 lines 3.94 kB
{"version":3,"sources":["map-debounced-handler.ts"],"names":["useRef","useEffect","isFunction","UNDEFINED","EMPTY_ARRAY","mapDebouncedHandlerFactory","setFn","clearFn","handlerName","setFnArgs","props","timerId","debouncedHandlerRef","onUnmountRef","currentPropsHandlerRef","handlerArgsRef","current","timeoutCallback","args","mapDebouncedHandlerTimeout","setTimeout","clearTimeout"],"mappings":";;AAAA,SAASA,MAAT,EAAiBC,SAAjB,QAAkC,OAAlC;AACA,SAASC,UAAT,EAAqBC,SAArB,EAAgCC,WAAhC,QAAmD,MAAnD;AAEA,OAAO,IAAMC,0BAA0B,GAAG,SAA7BA,0BAA6B,CAACC,KAAD,EAAkBC,OAAlB;AAAA,SACxC,UAAgBC,WAAhB;AAAA,sCAAyCC,SAAzC;AAAyCA,MAAAA,SAAzC;AAAA;;AAAA,WACE,UAACC,KAAD,EAAiB;AACf,UAAMC,OAAO,GAAGX,MAAM,CAAgB,IAAhB,CAAtB;AACA,UAAMY,mBAAmB,GAAGZ,MAAM,EAAlC;AACA,UAAMa,YAAY,GAAGb,MAAM,EAA3B;AACA,UAAMc,sBAAsB,GAAGd,MAAM,EAArC;AACA,UAAMe,cAAc,GAAGf,MAAM,CAAQI,WAAR,CAA7B;AAEAU,MAAAA,sBAAsB,CAACE,OAAvB,GAAiCN,KAAK,CAACF,WAAD,CAAtC;;AAEA,UAAII,mBAAmB,CAACI,OAApB,KAAgCb,SAApC,EAA+C;AAC7C,YAAMc,eAAe,GAAG,SAAlBA,eAAkB,GAAM;AAC5BN,UAAAA,OAAO,CAACK,OAAR,GAAkB,IAAlB;;AAEA,cAAId,UAAU,CAACY,sBAAsB,CAACE,OAAxB,CAAd,EAAgD;AAC9CF,YAAAA,sBAAsB,CAACE,OAAvB,OAAAF,sBAAsB,qBAAYC,cAAc,CAACC,OAA3B,EAAtB;AACD;AACF,SAND;;AAQAJ,QAAAA,mBAAmB,CAACI,OAApB,GAA8B,YAAoB;AAChD,cAAIL,OAAO,CAACK,OAAR,KAAoB,IAAxB,EAA8B;AAC5BT,YAAAA,OAAO,CAACI,OAAO,CAACK,OAAT,CAAP;AAEAL,YAAAA,OAAO,CAACK,OAAR,GAAkB,IAAlB;AACD;;AAEDD,UAAAA,cAAc,CAACC,OAAf,GAAyBZ,WAAzB;;AAEA,cAAIF,UAAU,CAACY,sBAAsB,CAACE,OAAxB,CAAd,EAAgD;AAAA,+CAThBE,IASgB;AAThBA,cAAAA,IASgB;AAAA;;AAC9CH,YAAAA,cAAc,CAACC,OAAf,GAAyBE,IAAzB;AACAP,YAAAA,OAAO,CAACK,OAAR,GAAkBV,KAAK,MAAL,UAAMW,eAAN,SAA0BR,SAA1B,EAAlB;AACD;AACF,SAbD;;AAeAI,QAAAA,YAAY,CAACG,OAAb,GAAuB;AAAA,iBAAM,YAAM;AACjC,gBAAIL,OAAO,CAACK,OAAR,KAAoB,IAAxB,EAA8B;AAC5BT,cAAAA,OAAO,CAACI,OAAO,CAACK,OAAT,CAAP;AAEAL,cAAAA,OAAO,CAACK,OAAR,GAAkB,IAAlB;AACD;AACF,WANsB;AAAA,SAAvB;AAOD;;AAEDf,MAAAA,SAAS,CAACY,YAAY,CAACG,OAAd,EAAwBZ,WAAxB,CAAT;AAEA,+BACKM,KADL,sBAEGF,WAFH,EAEiBI,mBAAmB,CAACI,OAFrC;AAID,KAjDH;AAAA,GADwC;AAAA,CAAnC;AAoDP,OAAO,IAAMG,0BAA0B,GAAGd,0BAA0B,CAACe,UAAD,EAAaC,YAAb,CAA7D","sourcesContent":["import { useRef, useEffect } from 'react'\nimport { isFunction, UNDEFINED, EMPTY_ARRAY } from 'tsfn'\n\nexport const mapDebouncedHandlerFactory = (setFn: Function, clearFn: Function) =>\n <P extends {}> (handlerName: keyof P, ...setFnArgs: any[]) =>\n (props: P): P => {\n const timerId = useRef<number | null>(null)\n const debouncedHandlerRef = useRef<(...args: any[]) => void>()\n const onUnmountRef = useRef<() => () => void>()\n const currentPropsHandlerRef = useRef<P[keyof P]>()\n const handlerArgsRef = useRef<any[]>(EMPTY_ARRAY)\n\n currentPropsHandlerRef.current = props[handlerName]\n\n if (debouncedHandlerRef.current === UNDEFINED) {\n const timeoutCallback = () => {\n timerId.current = null\n\n if (isFunction(currentPropsHandlerRef.current)) {\n currentPropsHandlerRef.current(...handlerArgsRef.current)\n }\n }\n\n debouncedHandlerRef.current = (...args: any[]) => {\n if (timerId.current !== null) {\n clearFn(timerId.current)\n\n timerId.current = null\n }\n\n handlerArgsRef.current = EMPTY_ARRAY\n\n if (isFunction(currentPropsHandlerRef.current)) {\n handlerArgsRef.current = args\n timerId.current = setFn(timeoutCallback, ...setFnArgs)\n }\n }\n\n onUnmountRef.current = () => () => {\n if (timerId.current !== null) {\n clearFn(timerId.current)\n\n timerId.current = null\n }\n }\n }\n\n useEffect(onUnmountRef.current!, EMPTY_ARRAY)\n\n return {\n ...props,\n [handlerName]: debouncedHandlerRef.current,\n }\n }\n\nexport const mapDebouncedHandlerTimeout = mapDebouncedHandlerFactory(setTimeout, clearTimeout)\n"],"file":"map-debounced-handler.js"}