Skip to content

Commit 7037bd9

Browse files
committed
Improve queue consuming.
Signed-off-by: Toha <[email protected]>
1 parent 9929619 commit 7037bd9

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ntlab/work",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "Nodejs queue and promise based work queue",
55
"main": "index.js",
66
"scripts": {

queue.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2022 Toha <[email protected]>
4+
* Copyright (c) 2022-2023 Toha <[email protected]>
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy of
77
* this software and associated documentation files (the "Software"), to deal in
@@ -22,7 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25-
const EventEmitter = require('events');
25+
const EventEmitter = require('events');
2626

2727
/**
2828
* Queue processing.
@@ -38,15 +38,12 @@ class Queue extends EventEmitter {
3838
this.next();
3939
}
4040

41-
applyHandler() {
42-
this.once('queue', this.handler);
43-
}
44-
45-
start() {
41+
consume(queue) {
4642
process.nextTick(() => {
47-
if (this.queues.length) {
48-
this.queue = this.queues.shift();
49-
this.emit('queue', this.queue);
43+
if (queue) {
44+
this.queue = queue;
45+
this.once('queue', this.handler);
46+
this.emit('queue', queue);
5047
}
5148
});
5249
}
@@ -57,8 +54,7 @@ class Queue extends EventEmitter {
5754
if (typeof this.check == 'function') {
5855
if (!this.check()) return;
5956
}
60-
this.applyHandler();
61-
this.start();
57+
this.consume(this.queues.shift());
6258
} else {
6359
this.done();
6460
}

work.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2022 Toha <[email protected]>
4+
* Copyright (c) 2022-2023 Toha <[email protected]>
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy of
77
* this software and associated documentation files (the "Software"), to deal in

0 commit comments

Comments
 (0)