OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Dart core library. | 5 // Dart core library. |
6 | 6 |
7 /** A promise to value of type [T] that may be computed asynchronously. */ | 7 /** A promise to value of type [T] that may be computed asynchronously. */ |
8 interface Promise<T> factory PromiseImpl<T> { | 8 interface Promise<T> factory PromiseImpl<T> { |
9 | 9 |
10 Promise(); | 10 Promise(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 void join(Collection<Promise<T>> arr, bool joinDone(Promise<T> completed)); | 83 void join(Collection<Promise<T>> arr, bool joinDone(Promise<T> completed)); |
84 | 84 |
85 /** | 85 /** |
86 * Mark this promise as complete when [n] promises in [arr] complete, then | 86 * Mark this promise as complete when [n] promises in [arr] complete, then |
87 * cancel the rest of the promises in [arr] that didn't complete. | 87 * cancel the rest of the promises in [arr] that didn't complete. |
88 */ | 88 */ |
89 void waitFor(Collection<Promise> arr, int n); | 89 void waitFor(Collection<Promise> arr, int n); |
90 } | 90 } |
91 | 91 |
92 | 92 |
93 class Proxy extends ProxyImpl { | 93 interface Proxy factory ProxyImpl { |
94 | 94 |
95 Proxy.forPort(SendPort port) | 95 Proxy.forPort(SendPort port); |
| 96 Proxy.forIsolate(Isolate isolate); |
| 97 Proxy._forIsolateWithPromise(Isolate isolate, Promise<SendPort> promise); |
| 98 /* |
| 99 * The [Proxy.forReply] constructor is used to create a proxy for |
| 100 * the object that will be the reply to a message send. |
| 101 */ |
| 102 Proxy.forReply(Promise<SendPort> port); |
| 103 |
| 104 } |
| 105 |
| 106 |
| 107 class ProxyImpl extends ProxyBase implements Proxy { |
| 108 |
| 109 ProxyImpl.forPort(SendPort port) |
96 : super.forPort(port) { } | 110 : super.forPort(port) { } |
97 | 111 |
98 Proxy.forIsolate(Isolate isolate) | 112 ProxyImpl.forIsolate(Isolate isolate) |
99 : this._forIsolateWithPromise(isolate, new Promise<SendPort>()); | 113 : this._forIsolateWithPromise(isolate, new Promise<SendPort>()); |
100 | 114 |
101 Proxy._forIsolateWithPromise(Isolate isolate, Promise<SendPort> promise) | 115 ProxyImpl._forIsolateWithPromise(Isolate isolate, Promise<SendPort> promise) |
102 // TODO(floitsch): it seems wrong to call super.forReply here. | 116 // TODO(floitsch): it seems wrong to call super.forReply here. |
103 : super.forReply(promise) { | 117 : super.forReply(promise) { |
104 isolate.spawn().then((SendPort port) { | 118 isolate.spawn().then((SendPort port) { |
105 promise.complete(port); | 119 promise.complete(port); |
106 }); | 120 }); |
107 } | 121 } |
108 | 122 |
109 /* | 123 /* |
110 * The [Proxy.forReply] constructor is used to create a proxy for | 124 * The [Proxy.forReply] constructor is used to create a proxy for |
111 * the object that will be the reply to a message send. | 125 * the object that will be the reply to a message send. |
112 */ | 126 */ |
113 Proxy.forReply(Promise<SendPort> port) | 127 ProxyImpl.forReply(Promise<SendPort> port) |
114 : super.forReply(port) { } | 128 : super.forReply(port) { } |
115 | 129 |
116 } | 130 } |
117 | 131 |
118 | 132 |
119 class Dispatcher<T> { | 133 class Dispatcher<T> { |
120 | 134 |
121 Dispatcher(this.target) { } | 135 Dispatcher(this.target) { } |
122 | 136 |
123 void _serve(ReceivePort port) { | 137 void _serve(ReceivePort port) { |
124 port.receive((var message, SendPort replyTo) { | 138 port.receive((var message, SendPort replyTo) { |
125 this.process(message, void reply(var response) { | 139 this.process(message, void reply(var response) { |
126 Proxy proxy = new Proxy.forPort(replyTo); | 140 Proxy proxy = new Proxy.forPort(replyTo); |
127 proxy.send([response]); | 141 proxy.send([response]); |
128 }); | 142 }); |
129 }); | 143 }); |
130 } | 144 } |
131 | 145 |
132 static SendPort serve(Dispatcher dispatcher) { | 146 static SendPort serve(Dispatcher dispatcher) { |
133 ReceivePort port = ProxyImpl.register(dispatcher); | 147 ReceivePort port = ProxyBase.register(dispatcher); |
134 dispatcher._serve(port); | 148 dispatcher._serve(port); |
135 return port.toSendPort(); | 149 return port.toSendPort(); |
136 } | 150 } |
137 | 151 |
138 // BUG(5015671): DartC doesn't support 'abstract' yet. | 152 // BUG(5015671): DartC doesn't support 'abstract' yet. |
139 /* abstract */ void process(var message, void reply(var response)) { | 153 /* abstract */ void process(var message, void reply(var response)) { |
140 throw "Abstract method called"; | 154 throw "Abstract method called"; |
141 } | 155 } |
142 | 156 |
143 T target; | 157 T target; |
144 | 158 |
145 } | 159 } |
146 | 160 |
147 // When a promise is sent across a port, it is converted to a | 161 // When a promise is sent across a port, it is converted to a |
148 // Promise<SendPort> down which we must send a port to receive the | 162 // Promise<SendPort> down which we must send a port to receive the |
149 // completion value. Hand the Promise<SendPort> to this class to deal | 163 // completion value. Hand the Promise<SendPort> to this class to deal |
150 // with it. | 164 // with it. |
151 | 165 |
152 class PromiseProxy<T> extends PromiseImpl<T> { | 166 class PromiseProxy<T> extends PromiseImpl<T> { |
153 PromiseProxy(Promise<SendPort> sendCompleter) { | 167 PromiseProxy(Promise<SendPort> sendCompleter) { |
154 ReceivePort completer = new ReceivePort.singleShot(); | 168 ReceivePort completer = new ReceivePort.singleShot(); |
155 completer.receive((var msg, SendPort _) { | 169 completer.receive((var msg, SendPort _) { |
156 complete(msg[0]); | 170 complete(msg[0]); |
157 }); | 171 }); |
158 sendCompleter.addCompleteHandler((SendPort port) { | 172 sendCompleter.addCompleteHandler((SendPort port) { |
159 port.send([completer.toSendPort()], null); | 173 port.send([completer.toSendPort()], null); |
160 }); | 174 }); |
161 } | 175 } |
162 } | 176 } |
OLD | NEW |