File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const poolify = ( factory , min , norm , max ) => {
4
+ const duplicate = n => new Array ( n ) . fill ( ) . map ( ( ) => factory ( ) ) ;
5
+
6
+ const pool = ( item ) => {
7
+ if ( item ) {
8
+ if ( pool . allocated <= max ) {
9
+ pool . items . push ( item ) ;
10
+ } else {
11
+ pool . allocated -- ;
12
+ }
13
+ console . log ( 'Recycle item, count =' , pool . items . length , pool . allocated ) ;
14
+ return ;
15
+ }
16
+ if ( pool . items . length < min ) {
17
+ const items = duplicate ( norm - pool . items . length ) ;
18
+ pool . allocated += pool . items . length ;
19
+ pool . items . push ( ...items ) ;
20
+ }
21
+ const res = pool . items . pop ( ) ;
22
+
23
+ console . log ( 'Get item, count =' , pool . items . length , pool . allocated ) ;
24
+ return res ;
25
+ } ;
26
+
27
+ const items = duplicate ( norm ) ;
28
+ return Object . assign ( pool , { items, allocated : norm } ) ;
29
+ } ;
30
+
31
+ const buffer = ( ) => new Uint32Array ( 1024 ) ;
32
+
33
+ const pool = poolify ( buffer , 5 , 10 , 13 ) ;
34
+
35
+ let i = 0 ;
36
+
37
+ const next = ( ) => {
38
+ const item = pool ( ) ;
39
+ console . log ( 'Buffer size' , item . length * 32 ) ;
40
+ i ++ ;
41
+ if ( i < 20 ) {
42
+ setTimeout ( next , i * 10 ) ;
43
+ setTimeout ( ( ) => pool ( item ) , i * 100 ) ;
44
+ }
45
+ } ;
46
+
47
+ next ( ) ;
You can’t perform that action at this time.
0 commit comments