lock-controller-6585a42a.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. 'use strict';
  5. /**
  6. * Creates a lock controller.
  7. *
  8. * Claiming a lock means that nothing else can acquire the lock until it is released.
  9. * This can momentarily prevent execution of code that needs to wait for the earlier code to finish.
  10. * For example, this can be used to prevent multiple transitions from occurring at the same time.
  11. */
  12. const createLockController = () => {
  13. let waitPromise;
  14. /**
  15. * When lock() is called, the lock is claimed.
  16. * Once a lock has been claimed, it cannot be claimed again until it is released.
  17. * When this function gets resolved, the lock is released, allowing it to be claimed again.
  18. *
  19. * @example ```tsx
  20. * const unlock = await this.lockController.lock();
  21. * // do other stuff
  22. * unlock();
  23. * ```
  24. */
  25. const lock = async () => {
  26. const p = waitPromise;
  27. let resolve;
  28. waitPromise = new Promise((r) => (resolve = r));
  29. if (p !== undefined) {
  30. await p;
  31. }
  32. return resolve;
  33. };
  34. return {
  35. lock,
  36. };
  37. };
  38. exports.createLockController = createLockController;