Skip to content

Commit eb41bad

Browse files
committed
Improve class implementations
1 parent 984e4d7 commit eb41bad

File tree

4 files changed

+94
-82
lines changed

4 files changed

+94
-82
lines changed

JavaScript/8-class.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22

33
class Pool {
44
constructor() {
5-
this.items = [];
5+
this.instances = [];
66
this.current = 0;
77
}
88

9-
next() {
10-
const item = this.items[this.current];
9+
acquire() {
10+
const instance = this.instances[this.current];
1111
this.current++;
12-
if (this.current === this.items.length) this.current = 0;
13-
return item;
12+
if (this.current === this.instances.length) this.current = 0;
13+
return instance;
1414
}
1515

16-
add(item) {
17-
if (this.items.includes(item)) throw new Error('Pool: add duplicates');
18-
this.items.push(item);
16+
release(instance) {
17+
if (this.instances.includes(instance)) {
18+
throw new Error('Pool: add duplicates');
19+
}
20+
this.instances.push(instance);
1921
}
2022
}
2123

2224
// Usage
2325

2426
const pool = new Pool();
25-
pool.add({ item: 1 });
26-
pool.add({ item: 2 });
27-
pool.add({ item: 3 });
27+
pool.release({ instance: 1 });
28+
pool.release({ instance: 2 });
29+
pool.release({ instance: 3 });
2830

2931
for (let i = 0; i < 10; i++) {
30-
console.log(pool.next());
32+
console.log(pool.acquire());
3133
}

JavaScript/9-exclusive.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,48 @@
22

33
class Pool {
44
constructor() {
5-
this.items = [];
5+
this.instances = [];
66
this.free = [];
77
this.current = 0;
88
this.size = 0;
99
this.available = 0;
1010
}
1111

12-
next() {
12+
acquire() {
1313
if (this.available === 0) return null;
14-
let item = null;
14+
let instance = null;
1515
let free = false;
1616
do {
17-
item = this.items[this.current];
17+
instance = this.instances[this.current];
1818
free = this.free[this.current];
1919
this.current++;
2020
if (this.current === this.size) this.current = 0;
21-
} while (!item || !free);
22-
return item;
21+
} while (!instance || !free);
22+
return instance;
2323
}
2424

25-
add(item) {
26-
if (this.items.includes(item)) throw new Error('Pool: add duplicates');
25+
add(instance) {
26+
if (this.instances.includes(instance)) {
27+
throw new Error('Pool: add duplicates');
28+
}
2729
this.size++;
2830
this.available++;
29-
this.items.push(item);
31+
this.instances.push(instance);
3032
this.free.push(true);
3133
}
3234

3335
capture() {
34-
const item = this.next();
35-
if (!item) return null;
36-
const index = this.items.indexOf(item);
36+
const instance = this.acquire();
37+
if (!instance) return null;
38+
const index = this.instances.indexOf(instance);
3739
this.free[index] = false;
3840
this.available--;
39-
return item;
41+
return instance;
4042
}
4143

42-
release(item) {
43-
const index = this.items.indexOf(item);
44-
if (index < 0) throw new Error('Pool: release unexpected item');
44+
release(instance) {
45+
const index = this.instances.indexOf(instance);
46+
if (index < 0) throw new Error('Pool: release unexpected instance');
4547
if (this.free[index]) throw new Error('Pool: release not captured');
4648
this.free[index] = true;
4749
this.available++;
@@ -51,9 +53,9 @@ class Pool {
5153
// Usage
5254

5355
const pool = new Pool();
54-
pool.add({ item: 1 });
55-
pool.add({ item: 2 });
56-
const last = { item: 3 };
56+
pool.add({ instance: 1 });
57+
pool.add({ instance: 2 });
58+
const last = { instance: 3 };
5759
pool.add(last);
5860

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

JavaScript/a-await.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,74 @@
22

33
class Pool {
44
constructor() {
5-
this.items = [];
5+
this.instances = [];
66
this.free = [];
77
this.queue = [];
88
this.current = 0;
99
this.size = 0;
1010
this.available = 0;
1111
}
1212

13-
async next() {
13+
async acquire() {
1414
if (this.size === 0) return null;
1515
if (this.available === 0) {
1616
return new Promise((resolve) => {
1717
this.queue.push(resolve);
1818
});
1919
}
20-
let item = null;
20+
let instance = null;
2121
let free = false;
2222
do {
23-
item = this.items[this.current];
23+
instance = this.instances[this.current];
2424
free = this.free[this.current];
2525
this.current++;
2626
if (this.current === this.size) this.current = 0;
27-
} while (!item || !free);
28-
return item;
27+
} while (!instance || !free);
28+
return instance;
2929
}
3030

31-
add(item) {
32-
if (this.items.includes(item)) throw new Error('Pool: add duplicates');
31+
add(instance) {
32+
if (this.instances.includes(instance)) {
33+
throw new Error('Pool: add duplicates');
34+
}
3335
this.size++;
3436
this.available++;
35-
this.items.push(item);
37+
this.instances.push(instance);
3638
this.free.push(true);
3739
}
3840

3941
async capture() {
40-
const item = await this.next();
41-
if (!item) return null;
42-
const index = this.items.indexOf(item);
42+
const instance = await this.acquire();
43+
if (!instance) return null;
44+
const index = this.instances.indexOf(instance);
4345
this.free[index] = false;
4446
this.available--;
45-
return item;
47+
return instance;
4648
}
4749

48-
release(item) {
49-
const index = this.items.indexOf(item);
50-
if (index < 0) throw new Error('Pool: release unexpected item');
50+
release(instance) {
51+
const index = this.instances.indexOf(instance);
52+
if (index < 0) throw new Error('Pool: release unexpected instance');
5153
if (this.free[index]) throw new Error('Pool: release not captured');
5254
this.free[index] = true;
5355
this.available++;
5456
if (this.queue.length > 0) {
5557
const resolve = this.queue.shift();
56-
if (resolve) setTimeout(resolve, 0, item);
58+
if (resolve) setTimeout(resolve, 0, instance);
5759
}
5860
}
5961
}
6062

6163
// Usage
6264

63-
(async () => {
65+
const main = async () => {
6466
const pool = new Pool();
65-
const item1 = { item: 1 };
66-
pool.add(item1);
67-
const item2 = { item: 2 };
68-
pool.add(item2);
69-
const item3 = { item: 3 };
70-
pool.add(item3);
67+
const instance1 = { instance: 1 };
68+
pool.add(instance1);
69+
const instance2 = { instance: 2 };
70+
pool.add(instance2);
71+
const instance3 = { instance: 3 };
72+
pool.add(instance3);
7173

7274
const x1 = await pool.capture();
7375
console.log({ x1 });
@@ -86,7 +88,9 @@ class Pool {
8688
console.log({ x6 });
8789
});
8890

89-
pool.release(item2);
90-
pool.release(item1);
91-
pool.release(item3);
92-
})();
91+
pool.release(instance2);
92+
pool.release(instance1);
93+
pool.release(instance3);
94+
};
95+
96+
main();

JavaScript/b-timeout.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Pool {
44
constructor(options = {}) {
5-
this.items = [];
5+
this.instances = [];
66
this.free = [];
77
this.queue = [];
88
this.timeout = options.timeout || 0;
@@ -11,68 +11,70 @@ class Pool {
1111
this.available = 0;
1212
}
1313

14-
async next() {
14+
async acquire() {
1515
if (this.size === 0) return null;
1616
if (this.available === 0) {
1717
return new Promise((resolve, reject) => {
1818
const waiting = { resolve, timer: null };
1919
waiting.timer = setTimeout(() => {
2020
waiting.resolve = null;
2121
this.queue.shift();
22-
reject(new Error('Pool next item timeout'));
22+
reject(new Error('Pool next instance timeout'));
2323
}, this.timeout);
2424
this.queue.push(waiting);
2525
});
2626
}
27-
let item = null;
27+
let instance = null;
2828
let free = false;
2929
do {
30-
item = this.items[this.current];
30+
instance = this.instances[this.current];
3131
free = this.free[this.current];
3232
this.current++;
3333
if (this.current === this.size) this.current = 0;
34-
} while (!item || !free);
35-
return item;
34+
} while (!instance || !free);
35+
return instance;
3636
}
3737

38-
add(item) {
39-
if (this.items.includes(item)) throw new Error('Pool: add duplicates');
38+
add(instance) {
39+
if (this.instances.includes(instance)) {
40+
throw new Error('Pool: add duplicates');
41+
}
4042
this.size++;
4143
this.available++;
42-
this.items.push(item);
44+
this.instances.push(instance);
4345
this.free.push(true);
4446
}
4547

4648
async capture() {
47-
const item = await this.next();
48-
if (!item) return null;
49-
const index = this.items.indexOf(item);
49+
const instance = await this.acquire();
50+
if (!instance) return null;
51+
const index = this.instances.indexOf(instance);
5052
this.free[index] = false;
5153
this.available--;
52-
return item;
54+
return instance;
5355
}
5456

55-
release(item) {
56-
const index = this.items.indexOf(item);
57-
if (index < 0) throw new Error('Pool: release unexpected item');
57+
release(instance) {
58+
const index = this.instances.indexOf(instance);
59+
if (index < 0) throw new Error('Pool: release unexpected instance');
5860
if (this.free[index]) throw new Error('Pool: release not captured');
5961
this.free[index] = true;
6062
this.available++;
6163
if (this.queue.length > 0) {
6264
const { resolve, timer } = this.queue.shift();
6365
clearTimeout(timer);
64-
if (resolve) setTimeout(resolve, 0, item);
66+
if (resolve) setTimeout(resolve, 0, instance);
6567
}
6668
}
6769
}
6870

6971
// Usage
7072

71-
(async () => {
73+
const main = async () => {
7274
const pool = new Pool({ timeout: 5000 });
73-
pool.add({ item: 1 });
74-
pool.add({ item: 2 });
75-
const last = { item: 3 };
75+
pool.add({ instance: 1 });
76+
pool.add({ instance: 2 });
77+
const last = { instance: 3 };
7678
pool.add(last);
7779

7880
const x1 = await pool.capture();
@@ -102,4 +104,6 @@ class Pool {
102104
setTimeout(() => {
103105
pool.release(x3);
104106
}, 6000);
105-
})();
107+
};
108+
109+
main();

0 commit comments

Comments
 (0)