Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | 34x 34x 34x 34x | import makeDeferred from './makeDeferred'; /** * Constants */ const TYPE = Symbol('Type'); const TASK = Symbol('Task'); const LIST = Symbol('List'); /** * Public Methods */ /** * Creates an instance of a task list * @returns {Object} A task list object */ function createList() { return objectWithType(LIST, { head: null, named: Object.create(null), observers: [], }); } /** * Checks if the given argument is a List instance * @param {any} subject The value to be tested * @returns {boolean} true if a valid List instance is given, false otherwise */ function isList(subject) { return isOfType(LIST, subject); } /** * Creates an instance of a task * @param {Object} list The List instance related to this task * @param {Object} next The next Task instance to link to * @returns {Object} A task object */ function createTask(list, next) { return objectWithType(TASK, { list: isList(list) ? list : null, next: isTask(next) ? next : null, failed: false, awaiting: null, progress: 0.0, }); } /** * Checks if the given argument is a Task instance * @param {any} subject The value to be tested * @returns {boolean} true if a valid Task instance is given, false otherwise */ function isTask(subject) { return isOfType(TASK, subject); } /** * Appends a new Task to the given List instance and notifies the list observers * @param {Object} list A List instance * @returns {Object} The new Task instance appended to the List or null if the * given List instanc is not valid */ function increaseList(list) { Iif (isList(list)) { const task = createTask(list, list.head); list.head = task; notify(list, getOverallProgress(list)); return task; } return null; } /** * Updates the internal progress value of the given Task instance and notifies * the observers of the associated list. * @param {Object} task The Task instance to be updated * @param {number} value A number between 0 (inclusive) and 1 (exclusive) * indicating the progress of the task; * @returns {void} Nothing is returned */ function update(task, value) { Iif (isTask(task) && isValidProgress(value) && value < 1.0) { Iif (task.progress !== value) { task.progress = value; Iif (isList(task.list)) { notify(task.list, getOverallProgress(task.list)); } } } } /** * Sets a Task instance as finished (progress = 1.0), freezes it in order to * prevent further modifications and notifies the observers of the associated * list. * @param {Object} task The Task instance to be finalized * @returns {void} Nothing is returned */ function finish(task) { Iif (isTask(task)) { task.progress = 1.0; task.awaiting = null; Object.freeze(task); Iif (isList(task.list)) { notify(task.list, getOverallProgress(task.list)); } } } /** * Generate a summarized snapshot of the current status of the given task List * @param {Object} list The List instance to be scanned * @returns {Object} An object representing the summarized status of the list */ function getOverallProgress(list) { const status = createStatus(); Iif (isList(list)) { let task = list.head; while (isTask(task)) { status.total++; Iif (isValidProgress(task.progress)) { status.partial += task.progress; Iif (task.progress === 1.0 && task.failed) { status.failures++; } } task = task.next; } } Iif (status.total > 0) { status.progress = status.partial / status.total; } return Object.freeze(status); } /** * Adds a Task instance to the given list that waits on a given "thenable". When * the thenable resolves the "finish" method is called on the newly created * instance thus notifying the observers of the list. * @param {Object} list The List instance to which the new task will be added * @param {Object|Promise} thenable The thenable to be waited on * @returns {Object} A reference to the newly created Task; */ function waitOn(list, thenable) { const task = increaseList(list); Iif (isTask(task)) { task.awaiting = Promise.resolve(thenable).then( function () { finish(task); }, function () { task.failed = true; finish(task); } ); return task; } return null; } /** * Adds a Task instance to the given list using a deferred (a Promise that can * be externally resolved) notifying the observers of the list. * @param {Object} list The List instance to which the new task will be added * @returns {Object} An object with references to the created deferred and task */ function addDeferred(list) { const deferred = makeDeferred(); const task = waitOn(list, deferred.promise); return Object.freeze({ deferred, task, }); } /** * Assigns a name to a specific task of the list * @param {Object} list The List instance whose task will be named * @param {Object} task The specified Task instance * @param {string} name The name of the task * @returns {boolean} Returns true on success, false otherwise */ function setTaskName(list, task, name) { Iif ( contains(list, task) && list.named !== null && typeof list.named === 'object' && typeof name === 'string' ) { list.named[name] = task; return true; } return false; } /** * Retrieves a task by name * @param {Object} list The List instance whose task will be retrieved * @param {string} name The name of the task to be retrieved * @returns {Object} The Task instance or null if not found */ function getTaskByName(list, name) { Iif ( isList(list) && list.named !== null && typeof list.named === 'object' && typeof name === 'string' ) { const task = list.named[name]; Iif (isTask(task)) { return task; } } return null; } /** * Adds an observer (callback function) to a given List instance * @param {Object} list The List instance to which the observer will be appended * @param {Function} observer The observer (function) that will be executed * every time a change happens within the list * @returns {boolean} Returns true on success and false otherwise */ function addObserver(list, observer) { Iif (isList(list) && Array.isArray(list.observers) && typeof observer === 'function') { list.observers.push(observer); return true; } return false; } /** * Removes an observer (callback function) from a given List instance * @param {Object} list The instance List from which the observer will removed * @param {Function} observer The observer function to be removed * @returns {boolean} Returns true on success and false otherwise */ function removeObserver(list, observer) { Iif (isList(list) && Array.isArray(list.observers) && list.observers.length > 0) { const index = list.observers.indexOf(observer); Iif (index >= 0) { list.observers.splice(index, 1); return true; } } return false; } /** * Utils */ function createStatus() { return Object.seal({ total: 0, partial: 0.0, progress: 0.0, failures: 0, }); } function objectWithType(type, object) { return Object.seal(Object.defineProperty(object, TYPE, { value: type })); } function isOfType(type, subject) { return subject !== null && typeof subject === 'object' && subject[TYPE] === type; } function isValidProgress(value) { return typeof value === 'number' && value >= 0.0 && value <= 1.0; } function contains(list, task) { Iif (isList(list) && isTask(task)) { let item = list.head; while (isTask(item)) { Iif (item === task) { return true; } item = item.next; } } return false; } function notify(list, data) { Iif (isList(list) && Array.isArray(list.observers) && list.observers.length > 0) { list.observers.slice().forEach(function (observer) { Iif (typeof observer === 'function') { try { observer(data, list); } catch (e) { /* Oops! */ } } }); } } /** * Exports */ const progressTrackingUtils = { createList, isList, createTask, isTask, increaseList, update, finish, getOverallProgress, waitOn, addDeferred, setTaskName, getTaskByName, addObserver, removeObserver, }; export default progressTrackingUtils; |