mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-26 17:55:07 +02:00
LibWeb: Implement the ReadableByteStreamTee half of ReadableStreamTee
This commit is contained in:
committed by
Andreas Kling
parent
d7612969e0
commit
6981ddfe13
Notes:
sideshowbarker
2024-07-17 08:34:29 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/6981ddfe13 Pull-request: https://github.com/SerenityOS/serenity/pull/22977 Reviewed-by: https://github.com/shannonbooth ✅
@@ -0,0 +1,64 @@
|
||||
<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;
|
||||
}
|
||||
|
||||
value = new TextDecoder().decode(value);
|
||||
println(`${name}: ${value}`);
|
||||
|
||||
return reader.read().then(processText);
|
||||
};
|
||||
|
||||
reader.read().then(processText);
|
||||
});
|
||||
};
|
||||
|
||||
const writeStream = (controller, data) => {
|
||||
const source = Uint8Array.from(Array.from(data).map(ch => ch.charCodeAt(0)));
|
||||
controller.enqueue(source);
|
||||
};
|
||||
|
||||
asyncTest(done => {
|
||||
const stream = new ReadableStream({
|
||||
type: "bytes",
|
||||
|
||||
start(controller) {
|
||||
pullCount = 0;
|
||||
},
|
||||
|
||||
pull(controller) {
|
||||
++pullCount;
|
||||
|
||||
if (pullCount == 1) {
|
||||
writeStream(controller, CHUNK1);
|
||||
} else if (pullCount == 2) {
|
||||
writeStream(controller, CHUNK2);
|
||||
} else if (pullCount == 3) {
|
||||
writeStream(controller, CHUNK3);
|
||||
} else {
|
||||
controller.close();
|
||||
}
|
||||
},
|
||||
|
||||
cancel() {},
|
||||
});
|
||||
|
||||
const teed = stream.tee();
|
||||
|
||||
readStream(teed[0], "stream1").then(() => {
|
||||
readStream(teed[1], "stream2").then(done);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user