OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import "dart:isolate"; | 6 import "dart:isolate"; |
7 | 7 |
8 // This type corresponds to the VM-internal class LibraryPrefix. | 8 // This type corresponds to the VM-internal class LibraryPrefix. |
| 9 class _LibraryPrefix { |
9 | 10 |
10 class _LibraryPrefix { | 11 bool _load() native "LibraryPrefix_load"; |
11 _load() native "LibraryPrefix_load"; | 12 |
| 13 void _invalidateDependentCode() |
| 14 native "LibraryPrefix_invalidateDependentCode"; |
12 | 15 |
13 loadLibrary() { | 16 loadLibrary() { |
14 var completer = new Completer<bool>(); | 17 var completer = _outstandingLoadRequests[this]; |
15 var port = new RawReceivePort(); | 18 if (completer != null) { |
16 port.handler = (_) { | 19 return completer.future; |
17 this._load(); | 20 } |
18 completer.complete(true); | 21 completer = new Completer<bool>(); |
19 port.close(); | 22 _outstandingLoadRequests[this] = completer; |
20 }; | 23 Timer.run(() { |
21 port.sendPort.send(1); | 24 var hasCompleted = this._load(); |
| 25 // Loading can complete immediately, for example when the same |
| 26 // library has been loaded eagerly or through another deferred |
| 27 // prefix. If that is the case, we must invalidate the dependent |
| 28 // code and complete the future now since there will be no callback |
| 29 // from the VM. |
| 30 if (hasCompleted) { |
| 31 _invalidateDependentCode(); |
| 32 completer.complete(true); |
| 33 _outstandingLoadRequests.remove(this); |
| 34 } |
| 35 }); |
22 return completer.future; | 36 return completer.future; |
23 } | 37 } |
24 } | 38 } |
| 39 |
| 40 var _outstandingLoadRequests = new Map<_LibraryPrefix, Completer>(); |
| 41 |
| 42 |
| 43 // Called from the VM when all outstanding load requests have |
| 44 // finished. |
| 45 _completeDeferredLoads() { |
| 46 var lenghth = _outstandingLoadRequests; |
| 47 _outstandingLoadRequests.forEach((prefix, completer) { |
| 48 prefix._invalidateDependentCode(); |
| 49 completer.complete(true); |
| 50 }); |
| 51 _outstandingLoadRequests.clear(); |
| 52 } |
OLD | NEW |