Skip to content

Commit 8bf4ed3

Browse files
committed
Add spacing between methods
1 parent 360a304 commit 8bf4ed3

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

JavaScript/1-channels.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ class Queue {
1111
this.onFailure = null;
1212
this.onDrain = null;
1313
}
14+
1415
static channels(concurrency) {
1516
return new Queue(concurrency);
1617
}
18+
1719
add(task) {
1820
const hasChannel = this.count < this.concurrency;
1921
if (hasChannel) {
@@ -22,6 +24,7 @@ class Queue {
2224
}
2325
this.waiting.push(task);
2426
}
27+
2528
next(task) {
2629
this.count++;
2730
this.onProcess(task, (err, result) => {
@@ -42,22 +45,27 @@ class Queue {
4245
}
4346
});
4447
}
48+
4549
process(listener) {
4650
this.onProcess = listener;
4751
return this;
4852
}
53+
4954
done(listener) {
5055
this.onDone = listener;
5156
return this;
5257
}
58+
5359
success(listener) {
5460
this.onSuccess = listener;
5561
return this;
5662
}
63+
5764
failure(listener) {
5865
this.onFailure = listener;
5966
return this;
6067
}
68+
6169
drain(listener) {
6270
this.onDrain = listener;
6371
return this;

JavaScript/2-timeouts.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ class Queue {
1313
this.waitTimeout = Infinity;
1414
this.processTimeout = Infinity;
1515
}
16+
1617
static channels(concurrency) {
1718
return new Queue(concurrency);
1819
}
20+
1921
wait(msec) {
2022
this.waitTimeout = msec;
2123
return this;
2224
}
25+
2326
timeout(msec) {
2427
this.processTimeout = msec;
2528
return this;
2629
}
30+
2731
add(task) {
2832
const hasChannel = this.count < this.concurrency;
2933
if (hasChannel) {
@@ -32,6 +36,7 @@ class Queue {
3236
}
3337
this.waiting.push({ task, start: Date.now() });
3438
}
39+
3540
next(task) {
3641
this.count++;
3742
let timer = null;
@@ -54,6 +59,7 @@ class Queue {
5459
}
5560
onProcess(task, finish);
5661
}
62+
5763
takeNext() {
5864
const { waiting, waitTimeout } = this;
5965
const { task, start } = waiting.shift();
@@ -69,6 +75,7 @@ class Queue {
6975
this.next(task);
7076
return;
7177
}
78+
7279
finish(err, res) {
7380
const { onFailure, onSuccess, onDone, onDrain } = this;
7481
if (err) {
@@ -79,22 +86,27 @@ class Queue {
7986
if (onDone) onDone(err, res);
8087
if (this.count === 0 && onDrain) onDrain();
8188
}
89+
8290
process(listener) {
8391
this.onProcess = listener;
8492
return this;
8593
}
94+
8695
done(listener) {
8796
this.onDone = listener;
8897
return this;
8998
}
99+
90100
success(listener) {
91101
this.onSuccess = listener;
92102
return this;
93103
}
104+
94105
failure(listener) {
95106
this.onFailure = listener;
96107
return this;
97108
}
109+
98110
drain(listener) {
99111
this.onDrain = listener;
100112
return this;

JavaScript/3-pause.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ class Queue {
1414
this.waitTimeout = Infinity;
1515
this.processTimeout = Infinity;
1616
}
17+
1718
static channels(concurrency) {
1819
return new Queue(concurrency);
1920
}
21+
2022
wait(msec) {
2123
this.waitTimeout = msec;
2224
return this;
2325
}
26+
2427
timeout(msec) {
2528
this.processTimeout = msec;
2629
return this;
2730
}
31+
2832
add(task) {
2933
if (!this.paused) {
3034
const hasChannel = this.count < this.concurrency;
@@ -35,6 +39,7 @@ class Queue {
3539
}
3640
this.waiting.push({ task, start: Date.now() });
3741
}
42+
3843
next(task) {
3944
this.count++;
4045
let timer = null;
@@ -54,6 +59,7 @@ class Queue {
5459
}
5560
onProcess(task, finish);
5661
}
62+
5763
takeNext() {
5864
const { waiting, waitTimeout } = this;
5965
const { task, start } = waiting.shift();
@@ -74,6 +80,7 @@ class Queue {
7480
if (hasChannel) this.next(task);
7581
return;
7682
}
83+
7784
finish(err, res) {
7885
const { onFailure, onSuccess, onDone, onDrain } = this;
7986
if (err) {
@@ -84,30 +91,37 @@ class Queue {
8491
if (onDone) onDone(err, res);
8592
if (this.count === 0 && onDrain) onDrain();
8693
}
94+
8795
process(listener) {
8896
this.onProcess = listener;
8997
return this;
9098
}
99+
91100
done(listener) {
92101
this.onDone = listener;
93102
return this;
94103
}
104+
95105
success(listener) {
96106
this.onSuccess = listener;
97107
return this;
98108
}
109+
99110
failure(listener) {
100111
this.onFailure = listener;
101112
return this;
102113
}
114+
103115
drain(listener) {
104116
this.onDrain = listener;
105117
return this;
106118
}
119+
107120
pause() {
108121
this.paused = true;
109122
return this;
110123
}
124+
111125
resume() {
112126
if (this.waiting.length > 0) {
113127
const channels = this.concurrency - this.count;

JavaScript/4-priority.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ class Queue {
1515
this.processTimeout = Infinity;
1616
this.priorityMode = false;
1717
}
18+
1819
static channels(concurrency) {
1920
return new Queue(concurrency);
2021
}
22+
2123
wait(msec) {
2224
this.waitTimeout = msec;
2325
return this;
2426
}
27+
2528
timeout(msec) {
2629
this.processTimeout = msec;
2730
return this;
2831
}
32+
2933
add(task, priority = 0) {
3034
if (!this.paused) {
3135
const hasChannel = this.count < this.concurrency;
@@ -39,6 +43,7 @@ class Queue {
3943
this.waiting.sort((a, b) => b.priority - a.priority);
4044
}
4145
}
46+
4247
next(task) {
4348
this.count++;
4449
let timer = null;
@@ -58,6 +63,7 @@ class Queue {
5863
}
5964
onProcess(task, finish);
6065
}
66+
6167
takeNext() {
6268
const { waiting, waitTimeout } = this;
6369
const { task, start } = waiting.shift();
@@ -78,6 +84,7 @@ class Queue {
7884
if (hasChannel) this.next(task);
7985
return;
8086
}
87+
8188
finish(err, res) {
8289
const { onFailure, onSuccess, onDone, onDrain } = this;
8390
if (err) {
@@ -88,30 +95,37 @@ class Queue {
8895
if (onDone) onDone(err, res);
8996
if (this.count === 0 && onDrain) onDrain();
9097
}
98+
9199
process(listener) {
92100
this.onProcess = listener;
93101
return this;
94102
}
103+
95104
done(listener) {
96105
this.onDone = listener;
97106
return this;
98107
}
108+
99109
success(listener) {
100110
this.onSuccess = listener;
101111
return this;
102112
}
113+
103114
failure(listener) {
104115
this.onFailure = listener;
105116
return this;
106117
}
118+
107119
drain(listener) {
108120
this.onDrain = listener;
109121
return this;
110122
}
123+
111124
pause() {
112125
this.paused = true;
113126
return this;
114127
}
128+
115129
resume() {
116130
if (this.waiting.length > 0) {
117131
const channels = this.concurrency - this.count;
@@ -122,6 +136,7 @@ class Queue {
122136
this.paused = false;
123137
return this;
124138
}
139+
125140
priority(flag = true) {
126141
this.priorityMode = flag;
127142
return this;

JavaScript/5-pipe.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ class Queue {
1616
this.priorityMode = false;
1717
this.destination = null;
1818
}
19+
1920
static channels(concurrency) {
2021
return new Queue(concurrency);
2122
}
23+
2224
wait(msec) {
2325
this.waitTimeout = msec;
2426
return this;
2527
}
28+
2629
timeout(msec) {
2730
this.processTimeout = msec;
2831
return this;
2932
}
33+
3034
add(task, priority = 0) {
3135
if (!this.paused) {
3236
const hasChannel = this.count < this.concurrency;
@@ -40,6 +44,7 @@ class Queue {
4044
this.waiting.sort((a, b) => b.priority - a.priority);
4145
}
4246
}
47+
4348
next(task) {
4449
this.count++;
4550
let timer = null;
@@ -59,6 +64,7 @@ class Queue {
5964
}
6065
onProcess(task, finish);
6166
}
67+
6268
takeNext() {
6369
const { waiting, waitTimeout } = this;
6470
const { task, start } = waiting.shift();
@@ -79,6 +85,7 @@ class Queue {
7985
if (hasChannel) this.next(task);
8086
return;
8187
}
88+
8289
finish(err, res) {
8390
const { onFailure, onSuccess, onDone, onDrain } = this;
8491
if (err) {
@@ -90,30 +97,37 @@ class Queue {
9097
if (onDone) onDone(err, res);
9198
if (this.count === 0 && onDrain) onDrain();
9299
}
100+
93101
process(listener) {
94102
this.onProcess = listener;
95103
return this;
96104
}
105+
97106
done(listener) {
98107
this.onDone = listener;
99108
return this;
100109
}
110+
101111
success(listener) {
102112
this.onSuccess = listener;
103113
return this;
104114
}
115+
105116
failure(listener) {
106117
this.onFailure = listener;
107118
return this;
108119
}
120+
109121
drain(listener) {
110122
this.onDrain = listener;
111123
return this;
112124
}
125+
113126
pause() {
114127
this.paused = true;
115128
return this;
116129
}
130+
117131
resume() {
118132
if (this.waiting.length > 0) {
119133
const channels = this.concurrency - this.count;
@@ -124,10 +138,12 @@ class Queue {
124138
this.paused = false;
125139
return this;
126140
}
141+
127142
priority(flag = true) {
128143
this.priorityMode = flag;
129144
return this;
130145
}
146+
131147
pipe(destination) {
132148
this.destination = destination;
133149
return this;

0 commit comments

Comments
 (0)