Skip to content

Commit b68b7b5

Browse files
committed
Optimize (remove mixin) and update dubug output
1 parent 15b3d8b commit b68b7b5

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

JavaScript/5-async.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,43 @@ const duplicate = (factory, n) => (
66

77
const poolify = (factory, min, norm, max) => {
88
let allocated = norm;
9-
const pool = (par) => {
9+
const items = duplicate(factory, norm);
10+
const delayed = [];
11+
12+
return (par) => {
1013
if (typeof(par) !== 'function') {
11-
if (pool.items.length < max) {
12-
const delayed = pool.delayed.shift();
13-
if (delayed) {
14-
console.log('Recycle item, pass to delayed', pool.items.length);
15-
delayed(par);
14+
if (items.length < max) {
15+
const request = delayed.shift();
16+
if (request) {
17+
const c1 = items.length;
18+
console.log(`${c1}->${c1} Recycle item, pass to delayed`);
19+
request(par);
1620
} else {
17-
console.log('Recycle item, add to pool', pool.items.length);
18-
pool.items.push(par);
21+
const c1 = items.length;
22+
items.push(par);
23+
const c2 = items.length;
24+
console.log(`${c1}->${c2} Recycle item, add to pool`);
1925
}
2026
}
2127
return;
2228
}
23-
if (pool.items.length < min && allocated < max) {
24-
const grow = Math.min(max - allocated, norm - pool.items.length);
29+
if (items.length < min && allocated < max) {
30+
const grow = Math.min(max - allocated, norm - items.length);
2531
allocated += grow;
26-
const items = duplicate(factory, grow);
27-
pool.items.push(...items);
32+
const instances = duplicate(factory, grow);
33+
items.push(...instances);
2834
}
29-
const res = pool.items.pop();
35+
const c1 = items.length;
36+
const res = items.pop();
37+
const c2 = items.length;
3038
if (res) {
31-
console.log('Get from pool, pass to callback', pool.items.length);
39+
console.log(`${c1}->${c2} Get from pool, pass to callback`);
3240
par(res);
3341
} else {
34-
console.log('Get from pool, add callback to queue', pool.items.length);
35-
pool.delayed.push(par);
42+
console.log(`${c1}->${c2} Get from pool, add callback to queue`);
43+
delayed.push(par);
3644
}
3745
};
38-
return Object.assign(pool, {
39-
items: duplicate(factory, norm),
40-
delayed: []
41-
});
4246
};
4347

4448
// Usage

0 commit comments

Comments
 (0)