Created
September 22, 2018 16:15
-
-
Save shellcode/d6a3bad1079c03e0dce559ec6aace041 to your computer and use it in GitHub Desktop.
Cake Factory
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
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