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:collection"; | |
7 import "dart:isolate"; | 6 import "dart:isolate"; |
8 | 7 |
9 // This type corresponds to the VM-internal class LibraryPrefix. | 8 // This type corresponds to the VM-internal class LibraryPrefix. |
10 class _LibraryPrefix { | 9 class _LibraryPrefix { |
11 | |
12 bool _load() native "LibraryPrefix_load"; | 10 bool _load() native "LibraryPrefix_load"; |
13 Error _loadError() native "LibraryPrefix_loadError"; | 11 Error _loadError() native "LibraryPrefix_loadError"; |
14 bool isLoaded() native "LibraryPrefix_isLoaded"; | 12 bool isLoaded() native "LibraryPrefix_isLoaded"; |
15 | |
16 bool _invalidateDependentCode() | 13 bool _invalidateDependentCode() |
17 native "LibraryPrefix_invalidateDependentCode"; | 14 native "LibraryPrefix_invalidateDependentCode"; |
18 | 15 |
19 loadLibrary() { | 16 loadLibrary() { |
20 var completer = _outstandingLoadRequests[this]; | 17 for (int i = 0; i < _outstandingLoadRequests.length; i++) { |
21 if (completer != null) { | 18 if (_outstandingLoadRequests[i][0] == this) { |
22 return completer.future; | 19 return _outstandingLoadRequests[i][1].future; |
20 } | |
23 } | 21 } |
24 completer = new Completer<bool>(); | 22 |
25 _outstandingLoadRequests[this] = completer; | 23 var completer = new Completer<bool>(); |
24 var pair = new List(); | |
25 pair.add(this); | |
26 pair.add(completer); | |
27 _outstandingLoadRequests.add(pair); | |
26 Timer.run(() { | 28 Timer.run(() { |
27 var hasCompleted = this._load(); | 29 var hasCompleted = this._load(); |
28 // Loading can complete immediately, for example when the same | 30 // Loading can complete immediately, for example when the same |
29 // library has been loaded eagerly or through another deferred | 31 // library has been loaded eagerly or through another deferred |
30 // prefix. If that is the case, we must invalidate the dependent | 32 // prefix. If that is the case, we must invalidate the dependent |
31 // code and complete the future now since there will be no callback | 33 // code and complete the future now since there will be no callback |
32 // from the VM. | 34 // from the VM. |
33 if (hasCompleted) { | 35 if (hasCompleted) { |
34 _invalidateDependentCode(); | 36 _invalidateDependentCode(); |
35 completer.complete(true); | 37 completer.complete(true); |
36 _outstandingLoadRequests.remove(this); | 38 _outstandingLoadRequests.remove(pair); |
37 } | 39 } |
38 }); | 40 }); |
39 return completer.future; | 41 return completer.future; |
40 } | 42 } |
41 } | 43 } |
42 | 44 |
43 var _outstandingLoadRequests = new HashMap<_LibraryPrefix, Completer>(); | 45 var _outstandingLoadRequests = new List<List>(); |
hausner
2015/11/17 21:24:39
You lose a little documentation because it's no lo
zra
2015/11/17 21:55:47
Done.
| |
44 | |
45 | 46 |
46 // Called from the VM when all outstanding load requests have | 47 // Called from the VM when all outstanding load requests have |
47 // finished. | 48 // finished. |
48 _completeDeferredLoads() { | 49 _completeDeferredLoads() { |
49 _outstandingLoadRequests.forEach((prefix, completer) { | 50 for (int i = 0; i < _outstandingLoadRequests.length; i++) { |
51 var prefix = _outstandingLoadRequests[i][0]; | |
52 var completer = _outstandingLoadRequests[i][1]; | |
50 var error = prefix._loadError(); | 53 var error = prefix._loadError(); |
51 if (error != null) { | 54 if (error != null) { |
52 completer.completeError(error); | 55 completer.completeError(error); |
53 } else { | 56 } else { |
54 prefix._invalidateDependentCode(); | 57 prefix._invalidateDependentCode(); |
55 completer.complete(true); | 58 completer.complete(true); |
56 } | 59 } |
57 }); | 60 } |
58 _outstandingLoadRequests.clear(); | 61 _outstandingLoadRequests.clear(); |
59 } | 62 } |
OLD | NEW |