|
1 | 1 | 'use strict';
|
2 | 2 | (function () {
|
| 3 | + // Fake setInterval-like functionality in environments that don't have it |
| 4 | + class IntervalHandle { |
| 5 | + constructor(callback, delayMs) { |
| 6 | + this.callback = callback; |
| 7 | + this.delayMs = delayMs; |
| 8 | + this.cancelled = false; |
| 9 | + Promise.resolve().then(() => this.check()); |
| 10 | + } |
| 11 | + |
| 12 | + async check() { |
| 13 | + while (true) { |
| 14 | + await new Promise(resolve => step_timeout(resolve, this.delayMs)); |
| 15 | + if (this.cancelled) { |
| 16 | + return; |
| 17 | + } |
| 18 | + this.callback(); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + cancel() { |
| 23 | + this.cancelled = true; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + let localSetInterval, localClearInterval; |
| 28 | + if (typeof globalThis.setInterval !== "undefined" && |
| 29 | + typeof globalThis.clearInterval !== "undefined") { |
| 30 | + localSetInterval = globalThis.setInterval; |
| 31 | + localClearInterval = globalThis.clearInterval; |
| 32 | + } else { |
| 33 | + localSetInterval = function setInterval(callback, delayMs) { |
| 34 | + return new IntervalHandle(callback, delayMs); |
| 35 | + } |
| 36 | + localClearInterval = function clearInterval(handle) { |
| 37 | + handle.cancel(); |
| 38 | + } |
| 39 | + } |
3 | 40 |
|
4 | 41 | class RandomPushSource {
|
5 | 42 | constructor(toPush) {
|
|
18 | 55 | }
|
19 | 56 |
|
20 | 57 | if (!this.started) {
|
21 |
| - this._intervalHandle = setInterval(writeChunk, 2); |
| 58 | + this._intervalHandle = localSetInterval(writeChunk, 2); |
22 | 59 | this.started = true;
|
23 | 60 | }
|
24 | 61 |
|
25 | 62 | if (this.paused) {
|
26 |
| - this._intervalHandle = setInterval(writeChunk, 2); |
| 63 | + this._intervalHandle = localSetInterval(writeChunk, 2); |
27 | 64 | this.paused = false;
|
28 | 65 | }
|
29 | 66 |
|
|
37 | 74 |
|
38 | 75 | if (source.toPush > 0 && source.pushed > source.toPush) {
|
39 | 76 | if (source._intervalHandle) {
|
40 |
| - clearInterval(source._intervalHandle); |
| 77 | + localClearInterval(source._intervalHandle); |
41 | 78 | source._intervalHandle = undefined;
|
42 | 79 | }
|
43 | 80 | source.closed = true;
|
|
55 | 92 |
|
56 | 93 | if (this.started) {
|
57 | 94 | this.paused = true;
|
58 |
| - clearInterval(this._intervalHandle); |
| 95 | + localClearInterval(this._intervalHandle); |
59 | 96 | this._intervalHandle = undefined;
|
60 | 97 | } else {
|
61 | 98 | throw new Error('Can\'t pause reading an unstarted source.');
|
|
178 | 215 | }
|
179 | 216 |
|
180 | 217 | function transferArrayBufferView(view) {
|
181 |
| - const noopByteStream = new ReadableStream({ |
182 |
| - type: 'bytes', |
183 |
| - pull(c) { |
184 |
| - c.byobRequest.respond(c.byobRequest.view.byteLength); |
185 |
| - c.close(); |
186 |
| - } |
187 |
| - }); |
188 |
| - const reader = noopByteStream.getReader({ mode: 'byob' }); |
189 |
| - return reader.read(view).then((result) => result.value); |
| 218 | + return structuredClone(view, { transfer: [view.buffer] }); |
190 | 219 | }
|
191 | 220 |
|
192 | 221 | self.RandomPushSource = RandomPushSource;
|
|
0 commit comments