Created
July 30, 2022 18:55
-
-
Save jasnell/811803ff59952098ee07b5239bb1ba00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Do this | |
const { writable, readable } = new TransformStream(); | |
const writer = writable.getWriter(); | |
writer.write('something'); | |
writer.write('something'); | |
writer.write('something'); | |
// Here, you can use the writer's `.ready` promise to determine | |
// when backpressure has been relieved. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Or this... (less desirable) | |
let writable; | |
const readable = new ReadableStream({ | |
start(c) { | |
writable = new WritableStream({ | |
write(chunk) { | |
c.enqueue(chunk); | |
} | |
}, { | |
highWaterMark: c.desiredSize | |
); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bad... don't do this | |
let controller; | |
const readable = new ReadableStream({ | |
start(c) { controller = c } | |
}); | |
c.enqueue('something'); | |
c.enqueue('something'); | |
c.enqueue('something'); // etc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment