1 | are-we-there-yet
|
2 | ----------------
|
3 |
|
4 | Track complex hierarchies of asynchronous task completion statuses. This is
|
5 | intended to give you a way of recording and reporting the progress of the big
|
6 | recursive fan-out and gather type workflows that are so common in async.
|
7 |
|
8 | What you do with this completion data is up to you, but the most common use case is to
|
9 | feed it to one of the many progress bar modules.
|
10 |
|
11 | Most progress bar modules include a rudimentary version of this, but my
|
12 | needs were more complex.
|
13 |
|
14 | Usage
|
15 | =====
|
16 |
|
17 | ```javascript
|
18 | var TrackerGroup = require("are-we-there-yet").TrackerGroup
|
19 |
|
20 | var top = new TrackerGroup("program")
|
21 |
|
22 | var single = top.newItem("one thing", 100)
|
23 | single.completeWork(20)
|
24 |
|
25 | console.log(top.completed()) // 0.2
|
26 |
|
27 | fs.stat("file", function(er, stat) {
|
28 | if (er) throw er
|
29 | var stream = top.newStream("file", stat.size)
|
30 | console.log(top.completed()) // now 0.1 as single is 50% of the job and is 20% complete
|
31 | // and 50% * 20% == 10%
|
32 | fs.createReadStream("file").pipe(stream).on("data", function (chunk) {
|
33 | // do stuff with chunk
|
34 | })
|
35 | top.on("change", function (name) {
|
36 | // called each time a chunk is read from "file"
|
37 | // top.completed() will start at 0.1 and fill up to 0.6 as the file is read
|
38 | })
|
39 | })
|
40 | ```
|
41 |
|
42 | Shared Methods
|
43 | ==============
|
44 |
|
45 | * var completed = tracker.completed()
|
46 |
|
47 | Implemented in: `Tracker`, `TrackerGroup`, `TrackerStream`
|
48 |
|
49 | Returns the ratio of completed work to work to be done. Range of 0 to 1.
|
50 |
|
51 | * tracker.finish()
|
52 |
|
53 | Implemented in: `Tracker`, `TrackerGroup`
|
54 |
|
55 | Marks the tracker as completed. With a TrackerGroup this marks all of its
|
56 | components as completed.
|
57 |
|
58 | Marks all of the components of this tracker as finished, which in turn means
|
59 | that `tracker.completed()` for this will now be 1.
|
60 |
|
61 | This will result in one or more `change` events being emitted.
|
62 |
|
63 | Events
|
64 | ======
|
65 |
|
66 | All tracker objects emit `change` events with the following arguments:
|
67 |
|
68 | ```
|
69 | function (name, completed, tracker)
|
70 | ```
|
71 |
|
72 | `name` is the name of the tracker that originally emitted the event,
|
73 | or if it didn't have one, the first containing tracker group that had one.
|
74 |
|
75 | `completed` is the percent complete (as returned by `tracker.completed()` method).
|
76 |
|
77 | `tracker` is the tracker object that you are listening for events on.
|
78 |
|
79 | TrackerGroup
|
80 | ============
|
81 |
|
82 | * var tracker = new TrackerGroup(**name**)
|
83 |
|
84 | * **name** *(optional)* - The name of this tracker group, used in change
|
85 | notifications if the component updating didn't have a name. Defaults to undefined.
|
86 |
|
87 | Creates a new empty tracker aggregation group. These are trackers whose
|
88 | completion status is determined by the completion status of other trackers added to this aggregation group.
|
89 |
|
90 | Ex.
|
91 |
|
92 | ```javascript
|
93 | var tracker = new TrackerGroup("parent")
|
94 | var foo = tracker.newItem("firstChild", 100)
|
95 | var bar = tracker.newItem("secondChild", 100)
|
96 |
|
97 | foo.finish()
|
98 | console.log(tracker.completed()) // 0.5
|
99 | bar.finish()
|
100 | console.log(tracker.completed()) // 1
|
101 | ```
|
102 |
|
103 | * tracker.addUnit(**otherTracker**, **weight**)
|
104 |
|
105 | * **otherTracker** - Any of the other are-we-there-yet tracker objects
|
106 | * **weight** *(optional)* - The weight to give the tracker, defaults to 1.
|
107 |
|
108 | Adds the **otherTracker** to this aggregation group. The weight determines
|
109 | how long you expect this tracker to take to complete in proportion to other
|
110 | units. So for instance, if you add one tracker with a weight of 1 and
|
111 | another with a weight of 2, you're saying the second will take twice as long
|
112 | to complete as the first. As such, the first will account for 33% of the
|
113 | completion of this tracker and the second will account for the other 67%.
|
114 |
|
115 | Returns **otherTracker**.
|
116 |
|
117 | * var subGroup = tracker.newGroup(**name**, **weight**)
|
118 |
|
119 | The above is exactly equivalent to:
|
120 |
|
121 | ```javascript
|
122 | var subGroup = tracker.addUnit(new TrackerGroup(name), weight)
|
123 | ```
|
124 |
|
125 | * var subItem = tracker.newItem(**name**, **todo**, **weight**)
|
126 |
|
127 | The above is exactly equivalent to:
|
128 |
|
129 | ```javascript
|
130 | var subItem = tracker.addUnit(new Tracker(name, todo), weight)
|
131 | ```
|
132 |
|
133 | * var subStream = tracker.newStream(**name**, **todo**, **weight**)
|
134 |
|
135 | The above is exactly equivalent to:
|
136 |
|
137 | ```javascript
|
138 | var subStream = tracker.addUnit(new TrackerStream(name, todo), weight)
|
139 | ```
|
140 |
|
141 | * console.log( tracker.debug() )
|
142 |
|
143 | Returns a tree showing the completion of this tracker group and all of its
|
144 | children, including recursively entering all of the children.
|
145 |
|
146 | Tracker
|
147 | =======
|
148 |
|
149 | * var tracker = new Tracker(**name**, **todo**)
|
150 |
|
151 | * **name** *(optional)* The name of this counter to report in change
|
152 | events. Defaults to undefined.
|
153 | * **todo** *(optional)* The amount of work todo (a number). Defaults to 0.
|
154 |
|
155 | Ordinarily these are constructed as a part of a tracker group (via
|
156 | `newItem`).
|
157 |
|
158 | * var completed = tracker.completed()
|
159 |
|
160 | Returns the ratio of completed work to work to be done. Range of 0 to 1. If
|
161 | total work to be done is 0 then it will return 0.
|
162 |
|
163 | * tracker.addWork(**todo**)
|
164 |
|
165 | * **todo** A number to add to the amount of work to be done.
|
166 |
|
167 | Increases the amount of work to be done, thus decreasing the completion
|
168 | percentage. Triggers a `change` event.
|
169 |
|
170 | * tracker.completeWork(**completed**)
|
171 |
|
172 | * **completed** A number to add to the work complete
|
173 |
|
174 | Increase the amount of work complete, thus increasing the completion percentage.
|
175 | Will never increase the work completed past the amount of work todo. That is,
|
176 | percentages > 100% are not allowed. Triggers a `change` event.
|
177 |
|
178 | * tracker.finish()
|
179 |
|
180 | Marks this tracker as finished, tracker.completed() will now be 1. Triggers
|
181 | a `change` event.
|
182 |
|
183 | TrackerStream
|
184 | =============
|
185 |
|
186 | * var tracker = new TrackerStream(**name**, **size**, **options**)
|
187 |
|
188 | * **name** *(optional)* The name of this counter to report in change
|
189 | events. Defaults to undefined.
|
190 | * **size** *(optional)* The number of bytes being sent through this stream.
|
191 | * **options** *(optional)* A hash of stream options
|
192 |
|
193 | The tracker stream object is a pass through stream that updates an internal
|
194 | tracker object each time a block passes through. It's intended to track
|
195 | downloads, file extraction and other related activities. You use it by piping
|
196 | your data source into it and then using it as your data source.
|
197 |
|
198 | If your data has a length attribute then that's used as the amount of work
|
199 | completed when the chunk is passed through. If it does not (eg, object
|
200 | streams) then each chunk counts as completing 1 unit of work, so your size
|
201 | should be the total number of objects being streamed.
|
202 |
|
203 | * tracker.addWork(**todo**)
|
204 |
|
205 | * **todo** Increase the expected overall size by **todo** bytes.
|
206 |
|
207 | Increases the amount of work to be done, thus decreasing the completion
|
208 | percentage. Triggers a `change` event.
|