@rohal12/spindle 0.62.2 → 0.62.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -36,6 +36,56 @@ async function loadOptional(pkg, feature, importer = defaultImporter) {
36
36
  }
37
37
  }
38
38
  //#endregion
39
+ //#region src/play/window-events.ts
40
+ /** Expose the story's global window at the DOM event boundary. */
41
+ function installWindowEvents(window) {
42
+ const storyWindow = globalThis;
43
+ const wrapped = /* @__PURE__ */ new WeakMap();
44
+ const originals = /* @__PURE__ */ new WeakMap();
45
+ const wrap = (listener) => {
46
+ if (typeof listener !== "function") return listener;
47
+ let callback = wrapped.get(listener);
48
+ if (!callback) {
49
+ callback = listener.bind(storyWindow);
50
+ wrapped.set(listener, callback);
51
+ originals.set(callback, listener);
52
+ }
53
+ return callback;
54
+ };
55
+ const { addEventListener, removeEventListener } = window;
56
+ window.addEventListener = (type, listener, options) => addEventListener(type, wrap(listener), options);
57
+ window.removeEventListener = (type, listener) => removeEventListener(type, wrapped.get(listener) ?? listener);
58
+ for (const name of Object.getOwnPropertyNames(window)) {
59
+ const descriptor = Object.getOwnPropertyDescriptor(window, name);
60
+ if (!name.startsWith("on") || !descriptor.get || !descriptor.set) continue;
61
+ Object.defineProperty(window, name, {
62
+ ...descriptor,
63
+ get() {
64
+ const listener = descriptor.get.call(window);
65
+ return originals.get(listener) ?? listener;
66
+ },
67
+ set(listener) {
68
+ descriptor.set.call(window, wrap(listener));
69
+ }
70
+ });
71
+ }
72
+ const exposeWindow = (target) => target === window ? storyWindow : target;
73
+ const prototype = window.Event.prototype;
74
+ for (const name of ["target", "currentTarget"]) {
75
+ const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
76
+ Object.defineProperty(prototype, name, {
77
+ ...descriptor,
78
+ get() {
79
+ return exposeWindow(descriptor.get.call(this));
80
+ }
81
+ });
82
+ }
83
+ const { composedPath } = prototype;
84
+ prototype.composedPath = function() {
85
+ return composedPath.call(this).map((target) => target === window ? storyWindow : target);
86
+ };
87
+ }
88
+ //#endregion
39
89
  //#region src/play/worker.ts
40
90
  /**
41
91
  * The worker thread of a play session: boots the story on happy-dom and
@@ -49,30 +99,68 @@ Object.defineProperty(process, "stdout", {
49
99
  });
50
100
  var port = parentPort;
51
101
  var data = workerData;
52
- /** Make happy-dom's window the global environment Spindle runs in. */
102
+ /** Install happy-dom's DOM in the global environment Spindle runs in. */
53
103
  async function installDom() {
54
104
  const { Window } = await loadOptional("happy-dom", "play sessions");
55
105
  const window = new Window({ url: "http://localhost/" });
106
+ installWindowEvents(window);
56
107
  const global = globalThis;
57
108
  const source = window;
58
- global.window = window;
59
109
  global.document = window.document;
60
- for (const name of Object.getOwnPropertyNames(window)) if (!(name in globalThis)) global[name] = source[name];
110
+ for (const name of Object.getOwnPropertyNames(window)) if (!(name in globalThis)) Object.defineProperty(globalThis, name, Object.getOwnPropertyDescriptor(window, name));
61
111
  for (const name of HAPPY_DOM_GLOBALS) Object.defineProperty(globalThis, name, {
62
112
  configurable: true,
63
113
  writable: true,
64
114
  value: source[name]
65
115
  });
66
- Object.defineProperty(globalThis, "BroadcastChannel", {
116
+ for (const name of [
117
+ "window",
118
+ "self",
119
+ "parent",
120
+ "top",
121
+ "frames"
122
+ ]) Object.defineProperty(globalThis, name, {
67
123
  configurable: true,
68
124
  writable: true,
69
- value: void 0
125
+ value: globalThis
70
126
  });
71
- Object.defineProperty(globalThis, "Story", {
127
+ Object.defineProperty(window, "Story", {
72
128
  configurable: true,
73
- get: () => source.Story
129
+ get: () => global.Story,
130
+ set: (value) => {
131
+ global.Story = value;
132
+ }
133
+ });
134
+ installIdleCallbacks();
135
+ Object.defineProperty(globalThis, "BroadcastChannel", {
136
+ configurable: true,
137
+ writable: true,
138
+ value: void 0
74
139
  });
75
140
  }
141
+ /** happy-dom has no idle scheduler; yield to a timer with a 50 ms budget. */
142
+ function installIdleCallbacks() {
143
+ const timers = /* @__PURE__ */ new Map();
144
+ let nextHandle = 0;
145
+ globalThis.requestIdleCallback = (callback, options) => {
146
+ const handle = ++nextHandle;
147
+ const requested = performance.now();
148
+ timers.set(handle, setTimeout(() => {
149
+ timers.delete(handle);
150
+ const started = performance.now();
151
+ const didTimeout = options?.timeout !== void 0 && started - requested >= options.timeout;
152
+ callback({
153
+ didTimeout,
154
+ timeRemaining: () => didTimeout ? 0 : Math.max(0, 50 - (performance.now() - started))
155
+ });
156
+ }, 1));
157
+ return handle;
158
+ };
159
+ globalThis.cancelIdleCallback = (handle) => {
160
+ clearTimeout(timers.get(handle));
161
+ timers.delete(handle);
162
+ };
163
+ }
76
164
  /**
77
165
  * Globals happy-dom's window replaces even where Node has its own:
78
166
  * - the event classes: happy-dom's dispatchEvent() refuses events made with
package/dist/pkg/play.js CHANGED
@@ -192,9 +192,9 @@ function workerExecArgv(execArgv) {
192
192
  return kept;
193
193
  }
194
194
  /**
195
- * Opens a session that runs the story in a worker. There `window` is
196
- * happy-dom's window, not `globalThis`: story code sees `Story` and the DOM
197
- * globals, but `window.foo = 1` does not make a bare global `foo`.
195
+ * Opens a session that runs the story in a worker with happy-dom's DOM.
196
+ * `window` is `globalThis`: properties authored on it are bare globals too.
197
+ * Idle callbacks use a timer-backed scheduler.
198
198
  * Requires `trusted: true`: story code has unrestricted host Node access.
199
199
  */
200
200
  function openPlaySession(options) {