Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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