-
Notifications
You must be signed in to change notification settings - Fork 161
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
Idea for solving the writev problem #27
Comments
Moving this out of the readme as I'm not sure it's going to make it: CorkableWritableStreamclass CorkableWritableStream extends WritableStream {
// Adds a writev argument.
constructor({
function start = () => {},
function write = () => {},
function writev = () => {},
function close = () => {},
function abort = close,
strategy: { function count, function needsMoreData }
})
// New methods
void cork()
void uncork()
// Overriden to take into account corking
Promise<undefined> write(data)
Promise<undefined> close()
// TODO: internal properties and overrides to support corking
} |
Thinking of real use case, for example, if the sink is network and the WritableStream to the sink accepts ArrayBuffers. We can make the sink accept also an array of ArrayBuffers for batch writing. It might be a problem for the case the data type is also ArrayBuffer for non batch write, but can still do type check or fail on encountering unexpected structure. You need to build an array by yourself but I don't feel like it's so cumbersome compared to calling cork/uncork. |
@tyoshino hmm I think I see what you're saying: something like var polymorphicStream = new WritableStream({
write(data, done, error) {
if (Array.isArray(data)) {
sink.writev(data, () => { /* use done/error */ });
} else {
sink.write(data, () => { /* use done/error */ })
}
}
}); ? In general I kind of hate overloading but you're right that it's a much easier and simpler solution, that only uses existing primitives. Recalling the Node discussion it seems kind of like their option C/D, except we thankfully don't deal with encoding at this level, so that nightmare is gone, and they did writev instead of overloading write. |
@domenic Right. I'm not against cork/uncork. Just tried to look for alternative. Have you considered adding one method say
IIUC, this is still efficient (not introducing any significant delay), and the only change is how cork-ed data is considered to be consumed. I.e., this sends out the I/O vectors to the sink before uncorking happens, so the |
Hmm, sorry. This means that we stop utilizing Streams for buffering cork-ed data. That's not desirable basically. I need to think more. |
Having |
This seems like the job for specific subclasses, especially if it's opaque to the primitive... I am pretty sure putting it in the base class would not gain anything.
I think we have to consider the perspective of the user... corkedWrite + write seems like it would be hard to reason about, since the behavior of write changes. Although, cork + write + uncork has similar problems. The easiest to reason about is probably just a writev([...array...]) form, but then it's tough to use if you want to queue up multiple writes e.g. in a loop before committing them... It's probably important to remember that the primary consumer of this is |
Yeah, it was kinda suggestion to delegate cork stuff to each impl.
Agreed but yeah, to reduce the work by user, I tried to figure out something. |
/cc @Gozala |
CorkableWritableStream
(what a name!) inherits fromWritableStream
(or fromBaseWritableStream
? How do I mix them?). It adds publiccork
anduncork
methods, and a constructor optionwritev
. Thus callingcork
switches you into batch-write mode where calls towrite
get batched, and callinguncork
switches you out of it.Not entirely sure about the compositionality of this approach---I feel like I'm trying to solve too many problems with inheritance, perhaps---but in general, it should be a fairly simple modification of a writable stream to make it corkable.
The text was updated successfully, but these errors were encountered: