Immediate.js 729 B

123456789101112131415161718192021222324252627282930
  1. let nextHandle = 1;
  2. let resolved;
  3. const activeHandles = {};
  4. function findAndClearHandle(handle) {
  5. if (handle in activeHandles) {
  6. delete activeHandles[handle];
  7. return true;
  8. }
  9. return false;
  10. }
  11. export const Immediate = {
  12. setImmediate(cb) {
  13. const handle = nextHandle++;
  14. activeHandles[handle] = true;
  15. if (!resolved) {
  16. resolved = Promise.resolve();
  17. }
  18. resolved.then(() => findAndClearHandle(handle) && cb());
  19. return handle;
  20. },
  21. clearImmediate(handle) {
  22. findAndClearHandle(handle);
  23. },
  24. };
  25. export const TestTools = {
  26. pending() {
  27. return Object.keys(activeHandles).length;
  28. }
  29. };
  30. //# sourceMappingURL=Immediate.js.map