| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | import * as fastq from '../' |
| 2 | |
| 3 | // Basic example |
| 4 | |
| 5 | const queue = fastq(worker, 1) |
| 6 | |
| 7 | queue.push('world', (err, result) => { |
| 8 | if (err) throw err |
| 9 | console.log('the result is', result) |
| 10 | }) |
| 11 | |
| 12 | queue.concurrency |
| 13 | |
| 14 | queue.drain() |
| 15 | |
| 16 | queue.empty = () => undefined |
| 17 | |
| 18 | console.log('the queue tasks are', queue.getQueue()) |
| 19 | |
| 20 | queue.idle() |
| 21 | |
| 22 | queue.kill() |
| 23 | |
| 24 | queue.killAndDrain() |
| 25 | |
| 26 | queue.length |
| 27 | |
| 28 | queue.pause() |
| 29 | |
| 30 | queue.resume() |
| 31 | |
| 32 | queue.saturated = () => undefined |
| 33 | |
| 34 | queue.unshift('world', (err, result) => { |
| 35 | if (err) throw err |
| 36 | console.log('the result is', result) |
| 37 | }) |
| 38 | |
| 39 | function worker(task: any, cb: fastq.done) { |
| 40 | cb(null, 'hello ' + task) |
| 41 | } |
| 42 | |
| 43 | // Generics example |
| 44 | |
| 45 | interface GenericsContext { |
| 46 | base: number; |
| 47 | } |
| 48 | |
| 49 | const genericsQueue = fastq<GenericsContext, number, string>({ base: 6 }, genericsWorker, 1) |
| 50 | |
| 51 | genericsQueue.push(7, (err, done) => { |
| 52 | if (err) throw err |
| 53 | console.log('the result is', done) |
| 54 | }) |
| 55 | |
| 56 | genericsQueue.unshift(7, (err, done) => { |
| 57 | if (err) throw err |
| 58 | console.log('the result is', done) |
| 59 | }) |
| 60 | |
| 61 | function genericsWorker(this: GenericsContext, task: number, cb: fastq.done<string>) { |
| 62 | cb(null, 'the meaning of life is ' + (this.base * task)) |
| 63 | } |