useLock.js 820 B

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = useLock;
  6. var _vue = require("vue");
  7. /**
  8. * Locker return cached mark.
  9. * If set to `true`, will return `true` in a short time even if set `false`.
  10. * If set to `false` and then set to `true`, will change to `true`.
  11. * And after time duration, it will back to `null` automatically.
  12. */
  13. function useLock() {
  14. let duration = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 250;
  15. let lock = null;
  16. let timeout;
  17. (0, _vue.onBeforeUnmount)(() => {
  18. clearTimeout(timeout);
  19. });
  20. function doLock(locked) {
  21. if (locked || lock === null) {
  22. lock = locked;
  23. }
  24. clearTimeout(timeout);
  25. timeout = setTimeout(() => {
  26. lock = null;
  27. }, duration);
  28. }
  29. return [() => lock, doLock];
  30. }