File size: 1,257 Bytes
56fda74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use client';

import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import useLazyRef from '../useLazyRef/useLazyRef';
import useOnMount from '../useOnMount/useOnMount';
export var Timeout = /*#__PURE__*/function () {
  function Timeout() {
    var _this = this;
    _classCallCheck(this, Timeout);
    this.currentId = null;
    this.clear = function () {
      if (_this.currentId !== null) {
        clearTimeout(_this.currentId);
        _this.currentId = null;
      }
    };
    this.disposeEffect = function () {
      return _this.clear;
    };
  }
  _createClass(Timeout, [{
    key: "start",
    value:
    /**
     * Executes `fn` after `delay`, clearing any previously scheduled call.
     */
    function start(delay, fn) {
      var _this2 = this;
      this.clear();
      this.currentId = setTimeout(function () {
        _this2.currentId = null;
        fn();
      }, delay);
    }
  }], [{
    key: "create",
    value: function create() {
      return new Timeout();
    }
  }]);
  return Timeout;
}();
export default function useTimeout() {
  var timeout = useLazyRef(Timeout.create).current;
  useOnMount(timeout.disposeEffect);
  return timeout;
}