OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:json_rpc_2/json_rpc_2.dart' as rpc; | 7 import 'package:json_rpc_2/json_rpc_2.dart' as rpc; |
8 | 8 |
9 import 'error.dart'; | 9 import 'error.dart'; |
10 import 'exceptions.dart'; | 10 import 'exceptions.dart'; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 /// `false`. | 96 /// `false`. |
97 /// | 97 /// |
98 /// Meanwhile, [onEvent] is called with every event from [stream] that matches | 98 /// Meanwhile, [onEvent] is called with every event from [stream] that matches |
99 /// this scope's isolate. It should *synchronously* return `null` or `false` | 99 /// this scope's isolate. It should *synchronously* return `null` or `false` |
100 /// for irrelevant events; once it sees a relevant event, it should return a | 100 /// for irrelevant events; once it sees a relevant event, it should return a |
101 /// non-`null`, non-`false` value (which may be a Future). | 101 /// non-`null`, non-`false` value (which may be a Future). |
102 /// | 102 /// |
103 /// This returns the value returned by [immediate] if it's not `null` or | 103 /// This returns the value returned by [immediate] if it's not `null` or |
104 /// `false`, or else the first non-`null`, non-`false` value returned by | 104 /// `false`, or else the first non-`null`, non-`false` value returned by |
105 /// [onEvent]. | 105 /// [onEvent]. |
106 Future/*<T>*/ getInState/*<T>*/(Stream<Map> stream, Future/*<T>*/ immediate(), | 106 Future<T> getInState<T>(Stream<Map> stream, Future<T> immediate(), |
107 onEvent(Map json)) async { | 107 onEvent(Map json)) async { |
108 var completer = new Completer.sync(); | 108 var completer = new Completer.sync(); |
109 | 109 |
110 // Don't top-level errors from the completer. These may come in from the | 110 // Don't top-level errors from the completer. These may come in from the |
111 // stream subscription while we're still waiting for `load()` to complete. | 111 // stream subscription while we're still waiting for `load()` to complete. |
112 completer.future.catchError((_) {}); | 112 completer.future.catchError((_) {}); |
113 | 113 |
114 // To avoid a race condition, we need to make sure we're listening to the | 114 // To avoid a race condition, we need to make sure we're listening to the |
115 // event stream before we load the current state, so that if the object | 115 // event stream before we load the current state, so that if the object |
116 // reaches the desired state during the load we'll see it. | 116 // reaches the desired state during the load we'll see it. |
(...skipping 23 matching lines...) Expand all Loading... |
140 | 140 |
141 var result = await immediate(); | 141 var result = await immediate(); |
142 if (result != null && result != false) { | 142 if (result != null && result != false) { |
143 subscription.cancel(); | 143 subscription.cancel(); |
144 return result; | 144 return result; |
145 } | 145 } |
146 | 146 |
147 return await completer.future; | 147 return await completer.future; |
148 } | 148 } |
149 } | 149 } |
OLD | NEW |