1
1
/**
2
2
* The MIT License (MIT)
3
3
*
4
- * Copyright (c) 2022-2023 Toha <[email protected] >
4
+ * Copyright (c) 2022-2024 Toha <[email protected] >
5
5
*
6
6
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7
7
* this software and associated documentation files (the "Software"), to deal in
24
24
25
25
const EventEmitter = require ( 'events' ) ;
26
26
const debug = require ( 'debug' ) ( 'work' ) ;
27
- const util = require ( 'util' ) ;
28
27
29
28
let seq = 0 ;
30
29
let dbg = x => x ;
@@ -67,33 +66,33 @@ class Work extends EventEmitter {
67
66
}
68
67
69
68
getRes ( idx ) {
70
- if ( typeof idx == 'string' ) {
69
+ if ( typeof idx === 'string' ) {
71
70
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!` ) ;
74
73
}
75
74
idx = sidx ;
76
75
}
77
76
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!` ) ;
79
78
}
80
79
return this . result [ idx ] ;
81
80
}
82
81
83
82
static works ( workers , options ) {
84
- if ( typeof options == 'undefined' ) {
83
+ if ( typeof options === 'undefined' ) {
85
84
options = { } ;
86
85
}
87
- if ( typeof options == 'function' ) {
86
+ if ( typeof options === 'function' ) {
88
87
options = { callback : options } ;
89
88
}
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 ) ;
91
90
const w = new this ( workers ) ;
92
91
return new Promise ( ( resolve , reject ) => {
93
92
let id = ++ seq ;
94
93
// always handler, called both on resolve and on reject
95
94
const always = err => new Promise ( ( resolve , reject ) => {
96
- if ( typeof options . done == 'function' ) {
95
+ if ( typeof options . done === 'function' ) {
97
96
options . done ( w , err )
98
97
. then ( ( ) => resolve ( ) )
99
98
. catch ( err => reject ( err ) )
@@ -107,7 +106,7 @@ class Work extends EventEmitter {
107
106
w . result . push ( res ) ;
108
107
w . pres = w . res ;
109
108
w . res = res ;
110
- if ( w . works . length == 0 ) {
109
+ if ( w . works . length === 0 ) {
111
110
always ( )
112
111
. then ( ( ) => {
113
112
debug ( '%d> [%d] resolved with %s' , id , idx , d ( w . rres ) ) ;
@@ -117,7 +116,7 @@ class Work extends EventEmitter {
117
116
;
118
117
} else {
119
118
w . once ( 'work' , f ) ;
120
- if ( typeof options . callback == 'function' ) {
119
+ if ( typeof options . callback === 'function' ) {
121
120
options . callback ( ( ) => w . next ( ) ) ;
122
121
} else {
123
122
w . next ( ) ;
@@ -134,7 +133,7 @@ class Work extends EventEmitter {
134
133
resolve ( ) ;
135
134
} else {
136
135
debug ( '%d> [%d] rejected with %s' , id , idx , d ( err ) ) ;
137
- if ( typeof options . onerror == 'function' ) {
136
+ if ( typeof options . onerror === 'function' ) {
138
137
options . onerror ( w ) ;
139
138
}
140
139
reject ( err ) ;
@@ -167,15 +166,15 @@ class Work extends EventEmitter {
167
166
;
168
167
}
169
168
} catch ( err ) {
170
- if ( winfo && options . onerror == undefined ) {
169
+ if ( winfo && options . onerror === undefined ) {
171
170
console . error ( 'Got error %s:\n%s' , err instanceof Error ? err . toString ( ) : err , winfo ) ;
172
171
}
173
172
stop ( idx , err ) ;
174
173
}
175
174
}
176
175
w . once ( 'work' , f ) ;
177
176
// guard against empty work
178
- if ( workers . length == 0 ) {
177
+ if ( workers . length === 0 ) {
179
178
always ( )
180
179
. then ( ( ) => {
181
180
debug ( '%d> [-] empty work, resolving instead' , id ) ;
@@ -188,7 +187,7 @@ class Work extends EventEmitter {
188
187
}
189
188
190
189
static debug ( f ) {
191
- if ( typeof f == 'function' ) {
190
+ if ( typeof f === 'function' ) {
192
191
dbg = f ;
193
192
}
194
193
}
@@ -197,28 +196,31 @@ class Work extends EventEmitter {
197
196
class Worker
198
197
{
199
198
constructor ( work ) {
200
- if ( typeof work == 'function' ) {
199
+ if ( typeof work === 'function' ) {
201
200
this . handler = work ;
202
201
}
203
202
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 ] } !` ) ;
206
208
}
207
209
this . handler = work [ 0 ] ;
208
210
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 ] } !` ) ;
211
213
}
212
214
this . enabled = work [ 1 ] ;
213
215
}
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!' ) ;
217
219
}
218
220
}
219
221
220
222
isEnabled ( caller ) {
221
- return typeof this . enabled == 'function' ? this . enabled ( caller ) : true ;
223
+ return typeof this . enabled === 'function' ? this . enabled ( caller ) : true ;
222
224
}
223
225
224
226
get info ( ) {
0 commit comments