refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
50 lines (43 loc) • 1.95 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import { useRef, useEffect } from 'react';
import { requestAnimationFrame, cancelAnimationFrame, UNDEFINED, EMPTY_ARRAY, EMPTY_OBJECT, NOOP } from 'tsfn';
export var mapSafeRequestAnimationFrameFactory = function mapSafeRequestAnimationFrameFactory(requestAnimationFrameFn, cancelAnimationFrameFn) {
return function (propName) {
return function (props) {
var timerIdsRef = useRef(EMPTY_OBJECT);
var setSafeRafRef = useRef();
var useEffectFnRef = useRef();
if (setSafeRafRef.current === UNDEFINED) {
timerIdsRef.current = new Set();
setSafeRafRef.current = function (cb) {
// check if component has been unmounted
if (timerIdsRef.current === EMPTY_OBJECT) {
return NOOP;
}
var timerId = requestAnimationFrameFn(function () {
timerIdsRef.current.delete(timerId);
cb();
});
timerIdsRef.current.add(timerId);
return function () {
cancelAnimationFrameFn(timerId);
timerIdsRef.current.delete(timerId);
};
};
useEffectFnRef.current = function () {
return function () {
timerIdsRef.current.forEach(function (id) {
return cancelAnimationFrameFn(id);
});
timerIdsRef.current.clear(); // indicates that component has been unmounted
timerIdsRef.current = EMPTY_OBJECT;
};
};
}
useEffect(useEffectFnRef.current, EMPTY_ARRAY); // FIXME https://github.com/microsoft/TypeScript/issues/13948
return Object.assign({}, props, _defineProperty({}, propName, setSafeRafRef.current));
};
};
};
export var mapSafeRequestAnimationFrame = mapSafeRequestAnimationFrameFactory(requestAnimationFrame, cancelAnimationFrame);
//# sourceMappingURL=map-safe-request-animation-frame.js.map