| 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 dependency_graph; | 5 library dependency_graph; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'utils.dart'; | 8 import 'utils.dart'; |
| 9 | 9 |
| 10 /* | 10 /* |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * - stateCount: Counts the number of nodes who are in a given [NodeState]. | 21 * - stateCount: Counts the number of nodes who are in a given [NodeState]. |
| 22 * | 22 * |
| 23 * Users of a [Graph] can listen for events by subscribing to the [events] | 23 * Users of a [Graph] can listen for events by subscribing to the [events] |
| 24 * stream. Three types of events will be fired (after the graph was modified): | 24 * stream. Three types of events will be fired (after the graph was modified): |
| 25 * - NodeAddedEvent: Fired after a node was added ot the graph. | 25 * - NodeAddedEvent: Fired after a node was added ot the graph. |
| 26 * - StateChangedEvent: Fired after the state of a node changed. | 26 * - StateChangedEvent: Fired after the state of a node changed. |
| 27 * - GraphSealedEvent: Fired after the graph was marked as immutable/sealed. | 27 * - GraphSealedEvent: Fired after the graph was marked as immutable/sealed. |
| 28 */ | 28 */ |
| 29 class Graph { | 29 class Graph { |
| 30 final _nodes = new Set<Node>(); | 30 final _nodes = new Set<Node>(); |
| 31 final _eventController = new StreamController<GraphEvent>(); | 31 final StreamController<GraphEvent> _eventController; |
| 32 final _stateCounts = new Map<NodeState, int>(); | 32 final _stateCounts = <NodeState, int>{}; |
| 33 var _eventStream; | 33 final Stream<GraphEvent> _eventStream; |
| 34 bool _isSealed = false; | 34 bool _isSealed = false; |
| 35 | 35 |
| 36 Graph() { | 36 factory Graph() { |
| 37 _eventStream = _eventController.stream.asBroadcastStream(); | 37 var controller = new StreamController<GraphEvent>(); |
| 38 return new Graph._(controller, controller.stream.asBroadcastStream()); |
| 38 } | 39 } |
| 39 | 40 |
| 41 Graph._(this._eventController, this._eventStream); |
| 42 |
| 40 Iterable<Node> get nodes => _nodes; | 43 Iterable<Node> get nodes => _nodes; |
| 41 Stream<GraphEvent> get events => _eventStream; | 44 Stream<GraphEvent> get events => _eventStream; |
| 42 bool get isSealed => _isSealed; | 45 bool get isSealed => _isSealed; |
| 43 | 46 |
| 44 int stateCount(NodeState state) { | 47 int stateCount(NodeState state) { |
| 45 int count = _stateCounts[state]; | 48 int count = _stateCounts[state]; |
| 46 return count == null ? 0 : count; | 49 return count == null ? 0 : count; |
| 47 } | 50 } |
| 48 | 51 |
| 49 void DumpCounts() { | 52 void DumpCounts() { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 var fromState = node.state; | 84 var fromState = node.state; |
| 82 node._state = newState; | 85 node._state = newState; |
| 83 | 86 |
| 84 _stateCounts[fromState] -= 1; | 87 _stateCounts[fromState] -= 1; |
| 85 _stateCounts.putIfAbsent(newState, () => 0); | 88 _stateCounts.putIfAbsent(newState, () => 0); |
| 86 _stateCounts[newState] += 1; | 89 _stateCounts[newState] += 1; |
| 87 | 90 |
| 88 _emitEvent(new StateChangedEvent(node, fromState, newState)); | 91 _emitEvent(new StateChangedEvent(node, fromState, newState)); |
| 89 } | 92 } |
| 90 | 93 |
| 91 _emitEvent(GraphEvent event) { | 94 void _emitEvent(GraphEvent event) { |
| 92 // We emit events asynchronously so the graph can be build up in small | 95 // We emit events asynchronously so the graph can be build up in small |
| 93 // batches and the events are delivered in small batches. | 96 // batches and the events are delivered in small batches. |
| 94 Timer.run(() { | 97 Timer.run(() { |
| 95 _eventController.add(event); | 98 _eventController.add(event); |
| 96 }); | 99 }); |
| 97 } | 100 } |
| 98 } | 101 } |
| 99 | 102 |
| 100 class Node extends UniqueObject { | 103 class Node extends UniqueObject { |
| 101 final Object _userData; | 104 final Object _userData; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 NodeAddedEvent(this.node); | 140 NodeAddedEvent(this.node); |
| 138 } | 141 } |
| 139 | 142 |
| 140 class StateChangedEvent extends GraphEvent { | 143 class StateChangedEvent extends GraphEvent { |
| 141 final Node node; | 144 final Node node; |
| 142 final NodeState from; | 145 final NodeState from; |
| 143 final NodeState to; | 146 final NodeState to; |
| 144 | 147 |
| 145 StateChangedEvent(this.node, this.from, this.to); | 148 StateChangedEvent(this.node, this.from, this.to); |
| 146 } | 149 } |
| OLD | NEW |