refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
1 lines • 3.98 kB
Source Map (JSON)
{"version":3,"sources":["map-throttled-handler.ts"],"names":["useRef","useEffect","requestAnimationFrame","cancelAnimationFrame","UNDEFINED","EMPTY_ARRAY","isFunction","mapThrottledHandlerFactory","setFn","clearFn","handlerName","setFnArgs","props","timerId","throttledHandlerRef","onUnmountRef","currentPropsHandlerRef","handlerArgsRef","current","timeoutCallback","args","mapThrottledHandlerTimeout","setTimeout","clearTimeout","mapThrottledHandlerAnimationFrame"],"mappings":";;AAAA,SAASA,MAAT,EAAiBC,SAAjB,QAAkC,OAAlC;AACA,SAASC,qBAAT,EAAgCC,oBAAhC,EAAsDC,SAAtD,EAAiEC,WAAjE,EAA8EC,UAA9E,QAAgG,MAAhG;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,GAAGb,MAAM,CAAM,IAAN,CAAtB;AACA,UAAMc,mBAAmB,GAAGd,MAAM,EAAlC;AACA,UAAMe,YAAY,GAAGf,MAAM,EAA3B;AACA,UAAMgB,sBAAsB,GAAGhB,MAAM,EAArC;AACA,UAAMiB,cAAc,GAAGjB,MAAM,CAAQK,WAAR,CAA7B;AAEAW,MAAAA,sBAAsB,CAACE,OAAvB,GAAiCN,KAAK,CAACF,WAAD,CAAtC;;AAEA,UAAII,mBAAmB,CAACI,OAApB,KAAgCd,SAApC,EAA+C;AAC7C,YAAMe,eAAe,GAAG,SAAlBA,eAAkB,GAAM;AAC5BN,UAAAA,OAAO,CAACK,OAAR,GAAkB,IAAlB;;AAEA,cAAIZ,UAAU,CAACU,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;AAAA,6CAAhBE,IAAgB;AAAhBA,YAAAA,IAAgB;AAAA;;AAChDH,UAAAA,cAAc,CAACC,OAAf,GAAyBE,IAAzB;;AAEA,cAAIP,OAAO,CAACK,OAAR,KAAoB,IAApB,IAA4BZ,UAAU,CAACU,sBAAsB,CAACE,OAAxB,CAA1C,EAA4E;AAC1EL,YAAAA,OAAO,CAACK,OAAR,GAAkBV,KAAK,MAAL,UAAMW,eAAN,SAA0BR,SAA1B,EAAlB;AACD;AACF,SAND;;AAQAI,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;;AAEDjB,MAAAA,SAAS,CAACc,YAAY,CAACG,OAAd,EAAwBb,WAAxB,CAAT;AAEA,+BACKO,KADL,sBAEGF,WAFH,EAEiBI,mBAAmB,CAACI,OAFrC;AAID,KA1CH;AAAA,GADwC;AAAA,CAAnC;AA6CP,OAAO,IAAMG,0BAA0B,GAAGd,0BAA0B,CAACe,UAAD,EAAaC,YAAb,CAA7D;AACP,OAAO,IAAMC,iCAAiC,GAAGjB,0BAA0B,CAACL,qBAAD,EAAwBC,oBAAxB,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"}