TrinetraLabs/Placebo_AI
0
1```2 ____ __ _____ __ _____3 / __ `/ / / / _ \/ / / / _ \4 / /_/ / /_/ / __/ /_/ / __/5 \__, /\__,_/\___/\__,_/\___/6 /_/7```8Asynchronous function queue with adjustable concurrency.9 10[](http://www.npmjs.org/queue)11[](https://travis-ci.org/jessetane/queue)12[](https://coveralls.io/r/jessetane/queue)13 14This module exports a class `Queue` that implements most of the `Array` API. Pass async functions (ones that accept a callback or return a promise) to an instance's additive array methods. Processing begins when you call `q.start()`.15 16## Example17`npm run example`18``` javascript19var queue = require('../')20 21var q = queue({ results: [] })22 23// add jobs using the familiar Array API24q.push(function (cb) {25 const result = 'two'26 cb(null, result)27})28 29q.push(30 function (cb) {31 const result = 'four'32 cb(null, result)33 },34 function (cb) {35 const result = 'five'36 cb(null, result)37 }38)39 40// jobs can accept a callback or return a promise41q.push(function () {42 return new Promise(function (resolve, reject) {43 const result = 'one'44 resolve(result)45 })46})47 48q.unshift(function (cb) {49 const result = 'one'50 cb(null, result)51})52 53q.splice(2, 0, function (cb) {54 const result = 'three'55 cb(null, result)56})57 58// use the timeout feature to deal with jobs that59// take too long or forget to execute a callback60q.timeout = 10061 62q.on('timeout', function (next, job) {63 console.log('job timed out:', job.toString().replace(/\n/g, ''))64 next()65})66 67q.push(function (cb) {68 setTimeout(function () {69 console.log('slow job finished')70 cb()71 }, 200)72})73 74q.push(function (cb) {75 console.log('forgot to execute callback')76})77 78// jobs can also override the queue's timeout79// on a per-job basis80function extraSlowJob (cb) {81 setTimeout(function () {82 console.log('extra slow job finished')83 cb()84 }, 400)85}86 87extraSlowJob.timeout = 50088q.push(extraSlowJob)89 90// jobs can also opt-out of the timeout altogether91function superSlowJob (cb) {92 setTimeout(function () {93 console.log('super slow job finished')94 cb()95 }, 1000)96}97 98superSlowJob.timeout = null99q.push(superSlowJob)100 101// get notified when jobs complete102q.on('success', function (result, job) {103 console.log('job finished processing:', job.toString().replace(/\n/g, ''))104 console.log('The result is:', result)105})106 107// begin processing, get notified on end / failure108q.start(function (err) {109 if (err) throw err110 console.log('all done:', q.results)111})112 113```114 115## Install116`npm install queue`117 118_Note_: You may need to install the [`events`](https://github.com/Gozala/events) dependency if 119your environment does not have it by default (eg. browser, react-native). 120 121## Test122`npm test`123`npm run test-browser`124 125## API126 127### `var q = queue([opts])`128Constructor. `opts` may contain inital values for:129* `q.concurrency`130* `q.timeout`131* `q.autostart`132* `q.results`133 134## Instance methods135### `q.start([cb])`136cb, if passed, will be called when the queue empties or when an error occurs.137 138### `q.stop()`139Stops the queue. can be resumed with `q.start()`.140 141### `q.end([err])`142Stop and empty the queue immediately.143 144## Instance methods mixed in from `Array`145Mozilla has docs on how these methods work [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). Note that `slice` does not copy the queue.146### `q.push(element1, ..., elementN)`147### `q.unshift(element1, ..., elementN)`148### `q.splice(index , howMany[, element1[, ...[, elementN]]])`149### `q.pop()`150### `q.shift()`151### `q.slice(begin[, end])`152### `q.reverse()`153### `q.indexOf(searchElement[, fromIndex])`154### `q.lastIndexOf(searchElement[, fromIndex])`155 156## Properties157### `q.concurrency`158Max number of jobs the queue should process concurrently, defaults to `Infinity`.159 160### `q.timeout`161Milliseconds to wait for a job to execute its callback. This can be overridden by specifying a `timeout` property on a per-job basis.162 163### `q.autostart`164Ensures the queue is always running if jobs are available. Useful in situations where you are using a queue only for concurrency control.165 166### `q.results`167An array to set job callback arguments on.168 169### `q.length`170Jobs pending + jobs to process (readonly).171 172## Events173 174### `q.emit('start', job)`175Immediately before a job begins to execute.176 177### `q.emit('success', result, job)`178After a job executes its callback.179 180### `q.emit('error', err, job)`181After a job passes an error to its callback.182 183### `q.emit('timeout', continue, job)`184After `q.timeout` milliseconds have elapsed and a job has not executed its callback.185 186### `q.emit('end'[, err])`187After all jobs have been processed188 189## Releases190The latest stable release is published to [npm](http://npmjs.org/queue). Abbreviated changelog below:191* [6.0](https://github.com/jessetane/queue/archive/6.0.1.tar.gz)192 * Add `start` event before job begins (@joelgriffith)193 * Add `timeout` property on a job to override the queue's timeout (@joelgriffith)194* [5.0](https://github.com/jessetane/queue/archive/5.0.0.tar.gz)195 * Updated TypeScript bindings (@Codex-)196* [4.4](https://github.com/jessetane/queue/archive/4.4.0.tar.gz)197 * Add results feature198* [4.3](https://github.com/jessetane/queue/archive/4.3.0.tar.gz)199 * Add promise support (@kwolfy)200* [4.2](https://github.com/jessetane/queue/archive/4.2.0.tar.gz)201 * Unref timers on end202* [4.1](https://github.com/jessetane/queue/archive/4.1.0.tar.gz)203 * Add autostart feature204* [4.0](https://github.com/jessetane/queue/archive/4.0.0.tar.gz)205 * Change license to MIT206* [3.1.x](https://github.com/jessetane/queue/archive/3.0.6.tar.gz)207 * Add .npmignore208* [3.0.x](https://github.com/jessetane/queue/archive/3.0.6.tar.gz)209 * Change the default concurrency to `Infinity`210 * Allow `q.start()` to accept an optional callback executed on `q.emit('end')`211* [2.x](https://github.com/jessetane/queue/archive/2.2.0.tar.gz)212 * Major api changes / not backwards compatible with 1.x213* [1.x](https://github.com/jessetane/queue/archive/1.0.2.tar.gz)214 * Early prototype215 216## License217Copyright © 2014 Jesse Tane <jesse.tane@gmail.com>218 219This work is free. You can redistribute it and/or modify it under the220terms of the [MIT License](https://opensource.org/licenses/MIT).221See LICENSE for full details.222 