| 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 // A list of two element lists. The first element is the _LibraryPrefix. The |
| 44 | 46 // second element is the Completer for the load request. |
| 47 var _outstandingLoadRequests = new List<List>(); |
| 45 | 48 |
| 46 // Called from the VM when all outstanding load requests have | 49 // Called from the VM when all outstanding load requests have |
| 47 // finished. | 50 // finished. |
| 48 _completeDeferredLoads() { | 51 _completeDeferredLoads() { |
| 49 _outstandingLoadRequests.forEach((prefix, completer) { | 52 for (int i = 0; i < _outstandingLoadRequests.length; i++) { |
| 53 var prefix = _outstandingLoadRequests[i][0]; |
| 54 var completer = _outstandingLoadRequests[i][1]; |
| 50 var error = prefix._loadError(); | 55 var error = prefix._loadError(); |
| 51 if (error != null) { | 56 if (error != null) { |
| 52 completer.completeError(error); | 57 completer.completeError(error); |
| 53 } else { | 58 } else { |
| 54 prefix._invalidateDependentCode(); | 59 prefix._invalidateDependentCode(); |
| 55 completer.complete(true); | 60 completer.complete(true); |
| 56 } | 61 } |
| 57 }); | 62 } |
| 58 _outstandingLoadRequests.clear(); | 63 _outstandingLoadRequests.clear(); |
| 59 } | 64 } |
| OLD | NEW |