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 |
169 TransformNode(this.classifier, this.transformer, this.key, this._location) { | 181 TransformNode(this.classifier, this.transformer, this.key, this._location) { |
170 _forced = transformer is! DeclaringAggregateTransformer; | 182 _forced = transformer is! DeclaringAggregateTransformer; |
171 | 183 |
172 _phaseAssetSubscription = phase.previous.onAsset.listen((node) { | 184 _phaseAssetSubscription = phase.previous.onAsset.listen((node) { |
173 if (!_missingInputs.contains(node.id)) return; | 185 if (!_missingInputs.contains(node.id)) return; |
174 if (_forced) node.force(); | 186 if (_forced) node.force(); |
175 _dirty(); | 187 _dirty(); |
176 }); | 188 }); |
177 | 189 |
178 _phaseStatusSubscription = phase.previous.onStatusChange.listen((status) { | 190 _phaseStatusSubscription = phase.previous.onStatusChange.listen((status) { |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 | 553 |
542 _state = _State.APPLIED; | 554 _state = _State.APPLIED; |
543 _streams.changeStatus(NodeStatus.IDLE); | 555 _streams.changeStatus(NodeStatus.IDLE); |
544 }); | 556 }); |
545 } | 557 } |
546 | 558 |
547 /// Gets the asset for an input [id]. | 559 /// Gets the asset for an input [id]. |
548 /// | 560 /// |
549 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 561 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
550 Future<Asset> getInput(AssetId id) { | 562 Future<Asset> getInput(AssetId id) { |
| 563 _timeAwaitingInputs.start(); |
| 564 _pendingSecondaryInputs++; |
551 return phase.previous.getOutput(id).then((node) { | 565 return phase.previous.getOutput(id).then((node) { |
552 // Throw if the input isn't found. This ensures the transformer's apply | 566 // Throw if the input isn't found. This ensures the transformer's apply |
553 // is exited. We'll then catch this and report it through the proper | 567 // is exited. We'll then catch this and report it through the proper |
554 // results stream. | 568 // results stream. |
555 if (node == null) { | 569 if (node == null) { |
556 _missingInputs.add(id); | 570 _missingInputs.add(id); |
557 throw new AssetNotFoundException(id); | 571 throw new AssetNotFoundException(id); |
558 } | 572 } |
559 | 573 |
560 _secondarySubscriptions.putIfAbsent(node.id, () { | 574 _secondarySubscriptions.putIfAbsent(node.id, () { |
561 return node.onStateChange.listen((_) => _dirty()); | 575 return node.onStateChange.listen((_) => _dirty()); |
562 }); | 576 }); |
563 | 577 |
564 return node.asset; | 578 return node.asset; |
| 579 }).whenComplete(() { |
| 580 _pendingSecondaryInputs--; |
| 581 if (_pendingSecondaryInputs != 0) _timeAwaitingInputs.stop(); |
565 }); | 582 }); |
566 } | 583 } |
567 | 584 |
568 /// Run [AggregateTransformer.apply]. | 585 /// Run [AggregateTransformer.apply]. |
569 /// | 586 /// |
570 /// Returns whether or not an error occurred while running the transformer. | 587 /// Returns whether or not an error occurred while running the transformer. |
571 Future<bool> _runApply() { | 588 Future<bool> _runApply() { |
572 var controller = new AggregateTransformController(this); | 589 var controller = new AggregateTransformController(this); |
573 _applyController = controller; | 590 _applyController = controller; |
574 _streams.onLogPool.add(controller.onLog); | 591 _streams.onLogPool.add(controller.onLog); |
575 for (var primary in _primaries) { | 592 for (var primary in _primaries) { |
576 if (!primary.state.isAvailable) continue; | 593 if (!primary.state.isAvailable) continue; |
577 controller.addInput(primary.asset); | 594 controller.addInput(primary.asset); |
578 } | 595 } |
579 _maybeFinishApplyController(); | 596 _maybeFinishApplyController(); |
580 | 597 |
581 return syncFuture(() { | 598 return syncFuture(() { |
| 599 _timeInTransformer.reset(); |
| 600 _timeAwaitingInputs.reset(); |
| 601 _timeInTransformer.start(); |
582 return transformer.apply(controller.transform); | 602 return transformer.apply(controller.transform); |
583 }).whenComplete(() { | 603 }).whenComplete(() { |
| 604 _timeInTransformer.stop(); |
| 605 _timeAwaitingInputs.stop(); |
| 606 |
584 // Cancel the controller here even if `apply` wasn't interrupted. Since | 607 // Cancel the controller here even if `apply` wasn't interrupted. Since |
585 // the apply is finished, we want to close out the controller's streams. | 608 // the apply is finished, we want to close out the controller's streams. |
586 controller.cancel(); | 609 controller.cancel(); |
587 _applyController = null; | 610 _applyController = null; |
588 }).then((_) { | 611 }).then((_) { |
589 assert(_state != _State.DECLARED); | 612 assert(_state != _State.DECLARED); |
590 assert(_state != _State.DECLARING); | 613 assert(_state != _State.DECLARING); |
591 assert(_state != _State.APPLIED); | 614 assert(_state != _State.APPLIED); |
592 | 615 |
593 if (!_forced && _primaries.any((node) => !node.state.isAvailable)) { | 616 if (!_forced && _primaries.any((node) => !node.state.isAvailable)) { |
594 _state = _State.DECLARED; | 617 _state = _State.DECLARED; |
595 _streams.changeStatus(NodeStatus.IDLE); | 618 _streams.changeStatus(NodeStatus.IDLE); |
596 return false; | 619 return false; |
597 } | 620 } |
598 | 621 |
599 if (_isRemoved) return false; | 622 if (_isRemoved) return false; |
600 if (_state == _State.NEEDS_APPLY) return false; | 623 if (_state == _State.NEEDS_APPLY) return false; |
601 if (_state == _State.NEEDS_DECLARE) return false; | 624 if (_state == _State.NEEDS_DECLARE) return false; |
602 if (controller.loggedError) return true; | 625 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 var ranLong = _timeInTransformer.elapsed > new Duration(seconds: 1); |
| 631 var ranLongLocally = |
| 632 _timeInTransformer.elapsed - _timeAwaitingInputs.elapsed > |
| 633 new Duration(milliseconds: 200); |
| 634 |
| 635 // Report the transformer's timing information if it spent more than 0.2s |
| 636 // doing things other than waiting for its secondary inputs or if it spent |
| 637 // more than 1s in total. |
| 638 if (ranLongLocally || ranLong) { |
| 639 _streams.onLogController.add(new LogEntry( |
| 640 info, info.primaryId, LogLevel.FINE, |
| 641 "Took ${niceDuration(_timeInTransformer.elapsed)} " |
| 642 "(${niceDuration(_timeAwaitingInputs.elapsed)} awaiting " |
| 643 "secondary inputs).", |
| 644 null)); |
| 645 } |
| 646 |
603 _handleApplyResults(controller); | 647 _handleApplyResults(controller); |
604 return false; | 648 return false; |
605 }).catchError((error, stackTrace) { | 649 }).catchError((error, stackTrace) { |
606 // If the transform became dirty while processing, ignore any errors from | 650 // If the transform became dirty while processing, ignore any errors from |
607 // it. | 651 // it. |
608 if (_state == _State.NEEDS_APPLY || _isRemoved) return false; | 652 if (_state == _State.NEEDS_APPLY || _isRemoved) return false; |
609 | 653 |
610 // Catch all transformer errors and pipe them to the results stream. This | 654 // Catch all transformer errors and pipe them to the results stream. This |
611 // is so a broken transformer doesn't take down the whole graph. | 655 // is so a broken transformer doesn't take down the whole graph. |
612 phase.cascade.reportError(_wrapException(error, stackTrace)); | 656 phase.cascade.reportError(_wrapException(error, stackTrace)); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 /// declaring and [APPLYING] otherwise. If a primary input is added or | 840 /// declaring and [APPLYING] otherwise. If a primary input is added or |
797 /// removed, this will transition to [DECLARING]. | 841 /// removed, this will transition to [DECLARING]. |
798 static const APPLIED = const _State._("applied"); | 842 static const APPLIED = const _State._("applied"); |
799 | 843 |
800 final String name; | 844 final String name; |
801 | 845 |
802 const _State._(this.name); | 846 const _State._(this.name); |
803 | 847 |
804 String toString() => name; | 848 String toString() => name; |
805 } | 849 } |
OLD | NEW |