Skip to content

Commit 7420567

Browse files
committed
Improve code style
1 parent bae8688 commit 7420567

File tree

8 files changed

+51
-79
lines changed

8 files changed

+51
-79
lines changed

JavaScript/1-channels.js

+8-17
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@ class Queue {
1818

1919
add(task) {
2020
const hasChannel = this.count < this.concurrency;
21-
if (hasChannel) {
22-
this.next(task);
23-
return;
24-
}
21+
if (hasChannel) return void this.next(task);
2522
this.waiting.push(task);
2623
}
2724

2825
next(task) {
2926
this.count++;
30-
this.onProcess(task, (err, result) => {
31-
if (err) {
32-
if (this.onFailure) this.onFailure(err);
27+
this.onProcess(task, (error, result) => {
28+
if (error) {
29+
if (this.onFailure) this.onFailure(error);
3330
} else if (this.onSuccess) {
3431
this.onSuccess(result);
3532
}
36-
if (this.onDone) this.onDone(err, result);
33+
if (this.onDone) this.onDone(error, result);
3734
this.count--;
3835
if (this.waiting.length > 0) {
3936
const task = this.waiting.shift();
@@ -87,15 +84,9 @@ const queue = Queue.channels(3)
8784
const waiting = queue.waiting.length;
8885
console.log(`Done: ${res.name}, count:${count}, waiting: ${waiting}`);
8986
})
90-
.success((res) => {
91-
console.log(`Success: ${res.name}`);
92-
})
93-
.failure((err) => {
94-
console.log(`Failure: ${err}`);
95-
})
96-
.drain(() => {
97-
console.log('Queue drain');
98-
});
87+
.success((res) => void console.log(`Success: ${res.name}`))
88+
.failure((error) => void console.log(`Failure: ${error}`))
89+
.drain(() => void console.log('Queue drain'));
9990

10091
for (let i = 0; i < 10; i++) {
10192
queue.add({ name: `Task${i}`, interval: i * 1000 });

JavaScript/2-timeouts.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ class Queue {
3030

3131
add(task) {
3232
const hasChannel = this.count < this.concurrency;
33-
if (hasChannel) {
34-
this.next(task);
35-
return;
36-
}
33+
if (hasChannel) return void this.next(task);
3734
this.waiting.push({ task, start: Date.now() });
3835
}
3936

@@ -42,19 +39,19 @@ class Queue {
4239
let timer = null;
4340
let finished = false;
4441
const { processTimeout, onProcess } = this;
45-
const finish = (err, res) => {
42+
const finish = (error, res) => {
4643
if (finished) return;
4744
finished = true;
4845
if (timer) clearTimeout(timer);
4946
this.count--;
50-
this.finish(err, res);
47+
this.finish(error, res);
5148
if (this.waiting.length > 0) this.takeNext();
5249
};
5350
if (processTimeout !== Infinity) {
5451
timer = setTimeout(() => {
5552
timer = null;
56-
const err = new Error('Process timed out');
57-
finish(err, task);
53+
const error = new Error('Process timed out');
54+
finish(error, task);
5855
}, processTimeout);
5956
}
6057
onProcess(task, finish);
@@ -66,24 +63,23 @@ class Queue {
6663
if (waitTimeout !== Infinity) {
6764
const delay = Date.now() - start;
6865
if (delay > waitTimeout) {
69-
const err = new Error('Waiting timed out');
70-
this.finish(err, task);
66+
const error = new Error('Waiting timed out');
67+
this.finish(error, task);
7168
if (waiting.length > 0) this.takeNext();
7269
return;
7370
}
7471
}
7572
this.next(task);
76-
return;
7773
}
7874

79-
finish(err, res) {
75+
finish(error, res) {
8076
const { onFailure, onSuccess, onDone, onDrain } = this;
81-
if (err) {
82-
if (onFailure) onFailure(err, res);
77+
if (error) {
78+
if (onFailure) onFailure(error, res);
8379
} else if (onSuccess) {
8480
onSuccess(res);
8581
}
86-
if (onDone) onDone(err, res);
82+
if (onDone) onDone(error, res);
8783
if (this.count === 0 && onDrain) onDrain();
8884
}
8985

@@ -123,9 +119,9 @@ const queue = Queue.channels(3)
123119
.wait(4000)
124120
.timeout(5000)
125121
.process(job)
126-
.success((task) => console.log(`Success: ${task.name}`))
127-
.failure((err, task) => console.log(`Failure: ${err} ${task.name}`))
128-
.drain(() => console.log('Queue drain'));
122+
.success((task) => void console.log(`Success: ${task.name}`))
123+
.failure((error, task) => void console.log(`Failure: ${error} ${task.name}`))
124+
.drain(() => void console.log('Queue drain'));
129125

130126
for (let i = 0; i < 10; i++) {
131127
queue.add({ name: `Task${i}`, interval: i * 1000 });

JavaScript/3-pause.js

+13-21
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ class Queue {
3232
add(task) {
3333
if (!this.paused) {
3434
const hasChannel = this.count < this.concurrency;
35-
if (hasChannel) {
36-
this.next(task);
37-
return;
38-
}
35+
if (hasChannel) return void this.next(task);
3936
}
4037
this.waiting.push({ task, start: Date.now() });
4138
}
@@ -45,17 +42,17 @@ class Queue {
4542
let timer = null;
4643
let finished = false;
4744
const { processTimeout, onProcess } = this;
48-
const finish = (err, res) => {
45+
const finish = (error, res) => {
4946
if (finished) return;
5047
finished = true;
5148
if (timer) clearTimeout(timer);
5249
this.count--;
53-
this.finish(err, res);
50+
this.finish(error, res);
5451
if (!this.paused && this.waiting.length > 0) this.takeNext();
5552
};
5653
if (processTimeout !== Infinity) {
57-
const err = new Error('Process timed out');
58-
timer = setTimeout(finish, processTimeout, err, task);
54+
const error = new Error('Process timed out');
55+
timer = setTimeout(finish, processTimeout, error, task);
5956
}
6057
onProcess(task, finish);
6158
}
@@ -66,8 +63,8 @@ class Queue {
6663
if (waitTimeout !== Infinity) {
6764
const delay = Date.now() - start;
6865
if (delay > waitTimeout) {
69-
const err = new Error('Waiting timed out');
70-
this.finish(err, task);
66+
const error = new Error('Waiting timed out');
67+
this.finish(error, task);
7168
if (waiting.length > 0) {
7269
setTimeout(() => {
7370
if (!this.paused && waiting.length > 0) this.takeNext();
@@ -78,17 +75,16 @@ class Queue {
7875
}
7976
const hasChannel = this.count < this.concurrency;
8077
if (hasChannel) this.next(task);
81-
return;
8278
}
8379

84-
finish(err, res) {
80+
finish(error, res) {
8581
const { onFailure, onSuccess, onDone, onDrain } = this;
86-
if (err) {
87-
if (onFailure) onFailure(err, res);
82+
if (error) {
83+
if (onFailure) onFailure(error, res);
8884
} else if (onSuccess) {
8985
onSuccess(res);
9086
}
91-
if (onDone) onDone(err, res);
87+
if (onDone) onDone(error, res);
9288
if (this.count === 0 && onDrain) onDrain();
9389
}
9490

@@ -144,12 +140,8 @@ const queue = Queue.channels(3)
144140
.wait(4000)
145141
.timeout(5000)
146142
.process(job)
147-
.success((task) => {
148-
console.log(`Success: ${task.name}`);
149-
})
150-
.failure((error, task) => {
151-
console.log(`Failure: ${error} ${task.name}`);
152-
})
143+
.success((task) => void console.log(`Success: ${task.name}`))
144+
.failure((error, task) => void console.log(`Failure: ${error} ${task.name}`))
153145
.pause();
154146

155147
for (let i = 0; i < 10; i++) {

JavaScript/4-priority.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class Queue {
8282
}
8383
const hasChannel = this.count < this.concurrency;
8484
if (hasChannel) this.next(task);
85-
return;
8685
}
8786

8887
finish(err, res) {
@@ -156,9 +155,7 @@ const queue = Queue.channels(3)
156155
if (error) console.error(error);
157156
console.log(`Done: ${task.name}`);
158157
})
159-
.drain(() => {
160-
console.log('Queue drain');
161-
});
158+
.drain(() => void console.log('Queue drain'));
162159

163160
for (let i = 0; i < 10; i++) {
164161
queue.add({ name: `Task${i}`, interval: 1000 }, i);

JavaScript/5-pipe.js

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class Queue {
8383
}
8484
const hasChannel = this.count < this.concurrency;
8585
if (hasChannel) this.next(task);
86-
return;
8786
}
8887

8988
finish(err, res) {

JavaScript/6-promise.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class Queue {
4040
}
4141
}
4242

43-
finish(err, res) {
43+
finish(error, res) {
4444
const { onFailure, onSuccess, onDone, onDrain } = this;
45-
if (err && onFailure) onFailure(err, res);
45+
if (error && onFailure) onFailure(error, res);
4646
else if (onSuccess) onSuccess(res);
47-
if (onDone) onDone(err, res);
47+
if (onDone) onDone(error, res);
4848
if (this.count === 0 && this.waiting.length === 0 && onDrain) onDrain();
4949
}
5050

@@ -87,12 +87,11 @@ const job = ({ name, interval }) =>
8787

8888
const queue = Queue.channels(3)
8989
.process(job)
90-
.done((err, res) => {
90+
.done((error, res) => {
9191
const { count } = queue;
9292
const waiting = queue.waiting.length;
93-
console.log(
94-
`Done | res: ${res}, err: ${err}, count:${count}, waiting: ${waiting}`,
95-
);
93+
console.log(`Done | res: ${res}, err: ${error}`);
94+
console.log(` | count: ${count}, waiting: ${waiting}`);
9695
})
9796
// .success((res) => void console.log(`Success: ${res}`))
9897
// .failure((err) => void console.log(`Failure: ${err}`))

JavaScript/7-stream.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class QueueStream extends Readable {
2424
while (launchCount-- > 0) {
2525
this.count++;
2626
const task = this.waiting.shift();
27-
this.onProcess(task, (err, res) => {
28-
if (err) this.emit('error', err);
29-
this.push({ err, res });
27+
this.onProcess(task, (error, res) => {
28+
if (error) this.emit('error', error);
29+
this.push({ error, res });
3030
this.count--;
3131
});
3232
}
@@ -57,6 +57,4 @@ for (let i = 0; i < 20; i++) {
5757
queue.on('data', (data) => void console.log(data));
5858
queue.on('end', () => void console.log('Tasks end'));
5959
queue.on('close', () => void console.log('Stream closed'));
60-
queue.on('error', (err) => {
61-
throw err;
62-
});
60+
queue.on('error', (error) => void console.error(error));

JavaScript/8-pipeline.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class QueueStream extends Readable {
2424
while (launchCount-- > 0) {
2525
const task = this.waiting.shift();
2626
this.count++;
27-
this.onProcess(task, (err, res) => {
28-
if (err) this.emit('error', err);
29-
this.push({ err, res });
27+
this.onProcess(task, (error, res) => {
28+
if (error) this.emit('error', error);
29+
this.push({ error, res });
3030
this.count--;
3131
});
3232
}
@@ -71,8 +71,8 @@ const job = (task, next) => {
7171

7272
const queue = QueueStream.channels(3).process(job);
7373

74-
pipeline(queue, stringifyStream, fileWStream, (err) => {
75-
if (err) throw err;
74+
pipeline(queue, stringifyStream, fileWStream, (error) => {
75+
if (error) throw error;
7676
console.log('pipeline done');
7777
});
7878

0 commit comments

Comments
 (0)