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-ext:sample_extension"); | 7 #import("dart-ext:sample_extension"); |
8 | 8 |
9 // A class caches the native port used to call an asynchronous extension. | 9 // A class caches the native port used to call an asynchronous extension. |
10 class RandomArray { | 10 class RandomArray { |
11 static SendPort _port; | 11 static SendPort _port; |
12 | 12 |
13 void randomArray(int seed, int length, void callback(List result)) { | 13 void randomArray(int seed, int length, void callback(List result)) { |
14 var args = new List(2); | 14 var args = new List(2); |
15 args[0] = seed; | 15 args[0] = seed; |
16 args[1] = length; | 16 args[1] = length; |
17 _servicePort.call(args).then((result) { | 17 _servicePort.call(args).then((result) { |
18 if (result != null) { | 18 if (result != null) { |
19 callback(result); | 19 callback(result); |
20 } else { | 20 } else { |
21 throw new Exception("Random array creation failed"); | 21 throw new Exception("Random array creation failed"); |
22 } | 22 } |
23 }); | 23 }); |
24 } | 24 } |
25 | 25 |
26 SendPort get _servicePort() { | 26 SendPort get _servicePort { |
27 if (_port == null) { | 27 if (_port == null) { |
28 _port = _newServicePort(); | 28 _port = _newServicePort(); |
29 } | 29 } |
30 return _port; | 30 return _port; |
31 } | 31 } |
32 | 32 |
33 SendPort _newServicePort() native "RandomArray_ServicePort"; | 33 SendPort _newServicePort() native "RandomArray_ServicePort"; |
34 } | 34 } |
OLD | NEW |