forked from StrandedKitty/streets-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysicalResourcePool.ts
60 lines (45 loc) · 1.37 KB
/
PhysicalResourcePool.ts
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
import PhysicalResource from "./PhysicalResource";
import Resource from "./Resource";
class PhysicalResourceEntry {
public frameCount = 0;
public constructor(public resource: PhysicalResource) {
}
}
export default class PhysicalResourcePool {
private resourcesMap: Map<string, PhysicalResourceEntry[]> = new Map();
public constructor(private unusedResourceLifeTime: number) {
}
public pushPhysicalResource(id: string, physicalResource: PhysicalResource): void {
if (!this.resourcesMap.get(id)) {
this.resourcesMap.set(id, []);
}
this.resourcesMap.get(id).push(new PhysicalResourceEntry(physicalResource));
}
public getPhysicalResource(id: string): PhysicalResource {
if (!this.resourcesMap.get(id)) {
return null;
}
const entries = this.resourcesMap.get(id);
const result = entries.pop().resource;
if (entries.length === 0) {
this.resourcesMap.delete(id);
}
return result;
}
public update(): void {
for (const [resourceId, resourceArray] of this.resourcesMap.entries()) {
for (let i = 0; i < resourceArray.length; i++) {
const resourceEntry = resourceArray[i];
++resourceEntry.frameCount;
if (resourceEntry.frameCount > this.unusedResourceLifeTime) {
resourceEntry.resource.delete();
resourceArray.splice(i, 1);
--i;
}
}
if (resourceArray.length === 0) {
this.resourcesMap.delete(resourceId);
}
}
}
}