refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
50 lines (43 loc) • 1.8 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import { useRef, useEffect } from 'react';
import { UNDEFINED, EMPTY_OBJECT, EMPTY_ARRAY, NOOP } from 'tsfn';
export var mapSafeTimeoutFactory = function mapSafeTimeoutFactory(setTimeoutFn, clearTimeoutFn) {
return function (propName) {
return function (props) {
var timerIdsRef = useRef(EMPTY_OBJECT);
var setSafeTimeoutRef = useRef();
var useEffectFnRef = useRef();
if (setSafeTimeoutRef.current === UNDEFINED) {
timerIdsRef.current = new Set();
setSafeTimeoutRef.current = function (cb, delay) {
// check if component has been unmounted
if (timerIdsRef.current === EMPTY_OBJECT) {
return NOOP;
}
var timerId = setTimeoutFn(function () {
timerIdsRef.current.delete(timerId);
cb();
}, delay);
timerIdsRef.current.add(timerId);
return function () {
clearTimeoutFn(timerId);
timerIdsRef.current.delete(timerId);
};
};
useEffectFnRef.current = function () {
return function () {
timerIdsRef.current.forEach(function (id) {
return clearTimeoutFn(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, setSafeTimeoutRef.current));
};
};
};
export var mapSafeTimeout = mapSafeTimeoutFactory(setTimeout, clearTimeout);
//# sourceMappingURL=map-safe-timeout.js.map