| [9af201e] | 1 | import * as fastq from '../'
|
|---|
| 2 | import { promise as queueAsPromised } from '../'
|
|---|
| 3 |
|
|---|
| 4 | // Basic example
|
|---|
| 5 |
|
|---|
| 6 | const queue = fastq(worker, 1)
|
|---|
| 7 |
|
|---|
| 8 | queue.push('world', (err, result) => {
|
|---|
| 9 | if (err) throw err
|
|---|
| 10 | console.log('the result is', result)
|
|---|
| 11 | })
|
|---|
| 12 |
|
|---|
| 13 | queue.push('push without cb')
|
|---|
| 14 |
|
|---|
| 15 | queue.concurrency
|
|---|
| 16 |
|
|---|
| 17 | queue.drain()
|
|---|
| 18 |
|
|---|
| 19 | queue.empty = () => undefined
|
|---|
| 20 |
|
|---|
| 21 | console.log('the queue tasks are', queue.getQueue())
|
|---|
| 22 |
|
|---|
| 23 | queue.idle()
|
|---|
| 24 |
|
|---|
| 25 | queue.kill()
|
|---|
| 26 |
|
|---|
| 27 | queue.killAndDrain()
|
|---|
| 28 |
|
|---|
| 29 | queue.length
|
|---|
| 30 |
|
|---|
| 31 | queue.pause()
|
|---|
| 32 |
|
|---|
| 33 | queue.resume()
|
|---|
| 34 |
|
|---|
| 35 | queue.running()
|
|---|
| 36 |
|
|---|
| 37 | queue.saturated = () => undefined
|
|---|
| 38 |
|
|---|
| 39 | queue.unshift('world', (err, result) => {
|
|---|
| 40 | if (err) throw err
|
|---|
| 41 | console.log('the result is', result)
|
|---|
| 42 | })
|
|---|
| 43 |
|
|---|
| 44 | queue.unshift('unshift without cb')
|
|---|
| 45 |
|
|---|
| 46 | function worker(task: any, cb: fastq.done) {
|
|---|
| 47 | cb(null, 'hello ' + task)
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | // Generics example
|
|---|
| 51 |
|
|---|
| 52 | interface GenericsContext {
|
|---|
| 53 | base: number;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | const genericsQueue = fastq<GenericsContext, number, string>({ base: 6 }, genericsWorker, 1)
|
|---|
| 57 |
|
|---|
| 58 | genericsQueue.push(7, (err, done) => {
|
|---|
| 59 | if (err) throw err
|
|---|
| 60 | console.log('the result is', done)
|
|---|
| 61 | })
|
|---|
| 62 |
|
|---|
| 63 | genericsQueue.unshift(7, (err, done) => {
|
|---|
| 64 | if (err) throw err
|
|---|
| 65 | console.log('the result is', done)
|
|---|
| 66 | })
|
|---|
| 67 |
|
|---|
| 68 | function genericsWorker(this: GenericsContext, task: number, cb: fastq.done<string>) {
|
|---|
| 69 | cb(null, 'the meaning of life is ' + (this.base * task))
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | const queue2 = queueAsPromised(asyncWorker, 1)
|
|---|
| 73 |
|
|---|
| 74 | async function asyncWorker(task: any) {
|
|---|
| 75 | return 'hello ' + task
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | async function run () {
|
|---|
| 79 | await queue.push(42)
|
|---|
| 80 | await queue.unshift(42)
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | run()
|
|---|