Skip to content

Commit b12ba43

Browse files
author
Flare
committed
Add more examples
1 parent 98e4716 commit b12ba43

5 files changed

Lines changed: 99 additions & 3 deletions

File tree

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,60 @@ setTimeout(function(){
3434
}, 1000)
3535
```
3636

37-
## Example(More Complex)
37+
## Example: Multi Worker
38+
39+
```js
40+
'use strict'
41+
const WaitQueue = require('wait-queue')
42+
const wq = new WaitQueue
43+
44+
// worker loop
45+
function run_worker(id, time){
46+
var loop = function(){
47+
// get item at the front of the queue
48+
wq.shift()
49+
.then((item)=>{
50+
console.log('worker-' + id)
51+
console.log(' queue-len', wq.queue.length, 'item', item)
52+
setTimeout(loop, time)
53+
})
54+
}
55+
loop()
56+
}
57+
58+
// worker-a use 1s every task
59+
run_worker('a', 1000)
60+
// worker-b use 2s every task
61+
run_worker('b', 2000)
62+
// worker-c use 5s every task
63+
run_worker('c', 5000)
64+
65+
// add a task every 500ms
66+
for(var n=0; n<20; n++){
67+
wq.push(n)
68+
}
69+
```
70+
71+
## Example: Push a function
72+
73+
```js
74+
'use strict'
75+
const WaitQueue = require('wait-queue')
76+
const wq = new WaitQueue
77+
78+
// there's no task here
79+
wq.shift()
80+
.then((item)=>{
81+
item.call()
82+
})
83+
84+
// add a function as item
85+
wq.push(function(){
86+
console.log('a function')
87+
})
88+
```
89+
90+
## Example: Loop Tasks
3891

3992
```js
4093
'use strict'

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class WaitQueue extends EventEmitter{
2828
}else{
2929
if(this.queue.length > 0 && this.listeners.length > 0){
3030
let listener = this.listeners.shift()
31-
// delay listener
3231
listener.call(this)
32+
// delay next loop
3333
setImmediate(this._flush.bind(this))
3434
}
3535
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wait-queue",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A javascript wait queue object handle infinity loop tasks more efficiently (using ES6 class and promise)",
55
"main": "index.js",
66
"directories": {

tests/test_callback.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
const WaitQueue = require('../index')
3+
const wq = new WaitQueue
4+
5+
// there's no task here
6+
wq.shift()
7+
.then((item)=>{
8+
item.call()
9+
})
10+
11+
// add a function as item
12+
wq.push(function(){
13+
console.log('a function')
14+
})

tests/test_multiworker.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
const WaitQueue = require('../index')
3+
const wq = new WaitQueue
4+
5+
// worker loop
6+
function run_worker(id, time){
7+
var loop = function(){
8+
// get item at the front of the queue
9+
wq.shift()
10+
.then((item)=>{
11+
console.log('worker-' + id)
12+
console.log(' queue-len', wq.queue.length, 'item', item)
13+
setTimeout(loop, time)
14+
})
15+
}
16+
loop()
17+
}
18+
19+
// worker-a use 1s every task
20+
run_worker('a', 1000)
21+
// worker-b use 2s every task
22+
run_worker('b', 2000)
23+
// worker-c use 5s every task
24+
run_worker('c', 5000)
25+
26+
// add a task every 500ms
27+
for(var n=0; n<20; n++){
28+
wq.push(n)
29+
}

0 commit comments

Comments
 (0)