main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var react_1 = require("react");
|
---|
4 | function useTimeoutFn(fn, ms) {
|
---|
5 | if (ms === void 0) { ms = 0; }
|
---|
6 | var ready = react_1.useRef(false);
|
---|
7 | var timeout = react_1.useRef();
|
---|
8 | var callback = react_1.useRef(fn);
|
---|
9 | var isReady = react_1.useCallback(function () { return ready.current; }, []);
|
---|
10 | var set = react_1.useCallback(function () {
|
---|
11 | ready.current = false;
|
---|
12 | timeout.current && clearTimeout(timeout.current);
|
---|
13 | timeout.current = setTimeout(function () {
|
---|
14 | ready.current = true;
|
---|
15 | callback.current();
|
---|
16 | }, ms);
|
---|
17 | }, [ms]);
|
---|
18 | var clear = react_1.useCallback(function () {
|
---|
19 | ready.current = null;
|
---|
20 | timeout.current && clearTimeout(timeout.current);
|
---|
21 | }, []);
|
---|
22 | // update ref when function changes
|
---|
23 | react_1.useEffect(function () {
|
---|
24 | callback.current = fn;
|
---|
25 | }, [fn]);
|
---|
26 | // set on mount, clear on unmount
|
---|
27 | react_1.useEffect(function () {
|
---|
28 | set();
|
---|
29 | return clear;
|
---|
30 | }, [ms]);
|
---|
31 | return [isReady, clear, set];
|
---|
32 | }
|
---|
33 | exports.default = useTimeoutFn;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.