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.4 KB
|
Rev | Line | |
---|
[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | var react_1 = require("react");
|
---|
| 4 | function useRafLoop(callback, initiallyActive) {
|
---|
| 5 | if (initiallyActive === void 0) { initiallyActive = true; }
|
---|
| 6 | var raf = react_1.useRef(null);
|
---|
| 7 | var rafActivity = react_1.useRef(false);
|
---|
| 8 | var rafCallback = react_1.useRef(callback);
|
---|
| 9 | rafCallback.current = callback;
|
---|
| 10 | var step = react_1.useCallback(function (time) {
|
---|
| 11 | if (rafActivity.current) {
|
---|
| 12 | rafCallback.current(time);
|
---|
| 13 | raf.current = requestAnimationFrame(step);
|
---|
| 14 | }
|
---|
| 15 | }, []);
|
---|
| 16 | var result = react_1.useMemo(function () {
|
---|
| 17 | return [
|
---|
| 18 | function () {
|
---|
| 19 | // stop
|
---|
| 20 | if (rafActivity.current) {
|
---|
| 21 | rafActivity.current = false;
|
---|
| 22 | raf.current && cancelAnimationFrame(raf.current);
|
---|
| 23 | }
|
---|
| 24 | },
|
---|
| 25 | function () {
|
---|
| 26 | // start
|
---|
| 27 | if (!rafActivity.current) {
|
---|
| 28 | rafActivity.current = true;
|
---|
| 29 | raf.current = requestAnimationFrame(step);
|
---|
| 30 | }
|
---|
| 31 | },
|
---|
| 32 | function () { return rafActivity.current; },
|
---|
| 33 | ];
|
---|
| 34 | }, []);
|
---|
| 35 | react_1.useEffect(function () {
|
---|
| 36 | if (initiallyActive) {
|
---|
| 37 | result[1]();
|
---|
| 38 | }
|
---|
| 39 | return result[0];
|
---|
| 40 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
---|
| 41 | }, []);
|
---|
| 42 | return result;
|
---|
| 43 | }
|
---|
| 44 | exports.default = useRafLoop;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.