1 | var logger=require('AppFramework.js').logger; |
||
2 | class ObjectPool |
||
0 ignored issues
–
show
Backwards Compatibility
introduced
by
![]() |
|||
3 | { |
||
4 | // Options |
||
5 | // {number} options.initialSize The number of objects to pre-allocate |
||
6 | // {number} options.maxSize Prevents creation of more objects than the number specified here |
||
7 | // {function} options.resetObject A function that will turn a used, dirty object into a clean state |
||
8 | |||
9 | constructor( options, createObjectFunc ) |
||
10 | { |
||
11 | if( !createObjectFunc ) |
||
12 | { |
||
13 | throw new Error( 'ObjectPool: No `createObject` function provided!' ); |
||
14 | } |
||
15 | |||
16 | // Private |
||
17 | this._pool = []; |
||
18 | this._createObject = createObjectFunc; |
||
19 | this._resetObject = options.resetObject; |
||
20 | this._maxSize = options.maxSize || 0; |
||
21 | this._totalCount = 0; |
||
22 | |||
23 | // Pre-allocate objects |
||
24 | this.allocate( options.initialSize || 0 ); |
||
25 | |||
26 | logger.debug( `Max size: ${this._maxSize}` ); |
||
0 ignored issues
–
show
'template literal syntax' is only available in ES6 (use 'esversion: 6').
Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code. Further Reading: ![]() |
|||
27 | }; |
||
0 ignored issues
–
show
|
|||
28 | |||
29 | // Pre-allocates the pool with the specified number of objects |
||
30 | allocate( size ) |
||
31 | { |
||
32 | while( size-- ) |
||
33 | { |
||
34 | if( this._maxSize === 0 || this._totalCount < this._maxSize ) |
||
35 | { |
||
36 | this._totalCount++; |
||
37 | |||
38 | this._pool.push( this._createObject() ); |
||
39 | } |
||
40 | else |
||
41 | { |
||
42 | // No room left in pool |
||
43 | throw new Error( "No room to allocate more objects in pool!" ); |
||
44 | } |
||
45 | } |
||
46 | } |
||
47 | |||
48 | // Removes an object from the pool and hands it over to the caller. If the pool is empty, will create a new object. |
||
49 | request() |
||
50 | { |
||
51 | if( this._pool.length > 0 ) |
||
52 | { |
||
53 | // Provide an existing resource |
||
54 | return this._pool.pop(); |
||
55 | } |
||
56 | else if( this._maxSize === 0 || this._totalCount < this._maxSize ) |
||
57 | { |
||
58 | this._totalCount++; |
||
59 | |||
60 | // Create new object |
||
61 | return this._createObject(); |
||
62 | } |
||
63 | else |
||
64 | { |
||
65 | // No resources available |
||
66 | return null; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | // Takes back an object and puts it back into the pool |
||
71 | recycle( object ) |
||
72 | { |
||
73 | // If a resetObject function exist, reset the object to a default state |
||
74 | if( this._resetObject ) |
||
75 | { |
||
76 | this._resetObject( object ); |
||
77 | } |
||
78 | |||
79 | // Place the object in the pool |
||
80 | this._pool.push( object ); |
||
81 | } |
||
82 | |||
83 | // Makes sure that the pool has at least the given size |
||
84 | reserve( size ) |
||
85 | { |
||
86 | var diff = size - this._totalCount; |
||
87 | |||
88 | if( diff > 0 ) |
||
89 | { |
||
90 | // Failure to allocate requested number of objects will throw |
||
91 | this.allocate( diff ); |
||
92 | } |
||
93 | else |
||
94 | { |
||
95 | // Do nothing. We won't delete existing objects |
||
96 | } |
||
97 | } |
||
98 | }; |
||
0 ignored issues
–
show
|
|||
99 | |||
100 | module.exports = ObjectPool; |