| 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 args = new List(2); | 16 var args = new List(2); |
| 17 args[0] = seed; | 17 args[0] = seed; |
| 18 args[1] = length; | 18 args[1] = length; |
| 19 return _servicePort.call(args).then((result) { | 19 ReceivePort receivePort = new ReceivePort(); |
| 20 _servicePort.send(args, receivePort.sendPort); |
| 21 return receivePort.first.then((result) { |
| 22 receivePort.close(); |
| 20 if (result != null) { | 23 if (result != null) { |
| 21 return result; | 24 return result; |
| 22 } else { | 25 } else { |
| 23 throw new Exception("Random array creation failed"); | 26 throw new Exception("Random array creation failed"); |
| 24 } | 27 } |
| 25 }); | 28 }); |
| 26 } | 29 } |
| 27 | 30 |
| 28 SendPort get _servicePort { | 31 SendPort get _servicePort { |
| 29 if (_port == null) { | 32 if (_port == null) { |
| 30 _port = _newServicePort(); | 33 _port = _newServicePort(); |
| 31 } | 34 } |
| 32 return _port; | 35 return _port; |
| 33 } | 36 } |
| 34 | 37 |
| 35 SendPort _newServicePort() native "RandomArray_ServicePort"; | 38 SendPort _newServicePort() native "RandomArray_ServicePort"; |
| 36 } | 39 } |
| OLD | NEW |