-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pool.js
75 lines (64 loc) · 2.09 KB
/
Pool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
export default class Pool
{
constructor(Name,AllocItem,OnWarning,PopFromFreeList)
{
if ( typeof Name != 'string' )
throw `New constructor for pool, first argument should be name, not ${Name}`;
this.Name = Name;
if ( !OnWarning )
OnWarning = function(){};
if ( !PopFromFreeList )
{
PopFromFreeList = function(){ throw `implement default find best free`; };
}
this.AllocItem = AllocItem;
this.UsedItems = [];
this.FreeItems = [];
this.OnWarning = OnWarning;
this.OnDebug = function(){};//OnWarning;
this.PopFromFreeList = PopFromFreeList;
}
Alloc()
{
// see if there are any best-match free items
// if there isn't one, allocate.
// this lets us filter & add new pool items based on arguments
// gr: I was getting what seems like a race condition
// the match happend, return index 1
// another matched happened, returned index 0, spliced
// then index1 was spliced (splicing 2)
// was I running something from another thread/module's event loop?
let Popped = this.PopFromFreeList(this.FreeItems,...arguments);
if ( Popped === undefined )
throw `A) Pool ${this.Name} FindBestFree() should not return undefined. return false if no match`;
if ( Popped )
{
this.UsedItems.push(Popped);
return Popped;
}
const NewItem = this.AllocItem(...arguments);
this.UsedItems.push(NewItem);
this.DebugPoolSize();
return NewItem;
}
Release(Item)
{
// remove from used queue
const UsedIndex = this.UsedItems.indexOf(Item);
if ( UsedIndex < 0 )
{
const Name = Item.Name ? `(.Name=${Item.Name})` : '';
this.OnWarning(`A) Pool ${this.Name} Releasing item ${Item}${Name} back into pool, but missing from Used Items list`);
this.DebugPoolSize();
return;
}
const Name = Item.Name ? `(.Name=${Item.Name})` : '';
//this.OnDebug(`Pool ${this.Name} Released item ${Item}${Name} back into pool.`);
this.UsedItems = this.UsedItems.filter( i => i != Item );
this.FreeItems.push( Item );
}
DebugPoolSize()
{
this.OnDebug(`A) Pool ${this.Name} size now x${this.UsedItems.length} Used, x${this.FreeItems.length} free`);
}
}