版博士V2.0程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

62 lignes
2.0 KiB

  1. import { getSafeTimers } from '@vitest/utils';
  2. import { g as getWorkerState } from './chunk-utils-global.fd174983.js';
  3. const { get } = Reflect;
  4. const safeRandom = Math.random;
  5. function withSafeTimers(fn) {
  6. const { setTimeout, clearTimeout, nextTick, setImmediate, clearImmediate } = getSafeTimers();
  7. const currentSetTimeout = globalThis.setTimeout;
  8. const currentClearTimeout = globalThis.clearTimeout;
  9. const currentRandom = globalThis.Math.random;
  10. const currentNextTick = globalThis.process.nextTick;
  11. const currentSetImmediate = globalThis.setImmediate;
  12. const currentClearImmediate = globalThis.clearImmediate;
  13. try {
  14. globalThis.setTimeout = setTimeout;
  15. globalThis.clearTimeout = clearTimeout;
  16. globalThis.Math.random = safeRandom;
  17. globalThis.process.nextTick = nextTick;
  18. globalThis.setImmediate = setImmediate;
  19. globalThis.clearImmediate = clearImmediate;
  20. const result = fn();
  21. return result;
  22. } finally {
  23. globalThis.setTimeout = currentSetTimeout;
  24. globalThis.clearTimeout = currentClearTimeout;
  25. globalThis.Math.random = currentRandom;
  26. globalThis.setImmediate = currentSetImmediate;
  27. globalThis.clearImmediate = currentClearImmediate;
  28. nextTick(() => {
  29. globalThis.process.nextTick = currentNextTick;
  30. });
  31. }
  32. }
  33. const promises = /* @__PURE__ */ new Set();
  34. const rpcDone = async () => {
  35. if (!promises.size)
  36. return;
  37. const awaitable = Array.from(promises);
  38. return Promise.all(awaitable);
  39. };
  40. const rpc = () => {
  41. const { rpc: rpc2 } = getWorkerState();
  42. return new Proxy(rpc2, {
  43. get(target, p, handler) {
  44. const sendCall = get(target, p, handler);
  45. const safeSendCall = (...args) => withSafeTimers(async () => {
  46. const result = sendCall(...args);
  47. promises.add(result);
  48. try {
  49. return await result;
  50. } finally {
  51. promises.delete(result);
  52. }
  53. });
  54. safeSendCall.asEvent = sendCall.asEvent;
  55. return safeSendCall;
  56. }
  57. });
  58. };
  59. export { rpcDone as a, rpc as r };