Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes to readable-streams tests for ShadowRealm #48653

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion streams/readable-streams/owning-type-video-frame.any.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// META: global=window,worker,shadowrealm
// META: global=window,worker
// META: script=../resources/test-utils.js
// META: script=../resources/rs-utils.js
'use strict';
Expand Down
45 changes: 41 additions & 4 deletions streams/resources/rs-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
'use strict';
(function () {
// Fake setInterval-like functionality in environments that don't have it
class IntervalHandle {
constructor(callback, delayMs) {
this.callback = callback;
this.delayMs = delayMs;
this.cancelled = false;
Promise.resolve().then(() => this.check());
}

async check() {
while (true) {
await new Promise(resolve => step_timeout(resolve, this.delayMs));
if (this.cancelled) {
return;
}
this.callback();
}
}

cancel() {
this.cancelled = true;
}
}

let localSetInterval, localClearInterval;
if (typeof globalThis.setInterval !== "undefined" &&
typeof globalThis.clearInterval !== "undefined") {
localSetInterval = globalThis.setInterval;
localClearInterval = globalThis.clearInterval;
} else {
localSetInterval = function setInterval(callback, delayMs) {
return new IntervalHandle(callback, delayMs);
}
localClearInterval = function clearInterval(handle) {
handle.cancel();
}
}

class RandomPushSource {
constructor(toPush) {
Expand All @@ -18,12 +55,12 @@
}

if (!this.started) {
this._intervalHandle = setInterval(writeChunk, 2);
this._intervalHandle = localSetInterval(writeChunk, 2);
this.started = true;
}

if (this.paused) {
this._intervalHandle = setInterval(writeChunk, 2);
this._intervalHandle = localSetInterval(writeChunk, 2);
this.paused = false;
}

Expand All @@ -37,7 +74,7 @@

if (source.toPush > 0 && source.pushed > source.toPush) {
if (source._intervalHandle) {
clearInterval(source._intervalHandle);
localClearInterval(source._intervalHandle);
source._intervalHandle = undefined;
}
source.closed = true;
Expand All @@ -55,7 +92,7 @@

if (this.started) {
this.paused = true;
clearInterval(this._intervalHandle);
localClearInterval(this._intervalHandle);
this._intervalHandle = undefined;
} else {
throw new Error('Can\'t pause reading an unstarted source.');
Expand Down