@@ -6,39 +6,43 @@ const duplicate = (factory, n) => (
6
6
7
7
const poolify = ( factory , min , norm , max ) => {
8
8
let allocated = norm ;
9
- const pool = ( par ) => {
9
+ const items = duplicate ( factory , norm ) ;
10
+ const delayed = [ ] ;
11
+
12
+ return ( par ) => {
10
13
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 ) ;
16
20
} 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` ) ;
19
25
}
20
26
}
21
27
return ;
22
28
}
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 ) ;
25
31
allocated += grow ;
26
- const items = duplicate ( factory , grow ) ;
27
- pool . items . push ( ...items ) ;
32
+ const instances = duplicate ( factory , grow ) ;
33
+ items . push ( ...instances ) ;
28
34
}
29
- const res = pool . items . pop ( ) ;
35
+ const c1 = items . length ;
36
+ const res = items . pop ( ) ;
37
+ const c2 = items . length ;
30
38
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` ) ;
32
40
par ( res ) ;
33
41
} 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 ) ;
36
44
}
37
45
} ;
38
- return Object . assign ( pool , {
39
- items : duplicate ( factory , norm ) ,
40
- delayed : [ ]
41
- } ) ;
42
46
} ;
43
47
44
48
// Usage
0 commit comments