-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Making a stream transformer involves a lot of boilerplate #27740
Comments
You can use StreamTransformer.fromHandlers to easily create transformers that just convert input events to output events. Example: new StreamTransformer.fromHandlers(handleData: (String event, EventSink output) {
if (event.startsWith('data:')) {
output.add(JSON.decode(event.substring('data:'.length)));
} else if (event.isNotEmpty) {
output.addError('Unexpected data from CloudBit stream: "$event"');
}
}); |
Turns out the code I have in the original comment won't work at all because you just can't extend StreamTransformer at all. The documentation for StreamTransformer:
The docs for fromHandlers should explain:
|
That is reasonable. It's clearly under-documented. (The |
Ah, yes, I was confused by the other classes around here and thought this one had some implementation logic you would want to inherit. |
It's cleaner to use a class when you have long-lived state (the alternative is jumping through hoops to get the closure to capture the state you want to keep, which can be somewhat ugly). In fact in doing the work I'm doing now, I rather wish there was a class that implemented the fromHandlers stuff so I could inherit from that rather than using fromHandlers... |
...though I guess since StreamTransformer wants to be re-usable, that doesn't really work here anyway. |
Edit: this comment is wrong, see below.
As far as I can tell, the simplest way to write a StreamTransformer is something like this (untested):It seems like the above could be simplified to:...whereFooStreamTransformer
is a subclass ofStreamTransformer
that does all the boilerplate logic.(Alternatively, if the code above is wrong, then the dartdocs for StreamTransformer should be expanded to explain how to actually implement a StreamTransformer.)
The text was updated successfully, but these errors were encountered: