UNPKG

refun

Version:

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

1 lines • 3.78 kB
{"version":3,"sources":["map-debounced-handler.ts"],"names":["mapDebouncedHandlerFactory","setFn","clearFn","handlerName","setFnArgs","props","timerId","debouncedHandlerRef","onUnmountRef","currentPropsHandlerRef","handlerArgsRef","EMPTY_ARRAY","current","UNDEFINED","timeoutCallback","args","mapDebouncedHandlerTimeout","setTimeout","clearTimeout"],"mappings":";;;;;;;;;;;;;AAAA;;AACA;;AAEO,IAAMA,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,GAAG,mBAAsB,IAAtB,CAAhB;AACA,UAAMC,mBAAmB,GAAG,oBAA5B;AACA,UAAMC,YAAY,GAAG,oBAArB;AACA,UAAMC,sBAAsB,GAAG,oBAA/B;AACA,UAAMC,cAAc,GAAG,mBAAcC,iBAAd,CAAvB;AAEAF,MAAAA,sBAAsB,CAACG,OAAvB,GAAiCP,KAAK,CAACF,WAAD,CAAtC;;AAEA,UAAII,mBAAmB,CAACK,OAApB,KAAgCC,eAApC,EAA+C;AAC7C,YAAMC,eAAe,GAAG,SAAlBA,eAAkB,GAAM;AAC5BR,UAAAA,OAAO,CAACM,OAAR,GAAkB,IAAlB;;AAEA,cAAI,sBAAWH,sBAAsB,CAACG,OAAlC,CAAJ,EAAgD;AAC9CH,YAAAA,sBAAsB,CAACG,OAAvB,OAAAH,sBAAsB,mCAAYC,cAAc,CAACE,OAA3B,EAAtB;AACD;AACF,SAND;;AAQAL,QAAAA,mBAAmB,CAACK,OAApB,GAA8B,YAAoB;AAChD,cAAIN,OAAO,CAACM,OAAR,KAAoB,IAAxB,EAA8B;AAC5BV,YAAAA,OAAO,CAACI,OAAO,CAACM,OAAT,CAAP;AAEAN,YAAAA,OAAO,CAACM,OAAR,GAAkB,IAAlB;AACD;;AAEDF,UAAAA,cAAc,CAACE,OAAf,GAAyBD,iBAAzB;;AAEA,cAAI,sBAAWF,sBAAsB,CAACG,OAAlC,CAAJ,EAAgD;AAAA,+CAThBG,IASgB;AAThBA,cAAAA,IASgB;AAAA;;AAC9CL,YAAAA,cAAc,CAACE,OAAf,GAAyBG,IAAzB;AACAT,YAAAA,OAAO,CAACM,OAAR,GAAkBX,KAAK,MAAL,UAAMa,eAAN,SAA0BV,SAA1B,EAAlB;AACD;AACF,SAbD;;AAeAI,QAAAA,YAAY,CAACI,OAAb,GAAuB;AAAA,iBAAM,YAAM;AACjC,gBAAIN,OAAO,CAACM,OAAR,KAAoB,IAAxB,EAA8B;AAC5BV,cAAAA,OAAO,CAACI,OAAO,CAACM,OAAT,CAAP;AAEAN,cAAAA,OAAO,CAACM,OAAR,GAAkB,IAAlB;AACD;AACF,WANsB;AAAA,SAAvB;AAOD;;AAED,4BAAUJ,YAAY,CAACI,OAAvB,EAAiCD,iBAAjC;AAEA,wCACKN,KADL,oCAEGF,WAFH,EAEiBI,mBAAmB,CAACK,OAFrC;AAID,KAjDH;AAAA,GADwC;AAAA,CAAnC;;;AAoDA,IAAMI,0BAA0B,GAAGhB,0BAA0B,CAACiB,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"}