Skip to content

Commit dcf14b1

Browse files
committed
Improve examples
1 parent d923eea commit dcf14b1

File tree

8 files changed

+118
-50
lines changed

8 files changed

+118
-50
lines changed

JavaScript/0-factory.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const pool = (factory) => {
4+
const instances = new Array(5).fill(null).map(() => factory.create());
5+
return (instance) => {
6+
if (instance) {
7+
instances.push(instance);
8+
console.log('Recycle item, count =', instances.length);
9+
return null;
10+
}
11+
const result = instances.pop();
12+
console.log('Get from pool, count =', instances.length);
13+
return result;
14+
};
15+
};
16+
17+
class Connection {
18+
constructor(index) {
19+
this.url = `http://10.0.0.1/${index}`;
20+
}
21+
}
22+
23+
class Factory {
24+
constructor() {
25+
this.index = 0;
26+
}
27+
28+
create() {
29+
return new Connection(this.index++);
30+
}
31+
}
32+
33+
/*
34+
35+
const alternativeFactory = (() => {
36+
let index = 0;
37+
return () => new Connection(`http://10.0.0.1/${index++}`);
38+
})();
39+
40+
*/
41+
42+
// Usage
43+
44+
const connectionFactory = new Factory();
45+
const connectionPool = pool(connectionFactory);
46+
const connection = connectionPool();
47+
console.log(connection);

JavaScript/1-simple.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
'use strict';
22

3-
const pool = (item) => {
4-
pool.items = pool.items || new Array(10).fill(new Array(1000).fill(0));
3+
class Connection {
4+
static index = 0;
55

6-
if (item) {
7-
pool.items.push(item);
8-
console.log('Recycle item, count =', pool.items.length);
9-
return;
6+
constructor(url) {
7+
this.url = url;
108
}
119

12-
const res = pool.items.pop();
13-
console.log('Get from pool, count =', pool.items.length);
14-
return res;
10+
static create() {
11+
return new Connection(`http://10.0.0.1/${Connection.index++}`);
12+
}
13+
}
14+
15+
const pool = () => {
16+
const instances = new Array(5).fill(null).map(Connection.create);
17+
return (instance) => {
18+
if (instance) {
19+
instances.push(instance);
20+
console.log('Recycle item, count =', instances.length);
21+
return null;
22+
}
23+
const result = instances.pop();
24+
console.log('Get from pool, count =', instances.length);
25+
return result;
26+
};
1527
};
1628

1729
// Usage
1830

19-
const a1 = pool();
20-
const b1 = a1.map((x, i) => i).reduce((x, y) => x + y);
21-
console.log(b1);
22-
23-
const a2 = pool();
24-
const b2 = a2.map((x, i) => i).reduce((x, y) => x + y);
25-
console.log(b2);
26-
27-
pool(a1);
28-
pool(a2);
29-
30-
const a3 = pool();
31-
const b3 = a3.map((x, i) => i).reduce((x, y) => x + y);
32-
console.log(b3);
31+
const connectionPool = pool(Connection.create);
3332

34-
pool(a3);
33+
for (let i = 0; i < 7; i++) {
34+
const connection = connectionPool();
35+
console.log(connection);
36+
}

JavaScript/2-drain.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
'use strict';
22

3-
const pool = (item) => {
4-
pool.items = pool.items || new Array(10).fill(new Array(1000).fill(0));
3+
const pool = (factory) => {
4+
const instances = new Array(5).fill(null).map(factory);
5+
return (instance) => {
6+
if (instance) {
7+
instances.push(instance);
8+
console.log('Recycle item, count =', instances.length);
9+
return null;
10+
}
11+
const result = instances.pop() || factory();
12+
console.log('Get from pool, count =', instances.length);
13+
return result;
14+
};
15+
};
16+
17+
// Usage
18+
19+
class Connection {
20+
static index = 0;
521

6-
if (item) {
7-
pool.items.push(item);
8-
console.log('Recycle item, count =', pool.items.length);
9-
return;
22+
constructor(url) {
23+
this.url = url;
1024
}
11-
const res = pool.items.pop() || new Array(1000).fill(0);
1225

13-
console.log('Get from pool, count =', pool.items.length);
14-
return res;
15-
};
26+
static create() {
27+
return new Connection(`http://10.0.0.1/${Connection.index++}`);
28+
}
29+
}
1630

17-
// Usage
31+
const connectionPool = pool(Connection.create);
1832

19-
for (let i = 0; i < 15; i++) {
20-
const a1 = pool();
21-
const b1 = a1.map((x, i) => i).reduce((x, y) => x + y);
22-
console.log(b1);
33+
for (let i = 0; i < 10; i++) {
34+
const connection = connectionPool();
35+
console.log(connection);
2336
}

JavaScript/3-poolify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const poolify = (factory, size) => {
77
if (item) {
88
items.push(item);
99
console.log('Recycle item, count =', items.length);
10-
return;
10+
return null;
1111
}
1212
const res = items.pop() || factory();
1313

@@ -18,7 +18,7 @@ const poolify = (factory, size) => {
1818

1919
// Usage
2020

21-
// Factory to allocate 4kb buffer
21+
// Factory to allocate 32kb buffer
2222
const buffer = () => new Uint32Array(1024);
2323

2424
// Allocate pool of 10 buffers

JavaScript/4-improved.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const poolify = (factory, min, norm, max) => {
1010
items.push(item);
1111
}
1212
console.log('Recycle item, count =', items.length);
13-
return;
13+
return null;
1414
}
1515
if (items.length < min) {
1616
const instances = duplicate(norm - items.length);

JavaScript/5-async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const next = () => {
5858
i++;
5959
if (i < 20) {
6060
setTimeout(next, i * 10);
61-
setTimeout(pool, i * 100, item);
61+
setTimeout(() => pool(item), i * 100);
6262
}
6363
});
6464
};

JavaScript/7-buffer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const poolify = (factory, min, norm, max) => {
1616
length: pool.items.length,
1717
allocated: pool.allocated,
1818
});
19-
return;
19+
return null;
2020
}
2121
if (pool.items.length < min) {
2222
const items = duplicate(norm - pool.items.length);
@@ -37,7 +37,10 @@ const poolify = (factory, min, norm, max) => {
3737
return Object.assign(pool, { items, allocated: norm });
3838
};
3939

40-
const factorify = (Category, ...args) => () => new Category(...args);
40+
const factorify =
41+
(Category, ...args) =>
42+
() =>
43+
new Category(...args);
4144

4245
// Usage
4346

JavaScript/b-timeout.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,14 @@ class Pool {
8888
pool.capture().then((x5) => {
8989
console.log({ x5 });
9090
});
91-
pool.capture().then((x6) => {
92-
console.log({ x6 });
93-
}).catch((err) => {
94-
console.error({ err });
95-
});
91+
pool
92+
.capture()
93+
.then((x6) => {
94+
console.log({ x6 });
95+
})
96+
.catch((err) => {
97+
console.error({ err });
98+
});
9699

97100
pool.release(x2);
98101
pool.release(x1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy