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-throttled-handler.ts"],"names":["mapThrottledHandlerFactory","setFn","clearFn","handlerName","setFnArgs","props","timerId","throttledHandlerRef","onUnmountRef","currentPropsHandlerRef","handlerArgsRef","EMPTY_ARRAY","current","UNDEFINED","timeoutCallback","args","mapThrottledHandlerTimeout","setTimeout","clearTimeout","mapThrottledHandlerAnimationFrame","requestAnimationFrame","cancelAnimationFrame"],"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,mBAAY,IAAZ,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;AAAA,6CAAhBG,IAAgB;AAAhBA,YAAAA,IAAgB;AAAA;;AAChDL,UAAAA,cAAc,CAACE,OAAf,GAAyBG,IAAzB;;AAEA,cAAIT,OAAO,CAACM,OAAR,KAAoB,IAApB,IAA4B,sBAAWH,sBAAsB,CAACG,OAAlC,CAAhC,EAA4E;AAC1EN,YAAAA,OAAO,CAACM,OAAR,GAAkBX,KAAK,MAAL,UAAMa,eAAN,SAA0BV,SAA1B,EAAlB;AACD;AACF,SAND;;AAQAI,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,KA1CH;AAAA,GADwC;AAAA,CAAnC;;;AA6CA,IAAMI,0BAA0B,GAAGhB,0BAA0B,CAACiB,UAAD,EAAaC,YAAb,CAA7D;;AACA,IAAMC,iCAAiC,GAAGnB,0BAA0B,CAACoB,2BAAD,EAAwBC,0BAAxB,CAApE","sourcesContent":["import { useRef, useEffect } from 'react'\nimport { requestAnimationFrame, cancelAnimationFrame, UNDEFINED, EMPTY_ARRAY, isFunction } from 'tsfn'\n\nexport const mapThrottledHandlerFactory = (setFn: Function, clearFn: Function) =>\n <P extends {}> (handlerName: keyof P, ...setFnArgs: any[]) =>\n (props: P): P => {\n const timerId = useRef<any>(null)\n const throttledHandlerRef = 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 (throttledHandlerRef.current === UNDEFINED) {\n const timeoutCallback = () => {\n timerId.current = null\n\n if (isFunction(currentPropsHandlerRef.current)) {\n currentPropsHandlerRef.current(...handlerArgsRef.current)\n }\n }\n\n throttledHandlerRef.current = (...args: any[]) => {\n handlerArgsRef.current = args\n\n if (timerId.current === null && isFunction(currentPropsHandlerRef.current)) {\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]: throttledHandlerRef.current,\n }\n }\n\nexport const mapThrottledHandlerTimeout = mapThrottledHandlerFactory(setTimeout, clearTimeout)\nexport const mapThrottledHandlerAnimationFrame = mapThrottledHandlerFactory(requestAnimationFrame, cancelAnimationFrame)\n"],"file":"map-throttled-handler.js"}