useLock.js 856 B

123456789101112131415161718192021222324252627282930313233
  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. var duration = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 250;
  15. var lock = null;
  16. var timeout;
  17. (0, _vue.onBeforeUnmount)(function () {
  18. clearTimeout(timeout);
  19. });
  20. function doLock(locked) {
  21. if (locked || lock === null) {
  22. lock = locked;
  23. }
  24. clearTimeout(timeout);
  25. timeout = setTimeout(function () {
  26. lock = null;
  27. }, duration);
  28. }
  29. return [function () {
  30. return lock;
  31. }, doLock];
  32. }