Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Future & Stream in Dart
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Tomochika Hara
July 27, 2013
Technology
490
0
Share
Future & Stream in Dart
2013 Future & Stream in Dart
Tomochika Hara
July 27, 2013
More Decks by Tomochika Hara
See All by Tomochika Hara
Swiftでつくるファミコンエミュレータのススメ
thara
3
2.4k
モバイルゲーム開発と Google App Engine
thara
0
390
GCPとAWSの比較
thara
0
130
Dart VM と Optional Typing
thara
0
410
Dart in なごやまつり
thara
0
61
Dartの基本
thara
0
76
Dart言語仕様 Pick-up
thara
0
630
Other Decks in Technology
See All in Technology
Babylon.js Japan Activities (2026/4)
limes2018
0
200
AI環境整備はどのくらい開発生産性を変えうるか? #AI駆動開発 #AI自走環境
ucchi0909
0
110
Discordでリモートポケカしてたら、なぜかDOを25分間動かせるようになった話
umireon
0
110
Oracle AI Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
6
1.4k
BIツール「Omni」の紹介 @Snowflake中部UG
sagara
0
260
システムは「動く」だけでは足りない 実装編 - 非機能要件・分散システム・トレードオフをコードで見る
nwiizo
1
260
プロダクトを触って語って理解する、チーム横断バグバッシュのすすめ / 20260411 Naoki Takahashi
shift_evolve
PRO
1
250
New CBs New Challenges
ysuzuki
1
160
サイバーフィジカル社会とは何か / What Is a Cyber-Physical Society?
ks91
PRO
0
160
プロンプトエンジニアリングを超えて:自由と統制のあいだでつくる Platform × Context Engineering
yuriemori
0
120
制約を設計する - 非決定性との境界線 / Designing constraints
soudai
PRO
6
2.4k
AIを活用したアクセシビリティ改善フロー
degudegu2510
1
160
Featured
See All Featured
The Curse of the Amulet
leimatthew05
1
11k
Into the Great Unknown - MozCon
thekraken
40
2.3k
Why Our Code Smells
bkeepers
PRO
340
58k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.6k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
330
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
So, you think you're a good person
axbom
PRO
2
2k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
160
The Language of Interfaces
destraynor
162
26k
Balancing Empowerment & Direction
lara
5
1k
A Soul's Torment
seathinner
6
2.6k
Transcript
Future & Stream in Dart tomochikahara @zetta1985
Agenda • Future API • Stream API
Future API for Ansynchronous Programming
What is a Future? • 「先物」 ◦ 別名Promise。 ◦ 処理結果を必要となる時点まで保持する
◦ 並列処理のデザインパターン ▪ Dartでは非同期なだけで並列処理は必須でない • Futureの値を決めるCompleter ◦ Futureはあくまでも値を保持するのみ ◦ 計算結果をFutureに与えるのがCompleter • dart:io, indexed_dbなどで利用
Future & Completer Completer Future Future Caller Callee Future<String> echoToMountain(String
yo_ho); call echoToMountain create get Future return Future then((reply) => catchMyEar(reply)) complete(reply) Execution
Future & Completer import "dart:async"; Future<String> echoToMountain(String yo_ho) { var
completer = new Completer<String>(); new Timer(new Duration(milliseconds: 5000), () { completer.complete(yo_ho); }); return completer.future; } void main() { print("before echoToMountain"); var future = echoToMountain("YooooHooooo"); future.then(print); print("after echoToMountain"); }
Future Example new File("./foo.txt").fullPath() .then((fullpath) { print("foo.txt's fullpath is $fullpath");
}) .catchError((e) { print("foo.txt is not exists."); }); File IO (Async try-catch) HTTP Request (Async Process Chain) var uri = Uri.parse("http://tomochikahara.com"); new HttpClient().getUrl(uri) .then((HttpClientRequest req){ // prepare request req.headers.add("contentType", "text/html"); return req.close(); }).then((HttpClientResponse res) { res.transform(new StringDecoder()).forEach(print); });
Stream API for sequence of any data
What is a Stream? • イベントの「流れ」 ◦ 単なる遅延リスト。 ◦ 何回も使えるFuture。
◦ 値が発すること=イベント、と表現 ◦ Producer - Consumer Pattern ▪ Dartでは非同期なだけで並列処理は必須でない • DOM EventやByte IO、標準入力などで利用
Stream What is a Stream? Controller (Sink) Subscription D C
B' A' ・・・ Transformer F E add listen
Stream var controller = new StreamController<String>(); controller.add("A"); var stream =
controller.stream; stream.listen((str) => print(str), onError : (e) => print("Error"), onDone : () => print("Done")); controller.add("B"); controller.add("C"); controller.addError(new StateError("Illegal State")); controller.add("D"); controller.close();
Stream Subscription var controller = new StreamController<String>(); controller.add("A"); var stream
= controller.stream; var subscription = stream.listen((str) => print(str), onDone : () => print("Done")); subscription.onError((e) { subscription.cancel(); }); controller.add("B"); controller.add("C"); controller.addError(new StateError("Illegal State")); controller.add("D"); controller.close();
Stream Transformer var controller = new StreamController<int>(); var stream =
controller.stream; var transformer = new StreamTransformer<int, String>( handleData : (int value, EventSink<String> sink) { sink.add("$value:(1)"); sink.add("$value:(2)"); }, handleDone : (EventSink<String> sink) { sink.add("Done!"); } ); stream.transform(transformer).forEach(print); for (var i = 0; i < 10; i++) { controller.add(i); } controller.close();
Stream Example query("input#hello").onClick.listen((MouseEvent e) { window.alert("hello, world."); }); DOM Event
File Access StreamSubscription<List<int>> subscription; subscription = new File('bar.txt').openRead().listen((bytes) { for (var b in bytes) { print(new String.fromCharCode(b)); if (b == '#'.codeUnitAt(0)) { subscription.cancel(); return; } } });
Stream Example 標準入力 HTTP Server HttpServer.bind("127.0.0.1", 8080).then((HttpServer server) { server.listen((HttpRequest
req) { req.response.write("This message from my server."); req.response.close(); }); }); print("Please Input A."); var stream = stdin .transform(new StringDecoder()) .transform(new LineTransformer()); stream.listen((str){ print("Your input's length : ${str.length}"); print(str == "A" ? "Just A." : "Not A."); });