Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPool.js
More file actions
Latest commit
75 lines (64 loc) · 2.09 KB
/
Copy pathPool.js
File metadata and controls
75 lines (64 loc) · 2.09 KB
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
exportdefaultclassPool
{
constructor(Name,AllocItem,OnWarning,PopFromFreeList)
{
if(typeofName!='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?
letPopped=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);
returnPopped;
}
constNewItem=this.AllocItem(...arguments);
this.UsedItems.push(NewItem);
this.DebugPoolSize();
returnNewItem;
}
Release(Item)
{
// remove from used queue
constUsedIndex=this.UsedItems.indexOf(Item);
if(UsedIndex<0)
{
constName=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;
}
constName=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`);
}
}