Skip to content

Commit 220d464

Browse files
committed
Improve named work handler.
Signed-off-by: Toha <[email protected]>
1 parent d0b3400 commit 220d464

File tree

6 files changed

+53
-40
lines changed

6 files changed

+53
-40
lines changed

README.md

+19-8
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,40 @@ const q = new Queue(queues, seq => {
1212
console.log(seq);
1313
q.next();
1414
});
15+
q.once('done', () => {
16+
...
17+
});
1518
```
1619

1720
## Promise Based Work Queue (work.js)
1821

1922
Provide promise queue mechanism for easy chaining. It accepts a function as its
20-
worker. Its also accepts an array with signature of `[function, function]` which
21-
the first element would be the worker and the second would be a state function
22-
and must be evaluated to true for worker to be executed.
23+
worker. Its also accepts an array with signature of `[[string,] function, function]`.
24+
If the first element is a string, it is considered as step name and can be used to
25+
reference the result later, otherwise the first element would be the worker and
26+
the second would be a state function and must be evaluated to true for worker to
27+
be executed.
2328

2429
```js
2530
const { Work } = require('@ntlab/work');
2631
Work.works([
27-
[w => new Promise((resolve, reject) => {
32+
['step-1', w => new Promise((resolve, reject) => {
2833
console.log('First work');
2934
resolve(false);
3035
})],
31-
[w => new Promise((resolve, reject) => {
36+
['step-2', w => new Promise((resolve, reject) => {
3237
console.log('This will be skipped');
3338
resolve();
34-
}), w => w.getRes(0)],
35-
[w => new Promise((resolve, reject) => {
39+
}), w => w.getRes(0)/* can be referenced using w.getRes('step-1') */],
40+
['step-3', w => new Promise((resolve, reject) => {
3641
console.log('It\'s done');
3742
resolve();
3843
})],
39-
]);
44+
])
45+
.then(res => {
46+
...
47+
})
48+
.catch(err => {
49+
...
50+
});
4051
```

index.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-2024 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

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.5",
3+
"version": "2.0.0",
44
"description": "Nodejs queue and promise based work queue",
55
"main": "index.js",
66
"scripts": {

queue.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2022-2023 Toha <[email protected]>
4+
* Copyright (c) 2022-2024 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
@@ -49,7 +49,7 @@ class Queue extends EventEmitter {
4949
next() {
5050
if (this.queues.length) {
5151
if (this.pending) return;
52-
if (typeof this.check == 'function') {
52+
if (typeof this.check === 'function') {
5353
if (!this.check()) return;
5454
}
5555
this.consume(this.queues.shift());
@@ -70,7 +70,7 @@ class Queue extends EventEmitter {
7070
}
7171

7272
requeue(queues, top) {
73-
const processNext = this.queues.length == 0 && this.queue == null;
73+
const processNext = this.queues.length === 0 && this.queue === null;
7474
if (top) {
7575
this.queues.unshift(...queues);
7676
} else {

work.js

+27-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2022-2023 Toha <[email protected]>
4+
* Copyright (c) 2022-2024 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
@@ -24,7 +24,6 @@
2424

2525
const EventEmitter = require('events');
2626
const debug = require('debug')('work');
27-
const util = require('util');
2827

2928
let seq = 0;
3029
let dbg = x => x;
@@ -67,33 +66,33 @@ class Work extends EventEmitter {
6766
}
6867

6968
getRes(idx) {
70-
if (typeof idx == 'string') {
69+
if (typeof idx === 'string') {
7170
let sidx = this.names[idx];
72-
if (sidx == undefined) {
73-
throw new Error(util.format('Named index %s doesn\'t exist!', idx));
71+
if (sidx === undefined) {
72+
throw new Error(`Named index ${idx} doesn't exist!`);
7473
}
7574
idx = sidx;
7675
}
7776
if (idx < 0 || idx >= this.result.length) {
78-
throw new Error(util.format('Index %d is out of bound!', idx));
77+
throw new Error(`Index ${idx} is out of bound!`);
7978
}
8079
return this.result[idx];
8180
}
8281

8382
static works(workers, options) {
84-
if (typeof options == 'undefined') {
83+
if (typeof options === 'undefined') {
8584
options = {};
8685
}
87-
if (typeof options == 'function') {
86+
if (typeof options === 'function') {
8887
options = {callback: options};
8988
}
90-
const d = x => typeof options.dbg == 'function' ? options.dbg(x) : dbg(x);
89+
const d = x => typeof options.dbg === 'function' ? options.dbg(x) : dbg(x);
9190
const w = new this(workers);
9291
return new Promise((resolve, reject) => {
9392
let id = ++seq;
9493
// always handler, called both on resolve and on reject
9594
const always = err => new Promise((resolve, reject) => {
96-
if (typeof options.done == 'function') {
95+
if (typeof options.done === 'function') {
9796
options.done(w, err)
9897
.then(() => resolve())
9998
.catch(err => reject(err))
@@ -107,7 +106,7 @@ class Work extends EventEmitter {
107106
w.result.push(res);
108107
w.pres = w.res;
109108
w.res = res;
110-
if (w.works.length == 0) {
109+
if (w.works.length === 0) {
111110
always()
112111
.then(() => {
113112
debug('%d> [%d] resolved with %s', id, idx, d(w.rres));
@@ -117,7 +116,7 @@ class Work extends EventEmitter {
117116
;
118117
} else {
119118
w.once('work', f);
120-
if (typeof options.callback == 'function') {
119+
if (typeof options.callback === 'function') {
121120
options.callback(() => w.next());
122121
} else {
123122
w.next();
@@ -134,7 +133,7 @@ class Work extends EventEmitter {
134133
resolve();
135134
} else {
136135
debug('%d> [%d] rejected with %s', id, idx, d(err));
137-
if (typeof options.onerror == 'function') {
136+
if (typeof options.onerror === 'function') {
138137
options.onerror(w);
139138
}
140139
reject(err);
@@ -167,15 +166,15 @@ class Work extends EventEmitter {
167166
;
168167
}
169168
} catch (err) {
170-
if (winfo && options.onerror == undefined) {
169+
if (winfo && options.onerror === undefined) {
171170
console.error('Got error %s:\n%s', err instanceof Error ? err.toString() : err, winfo);
172171
}
173172
stop(idx, err);
174173
}
175174
}
176175
w.once('work', f);
177176
// guard against empty work
178-
if (workers.length == 0) {
177+
if (workers.length === 0) {
179178
always()
180179
.then(() => {
181180
debug('%d> [-] empty work, resolving instead', id);
@@ -188,7 +187,7 @@ class Work extends EventEmitter {
188187
}
189188

190189
static debug(f) {
191-
if (typeof f == 'function') {
190+
if (typeof f === 'function') {
192191
dbg = f;
193192
}
194193
}
@@ -197,28 +196,31 @@ class Work extends EventEmitter {
197196
class Worker
198197
{
199198
constructor(work) {
200-
if (typeof work == 'function') {
199+
if (typeof work === 'function') {
201200
this.handler = work;
202201
}
203202
if (Array.isArray(work)) {
204-
if (typeof work[0] != 'function') {
205-
throw Error('First element of worker must be function!');
203+
if (typeof work[0] === 'string') {
204+
this.name = work.shift();
205+
}
206+
if (typeof work[0] !== 'function') {
207+
throw Error(`Worker handler must be function, got ${typeof work[0]}!`);
206208
}
207209
this.handler = work[0];
208210
if (work.length > 1) {
209-
if (typeof work[1] != 'function') {
210-
throw Error('Second element of worker must be function!');
211+
if (typeof work[1] !== 'function') {
212+
throw Error(`Worker state handler must be function, got ${typeof work[1]}!`);
211213
}
212214
this.enabled = work[1];
213215
}
214-
if (work.length > 2) {
215-
this.name = work[2];
216-
}
216+
}
217+
if (typeof this.handler !== 'function') {
218+
throw Error('Worker handler is required!');
217219
}
218220
}
219221

220222
isEnabled(caller) {
221-
return typeof this.enabled == 'function' ? this.enabled(caller) : true;
223+
return typeof this.enabled === 'function' ? this.enabled(caller) : true;
222224
}
223225

224226
get info() {

0 commit comments

Comments
 (0)