clock.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var clock_exports = {};
  20. __export(clock_exports, {
  21. Clock: () => Clock
  22. });
  23. module.exports = __toCommonJS(clock_exports);
  24. class Clock {
  25. constructor(browserContext) {
  26. this._browserContext = browserContext;
  27. }
  28. async install(options = {}) {
  29. await this._browserContext._channel.clockInstall(options.time !== void 0 ? parseTime(options.time) : {});
  30. }
  31. async fastForward(ticks) {
  32. await this._browserContext._channel.clockFastForward(parseTicks(ticks));
  33. }
  34. async pauseAt(time) {
  35. await this._browserContext._channel.clockPauseAt(parseTime(time));
  36. }
  37. async resume() {
  38. await this._browserContext._channel.clockResume({});
  39. }
  40. async runFor(ticks) {
  41. await this._browserContext._channel.clockRunFor(parseTicks(ticks));
  42. }
  43. async setFixedTime(time) {
  44. await this._browserContext._channel.clockSetFixedTime(parseTime(time));
  45. }
  46. async setSystemTime(time) {
  47. await this._browserContext._channel.clockSetSystemTime(parseTime(time));
  48. }
  49. }
  50. function parseTime(time) {
  51. if (typeof time === "number")
  52. return { timeNumber: time };
  53. if (typeof time === "string")
  54. return { timeString: time };
  55. if (!isFinite(time.getTime()))
  56. throw new Error(`Invalid date: ${time}`);
  57. return { timeNumber: time.getTime() };
  58. }
  59. function parseTicks(ticks) {
  60. return {
  61. ticksNumber: typeof ticks === "number" ? ticks : void 0,
  62. ticksString: typeof ticks === "string" ? ticks : void 0
  63. };
  64. }
  65. // Annotate the CommonJS export names for ESM import in node:
  66. 0 && (module.exports = {
  67. Clock
  68. });