| 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 class _LibraryPrefix { |
| 10 bool _load() native "LibraryPrefix_load"; | 10 bool _load() native "LibraryPrefix_load"; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 pair.add(this); | 25 pair.add(this); |
| 26 pair.add(completer); | 26 pair.add(completer); |
| 27 _outstandingLoadRequests.add(pair); | 27 _outstandingLoadRequests.add(pair); |
| 28 Timer.run(() { | 28 Timer.run(() { |
| 29 var hasCompleted = this._load(); | 29 var hasCompleted = this._load(); |
| 30 // Loading can complete immediately, for example when the same | 30 // Loading can complete immediately, for example when the same |
| 31 // library has been loaded eagerly or through another deferred | 31 // library has been loaded eagerly or through another deferred |
| 32 // prefix. If that is the case, we must invalidate the dependent | 32 // prefix. If that is the case, we must invalidate the dependent |
| 33 // 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 |
| 34 // from the VM. | 34 // from the VM. |
| 35 if (hasCompleted) { | 35 if (hasCompleted && !completer.isCompleted) { |
| 36 _invalidateDependentCode(); | 36 _invalidateDependentCode(); |
| 37 completer.complete(true); | 37 completer.complete(true); |
| 38 _outstandingLoadRequests.remove(pair); | 38 _outstandingLoadRequests.remove(pair); |
| 39 } | 39 } |
| 40 }); | 40 }); |
| 41 return completer.future; | 41 return completer.future; |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 // A list of two element lists. The first element is the _LibraryPrefix. The | 45 // A list of two element lists. The first element is the _LibraryPrefix. The |
| 46 // second element is the Completer for the load request. | 46 // second element is the Completer for the load request. |
| 47 var _outstandingLoadRequests = new List<List>(); | 47 var _outstandingLoadRequests = new List<List>(); |
| 48 | 48 |
| 49 // Called from the VM when an outstanding load request has finished. | 49 // Called from the VM when an outstanding load request has finished. |
| 50 _completeDeferredLoads() { | 50 _completeDeferredLoads() { |
| 51 // Determine which outstanding load requests have completed and complete | 51 // Determine which outstanding load requests have completed and complete |
| 52 // their completer (with an error or true). For outstanding load requests | 52 // their completer (with an error or true). For outstanding load requests |
| 53 // which have not completed, remember them for next time in | 53 // which have not completed, remember them for next time in |
| 54 // stillOutstandingLoadRequests. | 54 // stillOutstandingLoadRequests. |
| 55 var stillOutstandingLoadRequests = new List<List>(); | 55 var stillOutstandingLoadRequests = new List<List>(); |
| 56 for (int i = 0; i < _outstandingLoadRequests.length; i++) { | 56 var completedLoadRequests = new List<List>(); |
| 57 var prefix = _outstandingLoadRequests[i][0]; | 57 |
| 58 if (prefix._load()) { | 58 // Make a copy of the outstandingRequests because the call to _load below |
| 59 var completer = _outstandingLoadRequests[i][1]; | 59 // may recursively trigger another call to |_completeDeferredLoads|, which |
| 60 var error = prefix._loadError(); | 60 // can cause |_outstandingLoadRequests| to be modified. |
| 61 if (error != null) { | 61 var outstandingRequests = _outstandingLoadRequests.toList(); |
| 62 completer.completeError(error); | 62 for (int i = 0; i < outstandingRequests.length; i++) { |
| 63 } else { | 63 var prefix = outstandingRequests[i][0]; |
| 64 prefix._invalidateDependentCode(); | 64 var completer = outstandingRequests[i][1]; |
| 65 completer.complete(true); | 65 var error = prefix._loadError(); |
| 66 } | 66 if (completer.isCompleted) { |
| 67 // Already completed. Skip. |
| 68 continue; |
| 69 } |
| 70 if (error != null) { |
| 71 completer.completeError(error); |
| 72 } else if (prefix._load()) { |
| 73 prefix._invalidateDependentCode(); |
| 74 completer.complete(true); |
| 67 } else { | 75 } else { |
| 68 stillOutstandingLoadRequests.add(_outstandingLoadRequests[i]); | 76 stillOutstandingLoadRequests.add(outstandingRequests[i]); |
| 69 } | 77 } |
| 70 } | 78 } |
| 71 _outstandingLoadRequests = stillOutstandingLoadRequests; | 79 _outstandingLoadRequests = stillOutstandingLoadRequests; |
| 72 } | 80 } |
| OLD | NEW |