-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex4.js
More file actions
46 lines (36 loc) · 968 Bytes
/
ex4.js
File metadata and controls
46 lines (36 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
/*
Problem:
Implement `createBackpressureQueue({ highWaterMark })`.
Return API:
- push(item): boolean
- pull(): next item or null
- size(): number
Rules:
- `push` returns true when queue is below highWaterMark after insert.
- `push` returns false when queue is at/above highWaterMark after insert.
- `pull` returns the next item in FIFO order or null if empty.
Starter code is intentionally incorrect:
- Always returns true.
- Provides no backpressure signal.
*/
function createBackpressureQueue({ highWaterMark }) {
if (!Number.isInteger(highWaterMark) || highWaterMark < 1) {
throw new TypeError('highWaterMark must be an integer >= 1');
}
const buffer = [];
return {
push(item) {
buffer.push(item);
return true;
},
pull() {
if (buffer.length === 0) return null;
return buffer.shift();
},
size() {
return buffer.length;
},
};
}
module.exports = { createBackpressureQueue };