|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +exports.useBoolean = void 0; |
| 4 | +const react_1 = require("react"); |
| 5 | +// /** |
| 6 | +// * hook version of interval timer, should behave the same without |
| 7 | +// * the pesky inconvenience of creating a new timer on each frame render |
| 8 | +// * If the passed delay is null or undefined this is a noop |
| 9 | +// * @return void |
| 10 | +// */ |
| 11 | +// export function useInterval(callback: () => void, delay: ?number) { |
| 12 | +// const savedCallback = useRef(); |
| 13 | +// // Remember the latest callback. |
| 14 | +// useEffect(() => { |
| 15 | +// savedCallback.current = callback; |
| 16 | +// }, [callback]); |
| 17 | +// // Set up the interval. |
| 18 | +// useEffect(() => { |
| 19 | +// function tick() { |
| 20 | +// if (savedCallback.current) { |
| 21 | +// savedCallback.current(); |
| 22 | +// } |
| 23 | +// } |
| 24 | +// if (delay != null) { |
| 25 | +// const id = setInterval(tick, delay); |
| 26 | +// return () => clearInterval(id); |
| 27 | +// } |
| 28 | +// }, [delay]); |
| 29 | +// } |
| 30 | +// export function useBackHandler(handler?: () => mixed) { |
| 31 | +// const savedHandler = useRef(null); |
| 32 | +// useEffect(() => { |
| 33 | +// savedHandler.current = handler; |
| 34 | +// }, [handler]); |
| 35 | +// useFocusEffect( |
| 36 | +// useCallback(() => { |
| 37 | +// const eventListener = () => { |
| 38 | +// // There is a problem here; 'hardwareBackPress' requires the function to return "true" |
| 39 | +// // to prevent bubling up of the press event, which we are not enforcing... |
| 40 | +// // eslint-disable-next-line |
| 41 | +// savedHandler.current?.(); |
| 42 | +// }; |
| 43 | +// BackHandler.addEventListener("hardwareBackPress", eventListener); |
| 44 | +// return () => { |
| 45 | +// BackHandler.removeEventListener("hardwareBackPress", eventListener); |
| 46 | +// }; |
| 47 | +// // passing empty dependency array to useCallback ensures focus effect will only run once on mount |
| 48 | +// // and the clean-up function will only run once on unmount as well |
| 49 | +// // wether the internal handler function is correctly updated is another problem |
| 50 | +// }, []) |
| 51 | +// ); |
| 52 | +// } |
| 53 | +function useBoolean(initialVal = false) { |
| 54 | + const [val, setVal] = (0, react_1.useState)(initialVal); |
| 55 | + function setTrue() { |
| 56 | + setVal(true); |
| 57 | + } |
| 58 | + function setFalse() { |
| 59 | + setVal(false); |
| 60 | + } |
| 61 | + function toggle() { |
| 62 | + setVal(!val); |
| 63 | + } |
| 64 | + return [val, setTrue, setFalse, toggle]; |
| 65 | +} |
| 66 | +exports.useBoolean = useBoolean; |
| 67 | +// export function usePolling(fn: () => Promise<unknown>, ms: number) { |
| 68 | +// let timeout = useRef<number | null>(null); |
| 69 | +// useFocusEffect( |
| 70 | +// useCallback(() => { |
| 71 | +// const wrapper = () => { |
| 72 | +// fn() |
| 73 | +// .then(() => { |
| 74 | +// timeout.current = setTimeout(() => { |
| 75 | +// wrapper(); |
| 76 | +// }, ms); |
| 77 | +// }) |
| 78 | +// .catch(() => { |
| 79 | +// timeout.current = setTimeout(() => { |
| 80 | +// wrapper(); |
| 81 | +// }, ms); |
| 82 | +// }); |
| 83 | +// }; |
| 84 | +// wrapper(); |
| 85 | +// return () => { |
| 86 | +// if (timeout.current) { |
| 87 | +// clearTimeout(timeout.current); |
| 88 | +// } |
| 89 | +// }; |
| 90 | +// }, [fn, ms]) |
| 91 | +// ); |
| 92 | +// } |
0 commit comments