Skip to content

Instantly share code, notes, and snippets.

@shellcode
Created September 22, 2018 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shellcode/d6a3bad1079c03e0dce559ec6aace041 to your computer and use it in GitHub Desktop.
Save shellcode/d6a3bad1079c03e0dce559ec6aace041 to your computer and use it in GitHub Desktop.
Cake Factory
import 'dart:async';
class Cake {
}
class Order {
String type;
Order(this.type);
}
void main(){
final controller = new StreamController();
final order = new Order('chocolate');
// Create the factory to be used later.
final baker = new StreamTransformer.fromHandlers(
handleData: (cakeType, sink){
// inspect the cakeType
if (cakeType == 'chocolate'){
sink.add(new Cake());
/* Can add multiple orders
sink.add(new Cake());
sink.add(new Cake());
*/
} else {
sink.addError('I cant bake that type!!!');
}
}
);
// Pull off the data from the order
controller.stream
// Order Inspector
.map((order) => order.type)
// Give order to the Baker
.transform(baker)
// Pickup the output
.listen(
(cake) => print('Heres your cake $cake'),
onError: (err) => print(err)
);
// Receive the order
controller.sink.add(order);
controller.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment