| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 barback.phase; | 5 library barback.phase; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'asset.dart'; | 10 import 'asset.dart'; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 /// Whenever an asset is added or modified, we need to asynchronously | 62 /// Whenever an asset is added or modified, we need to asynchronously |
| 63 /// determine which transformers can use it as their primary input. We can't | 63 /// determine which transformers can use it as their primary input. We can't |
| 64 /// start processing until we know which transformers to run, and this allows | 64 /// start processing until we know which transformers to run, and this allows |
| 65 /// us to wait until we do. | 65 /// us to wait until we do. |
| 66 var _adjustTransformersFutures = new Map<AssetId, Future>(); | 66 var _adjustTransformersFutures = new Map<AssetId, Future>(); |
| 67 | 67 |
| 68 /// New asset nodes that were added while [_adjustTransformers] was still | 68 /// New asset nodes that were added while [_adjustTransformers] was still |
| 69 /// being run on an old version of that asset. | 69 /// being run on an old version of that asset. |
| 70 var _pendingNewInputs = new Map<AssetId, AssetNode>(); | 70 var _pendingNewInputs = new Map<AssetId, AssetNode>(); |
| 71 | 71 |
| 72 /// A map of output ids to the asset node outputs for those ids. | 72 /// A map of output ids to the asset node outputs for those ids and the |
| 73 /// transforms that produced those asset nodes. |
| 73 /// | 74 /// |
| 74 /// Usually there's only one node for a given output id. However, it's | 75 /// Usually there's only one node for a given output id. However, it's |
| 75 /// possible for multiple transformers in this phase to output an asset with | 76 /// possible for multiple transformers in this phase to output an asset with |
| 76 /// the same id. In that case, the chronologically first output emitted is | 77 /// the same id. In that case, the chronologically first output emitted is |
| 77 /// passed forward. We keep track of the other nodes so that if that output is | 78 /// passed forward. We keep track of the other nodes so that if that output is |
| 78 /// removed, we know which asset to replace it with. | 79 /// removed, we know which asset to replace it with. |
| 79 final _outputs = new Map<AssetId, Queue<AssetNode>>(); | 80 final _outputs = new Map<AssetId, Queue<AssetNode>>(); |
| 80 | 81 |
| 81 /// A stream that emits an event whenever this phase becomes dirty and needs | 82 /// A stream that emits an event whenever this phase becomes dirty and needs |
| 82 /// to be run. | 83 /// to be run. |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 } | 331 } |
| 331 }); | 332 }); |
| 332 })).then((_) { | 333 })).then((_) { |
| 333 // Report collisions in a deterministic order. | 334 // Report collisions in a deterministic order. |
| 334 collisions = collisions.toList(); | 335 collisions = collisions.toList(); |
| 335 collisions.sort((a, b) => a.compareTo(b)); | 336 collisions.sort((a, b) => a.compareTo(b)); |
| 336 for (var collision in collisions) { | 337 for (var collision in collisions) { |
| 337 // Ensure that there's still a collision. It's possible it was resolved | 338 // Ensure that there's still a collision. It's possible it was resolved |
| 338 // while another transform was running. | 339 // while another transform was running. |
| 339 if (_outputs[collision].length <= 1) continue; | 340 if (_outputs[collision].length <= 1) continue; |
| 340 cascade.reportError(new AssetCollisionException(collision)); | 341 cascade.reportError(new AssetCollisionException( |
| 342 _outputs[collision].where((asset) => asset.transform != null) |
| 343 .map((asset) => asset.transform.info), |
| 344 collision)); |
| 341 } | 345 } |
| 342 }); | 346 }); |
| 343 } | 347 } |
| 344 | 348 |
| 345 /// Properly resolve collisions when [output] is removed. | 349 /// Properly resolve collisions when [output] is removed. |
| 346 void _handleOutputRemoval(AssetNode output) { | 350 void _handleOutputRemoval(AssetNode output) { |
| 347 output.whenRemoved.then((_) { | 351 output.whenRemoved.then((_) { |
| 348 var assets = _outputs[output.id]; | 352 var assets = _outputs[output.id]; |
| 349 if (assets.length == 1) { | 353 if (assets.length == 1) { |
| 350 assert(assets.single == output); | 354 assert(assets.single == output); |
| 351 _outputs.remove(output.id); | 355 _outputs.remove(output.id); |
| 352 return; | 356 return; |
| 353 } | 357 } |
| 354 | 358 |
| 355 // If there was more than one asset, we're resolving a collision -- | 359 // If there was more than one asset, we're resolving a collision -- |
| 356 // possibly partially. | 360 // possibly partially. |
| 357 var wasFirst = assets.first == output; | 361 var wasFirst = assets.first == output; |
| 358 assets.remove(output); | 362 assets.remove(output); |
| 359 | 363 |
| 360 // If this was the first asset, we need to pass the next asset | 364 // If this was the first asset, we need to pass the next asset |
| 361 // (chronologically) to the next phase. Pump the event queue first to give | 365 // (chronologically) to the next phase. Pump the event queue first to give |
| 362 // [_next] a chance to handle the removal of its input before getting a | 366 // [_next] a chance to handle the removal of its input before getting a |
| 363 // new input. | 367 // new input. |
| 364 if (wasFirst) newFuture(() => _next.addInput(assets.first)); | 368 if (wasFirst) { |
| 369 newFuture(() => _next.addInput(assets.first)); |
| 370 } |
| 365 | 371 |
| 366 // If there's still a collision, report it. This lets the user know | 372 // If there's still a collision, report it. This lets the user know |
| 367 // if they've successfully resolved the collision or not. | 373 // if they've successfully resolved the collision or not. |
| 368 if (assets.length > 1) { | 374 if (assets.length > 1) { |
| 369 // Pump the event queue to ensure that the removal of the input triggers | 375 // Pump the event queue to ensure that the removal of the input triggers |
| 370 // a new build to which we can attach the error. | 376 // a new build to which we can attach the error. |
| 371 newFuture(() => | 377 newFuture(() => cascade.reportError(new AssetCollisionException( |
| 372 cascade.reportError(new AssetCollisionException(output.id))); | 378 assets.where((asset) => asset.transform != null) |
| 379 .map((asset) => asset.transform.info), |
| 380 output.id))); |
| 373 } | 381 } |
| 374 }); | 382 }); |
| 375 } | 383 } |
| 376 } | 384 } |
| OLD | NEW |