delay.mjs 988 B

12345678910111213141516171819202122232425262728293031
  1. // Utilities
  2. import { IN_BROWSER, propsFactory } from "../util/index.mjs"; // Types
  3. // Composables
  4. export const makeDelayProps = propsFactory({
  5. closeDelay: [Number, String],
  6. openDelay: [Number, String]
  7. }, 'delay');
  8. export function useDelay(props, cb) {
  9. const delays = {};
  10. const runDelayFactory = prop => () => {
  11. // istanbul ignore next
  12. if (!IN_BROWSER) return Promise.resolve(true);
  13. const active = prop === 'openDelay';
  14. delays.closeDelay && window.clearTimeout(delays.closeDelay);
  15. delete delays.closeDelay;
  16. delays.openDelay && window.clearTimeout(delays.openDelay);
  17. delete delays.openDelay;
  18. return new Promise(resolve => {
  19. const delay = parseInt(props[prop] ?? 0, 10);
  20. delays[prop] = window.setTimeout(() => {
  21. cb?.(active);
  22. resolve(active);
  23. }, delay);
  24. });
  25. };
  26. return {
  27. runCloseDelay: runDelayFactory('closeDelay'),
  28. runOpenDelay: runDelayFactory('openDelay')
  29. };
  30. }
  31. //# sourceMappingURL=delay.mjs.map