OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library sample_asynchronous_extension; | 5 library sample_asynchronous_extension; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
9 import 'dart-ext:sample_extension'; | 9 import 'dart-ext:sample_extension'; |
10 | 10 |
11 // A class caches the native port used to call an asynchronous extension. | 11 // A class caches the native port used to call an asynchronous extension. |
12 class RandomArray { | 12 class RandomArray { |
13 static SendPort _port; | 13 static SendPort _port; |
14 | 14 |
15 Future<List<int> > randomArray(int seed, int length) { | 15 Future<List<int>> randomArray(int seed, int length) { |
16 var completer = new Completer(); | 16 var completer = new Completer(); |
17 var replyPort = new RawReceivePort(); | 17 var replyPort = new RawReceivePort(); |
18 var args = new List(3); | 18 var args = new List(3); |
19 args[0] = seed; | 19 args[0] = seed; |
20 args[1] = length; | 20 args[1] = length; |
21 args[2] = replyPort.sendPort; | 21 args[2] = replyPort.sendPort; |
22 _servicePort.send(args); | 22 _servicePort.send(args); |
23 replyPort.handler = (result) { | 23 replyPort.handler = (result) { |
24 replyPort.close(); | 24 replyPort.close(); |
25 if (result != null) { | 25 if (result != null) { |
26 completer.complete(result); | 26 completer.complete(result); |
27 } else { | 27 } else { |
28 completer.completeError(new Exception("Random array creation failed")); | 28 completer.completeError(new Exception("Random array creation failed")); |
29 } | 29 } |
30 }; | 30 }; |
31 return completer.future; | 31 return completer.future; |
32 } | 32 } |
33 | 33 |
34 SendPort get _servicePort { | 34 SendPort get _servicePort { |
35 if (_port == null) { | 35 if (_port == null) { |
36 _port = _newServicePort(); | 36 _port = _newServicePort(); |
37 } | 37 } |
38 return _port; | 38 return _port; |
39 } | 39 } |
40 | 40 |
41 SendPort _newServicePort() native "RandomArray_ServicePort"; | 41 SendPort _newServicePort() native "RandomArray_ServicePort"; |
42 } | 42 } |
OLD | NEW |