main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | var tslib_1 = require("tslib");
|
---|
| 4 | var react_1 = require("react");
|
---|
| 5 | var useUnmount_1 = tslib_1.__importDefault(require("./useUnmount"));
|
---|
| 6 | var useThrottleFn = function (fn, ms, args) {
|
---|
| 7 | if (ms === void 0) { ms = 200; }
|
---|
| 8 | var _a = react_1.useState(null), state = _a[0], setState = _a[1];
|
---|
| 9 | var timeout = react_1.useRef();
|
---|
| 10 | var nextArgs = react_1.useRef();
|
---|
| 11 | react_1.useEffect(function () {
|
---|
| 12 | if (!timeout.current) {
|
---|
| 13 | setState(fn.apply(void 0, args));
|
---|
| 14 | var timeoutCallback_1 = function () {
|
---|
| 15 | if (nextArgs.current) {
|
---|
| 16 | setState(fn.apply(void 0, nextArgs.current));
|
---|
| 17 | nextArgs.current = undefined;
|
---|
| 18 | timeout.current = setTimeout(timeoutCallback_1, ms);
|
---|
| 19 | }
|
---|
| 20 | else {
|
---|
| 21 | timeout.current = undefined;
|
---|
| 22 | }
|
---|
| 23 | };
|
---|
| 24 | timeout.current = setTimeout(timeoutCallback_1, ms);
|
---|
| 25 | }
|
---|
| 26 | else {
|
---|
| 27 | nextArgs.current = args;
|
---|
| 28 | }
|
---|
| 29 | }, args);
|
---|
| 30 | useUnmount_1.default(function () {
|
---|
| 31 | timeout.current && clearTimeout(timeout.current);
|
---|
| 32 | });
|
---|
| 33 | return state;
|
---|
| 34 | };
|
---|
| 35 | exports.default = useThrottleFn;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.