| 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.graph.transform_node; | 5 library barback.graph.transform_node; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import '../asset/asset.dart'; | 9 import '../asset/asset.dart'; |
| 10 import '../asset/asset_id.dart'; | 10 import '../asset/asset_id.dart'; |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 159 |
| 160 /// The controller for the currently-running [AggregateTransformer.apply] | 160 /// The controller for the currently-running [AggregateTransformer.apply] |
| 161 /// call's [AggregateTransform]. | 161 /// call's [AggregateTransform]. |
| 162 /// | 162 /// |
| 163 /// This will be non-`null` when [AggregateTransform.apply] is running, which | 163 /// This will be non-`null` when [AggregateTransform.apply] is running, which |
| 164 /// means that it's always non-`null` when [_state] is [_State.APPLYING] or | 164 /// means that it's always non-`null` when [_state] is [_State.APPLYING] or |
| 165 /// [_State.NEEDS_APPLY], sometimes non-`null` when it's | 165 /// [_State.NEEDS_APPLY], sometimes non-`null` when it's |
| 166 /// [_State.NEEDS_DECLARE], and always `null` otherwise. | 166 /// [_State.NEEDS_DECLARE], and always `null` otherwise. |
| 167 AggregateTransformController _applyController; | 167 AggregateTransformController _applyController; |
| 168 | 168 |
| 169 /// The number of secondary inputs that have been requested but not yet | |
| 170 /// produced. | |
| 171 int _pendingSecondaryInputs = 0; | |
| 172 | |
| 173 /// A stopwatch that tracks the total time spent in a transformer's `apply` | |
| 174 /// function. | |
| 175 final _timeInTransformer = new Stopwatch(); | |
| 176 | |
| 177 /// A stopwatch that tracks the time in a transformer's `apply` function spent | |
| 178 /// waiting for [getInput] calls to complete. | |
| 179 final _timeAwaitingInputs = new Stopwatch(); | |
| 180 | |
| 181 TransformNode(this.classifier, this.transformer, this.key, this._location) { | 169 TransformNode(this.classifier, this.transformer, this.key, this._location) { |
| 182 _forced = transformer is! DeclaringAggregateTransformer; | 170 _forced = transformer is! DeclaringAggregateTransformer; |
| 183 | 171 |
| 184 _phaseAssetSubscription = phase.previous.onAsset.listen((node) { | 172 _phaseAssetSubscription = phase.previous.onAsset.listen((node) { |
| 185 if (!_missingInputs.contains(node.id)) return; | 173 if (!_missingInputs.contains(node.id)) return; |
| 186 if (_forced) node.force(); | 174 if (_forced) node.force(); |
| 187 _dirty(); | 175 _dirty(); |
| 188 }); | 176 }); |
| 189 | 177 |
| 190 _phaseStatusSubscription = phase.previous.onStatusChange.listen((status) { | 178 _phaseStatusSubscription = phase.previous.onStatusChange.listen((status) { |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 | 541 |
| 554 _state = _State.APPLIED; | 542 _state = _State.APPLIED; |
| 555 _streams.changeStatus(NodeStatus.IDLE); | 543 _streams.changeStatus(NodeStatus.IDLE); |
| 556 }); | 544 }); |
| 557 } | 545 } |
| 558 | 546 |
| 559 /// Gets the asset for an input [id]. | 547 /// Gets the asset for an input [id]. |
| 560 /// | 548 /// |
| 561 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 549 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 562 Future<Asset> getInput(AssetId id) { | 550 Future<Asset> getInput(AssetId id) { |
| 563 _timeAwaitingInputs.start(); | |
| 564 _pendingSecondaryInputs++; | |
| 565 return phase.previous.getOutput(id).then((node) { | 551 return phase.previous.getOutput(id).then((node) { |
| 566 // Throw if the input isn't found. This ensures the transformer's apply | 552 // Throw if the input isn't found. This ensures the transformer's apply |
| 567 // is exited. We'll then catch this and report it through the proper | 553 // is exited. We'll then catch this and report it through the proper |
| 568 // results stream. | 554 // results stream. |
| 569 if (node == null) { | 555 if (node == null) { |
| 570 _missingInputs.add(id); | 556 _missingInputs.add(id); |
| 571 throw new AssetNotFoundException(id); | 557 throw new AssetNotFoundException(id); |
| 572 } | 558 } |
| 573 | 559 |
| 574 _secondarySubscriptions.putIfAbsent(node.id, () { | 560 _secondarySubscriptions.putIfAbsent(node.id, () { |
| 575 return node.onStateChange.listen((_) => _dirty()); | 561 return node.onStateChange.listen((_) => _dirty()); |
| 576 }); | 562 }); |
| 577 | 563 |
| 578 return node.asset; | 564 return node.asset; |
| 579 }).whenComplete(() { | |
| 580 _pendingSecondaryInputs--; | |
| 581 if (_pendingSecondaryInputs != 0) _timeAwaitingInputs.stop(); | |
| 582 }); | 565 }); |
| 583 } | 566 } |
| 584 | 567 |
| 585 /// Run [AggregateTransformer.apply]. | 568 /// Run [AggregateTransformer.apply]. |
| 586 /// | 569 /// |
| 587 /// Returns whether or not an error occurred while running the transformer. | 570 /// Returns whether or not an error occurred while running the transformer. |
| 588 Future<bool> _runApply() { | 571 Future<bool> _runApply() { |
| 589 var controller = new AggregateTransformController(this); | 572 var controller = new AggregateTransformController(this); |
| 590 _applyController = controller; | 573 _applyController = controller; |
| 591 _streams.onLogPool.add(controller.onLog); | 574 _streams.onLogPool.add(controller.onLog); |
| 592 for (var primary in _primaries) { | 575 for (var primary in _primaries) { |
| 593 if (!primary.state.isAvailable) continue; | 576 if (!primary.state.isAvailable) continue; |
| 594 controller.addInput(primary.asset); | 577 controller.addInput(primary.asset); |
| 595 } | 578 } |
| 596 _maybeFinishApplyController(); | 579 _maybeFinishApplyController(); |
| 597 | 580 |
| 598 return syncFuture(() { | 581 return syncFuture(() { |
| 599 _timeInTransformer.reset(); | |
| 600 _timeAwaitingInputs.reset(); | |
| 601 _timeInTransformer.start(); | |
| 602 return transformer.apply(controller.transform); | 582 return transformer.apply(controller.transform); |
| 603 }).whenComplete(() { | 583 }).whenComplete(() { |
| 604 _timeInTransformer.stop(); | |
| 605 _timeAwaitingInputs.stop(); | |
| 606 | |
| 607 // Cancel the controller here even if `apply` wasn't interrupted. Since | 584 // Cancel the controller here even if `apply` wasn't interrupted. Since |
| 608 // the apply is finished, we want to close out the controller's streams. | 585 // the apply is finished, we want to close out the controller's streams. |
| 609 controller.cancel(); | 586 controller.cancel(); |
| 610 _applyController = null; | 587 _applyController = null; |
| 611 }).then((_) { | 588 }).then((_) { |
| 612 assert(_state != _State.DECLARED); | 589 assert(_state != _State.DECLARED); |
| 613 assert(_state != _State.DECLARING); | 590 assert(_state != _State.DECLARING); |
| 614 assert(_state != _State.APPLIED); | 591 assert(_state != _State.APPLIED); |
| 615 | 592 |
| 616 if (!_forced && _primaries.any((node) => !node.state.isAvailable)) { | 593 if (!_forced && _primaries.any((node) => !node.state.isAvailable)) { |
| 617 _state = _State.DECLARED; | 594 _state = _State.DECLARED; |
| 618 _streams.changeStatus(NodeStatus.IDLE); | 595 _streams.changeStatus(NodeStatus.IDLE); |
| 619 return false; | 596 return false; |
| 620 } | 597 } |
| 621 | 598 |
| 622 if (_isRemoved) return false; | 599 if (_isRemoved) return false; |
| 623 if (_state == _State.NEEDS_APPLY) return false; | 600 if (_state == _State.NEEDS_APPLY) return false; |
| 624 if (_state == _State.NEEDS_DECLARE) return false; | 601 if (_state == _State.NEEDS_DECLARE) return false; |
| 625 if (controller.loggedError) return true; | 602 if (controller.loggedError) return true; |
| 626 | |
| 627 // If the transformer took long enough, log its duration in fine output. | |
| 628 // That way it's not always visible, but users running with "pub serve | |
| 629 // --verbose" can see it. | |
| 630 if (_timeInTransformer.elapsed > new Duration(seconds: 1) || | |
| 631 (_timeInTransformer.elapsed - _timeAwaitingInputs.elapsed > | |
| 632 new Duration(milliseconds: 200))) { | |
| 633 _streams.onLogController.add(new LogEntry( | |
| 634 info, info.primaryId, LogLevel.FINE, | |
| 635 "Took ${niceDuration(_timeInTransformer.elapsed)} " | |
| 636 "(${niceDuration(_timeAwaitingInputs.elapsed)} awaiting " | |
| 637 "secondary inputs).", | |
| 638 null)); | |
| 639 } | |
| 640 | |
| 641 _handleApplyResults(controller); | 603 _handleApplyResults(controller); |
| 642 return false; | 604 return false; |
| 643 }).catchError((error, stackTrace) { | 605 }).catchError((error, stackTrace) { |
| 644 // If the transform became dirty while processing, ignore any errors from | 606 // If the transform became dirty while processing, ignore any errors from |
| 645 // it. | 607 // it. |
| 646 if (_state == _State.NEEDS_APPLY || _isRemoved) return false; | 608 if (_state == _State.NEEDS_APPLY || _isRemoved) return false; |
| 647 | 609 |
| 648 // Catch all transformer errors and pipe them to the results stream. This | 610 // Catch all transformer errors and pipe them to the results stream. This |
| 649 // is so a broken transformer doesn't take down the whole graph. | 611 // is so a broken transformer doesn't take down the whole graph. |
| 650 phase.cascade.reportError(_wrapException(error, stackTrace)); | 612 phase.cascade.reportError(_wrapException(error, stackTrace)); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 834 /// declaring and [APPLYING] otherwise. If a primary input is added or | 796 /// declaring and [APPLYING] otherwise. If a primary input is added or |
| 835 /// removed, this will transition to [DECLARING]. | 797 /// removed, this will transition to [DECLARING]. |
| 836 static const APPLIED = const _State._("applied"); | 798 static const APPLIED = const _State._("applied"); |
| 837 | 799 |
| 838 final String name; | 800 final String name; |
| 839 | 801 |
| 840 const _State._(this.name); | 802 const _State._(this.name); |
| 841 | 803 |
| 842 String toString() => name; | 804 String toString() => name; |
| 843 } | 805 } |
| OLD | NEW |