mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:47:34 +00:00
LibWeb: Implement the ReadableStreamDefaultTee half of ReadableStreamTee
This commit is contained in:
parent
d8413774df
commit
debfe996d7
4 changed files with 371 additions and 2 deletions
|
@ -0,0 +1,56 @@
|
|||
<script src="../include.js"></script>
|
||||
<script>
|
||||
const CHUNK1 = "abcdefghijklmnopqrstuvwxyz";
|
||||
const CHUNK2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const CHUNK3 = "0123456789!@#$%^&*()-=_+,<";
|
||||
|
||||
const readStream = (stream, name) => {
|
||||
const reader = stream.getReader();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const processText = ({ done, value }) => {
|
||||
if (done) {
|
||||
println(`${name}: Done!`);
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
println(`${name}: ${value}`);
|
||||
|
||||
return reader.read().then(processText);
|
||||
};
|
||||
|
||||
reader.read().then(processText);
|
||||
});
|
||||
};
|
||||
|
||||
asyncTest(done => {
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
pullCount = 0;
|
||||
},
|
||||
|
||||
pull(controller) {
|
||||
++pullCount;
|
||||
|
||||
if (pullCount == 1) {
|
||||
controller.enqueue(CHUNK1);
|
||||
} else if (pullCount == 2) {
|
||||
controller.enqueue(CHUNK2);
|
||||
} else if (pullCount == 3) {
|
||||
controller.enqueue(CHUNK3);
|
||||
} else {
|
||||
controller.close();
|
||||
}
|
||||
},
|
||||
|
||||
cancel() {},
|
||||
});
|
||||
|
||||
const teed = stream.tee();
|
||||
|
||||
readStream(teed[0], "stream1").then(() => {
|
||||
readStream(teed[1], "stream2").then(done);
|
||||
});
|
||||
});
|
||||
</script>
|
Loading…
Add table
Add a link
Reference in a new issue