-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourcePool.js
More file actions
42 lines (41 loc) · 1.03 KB
/
ResourcePool.js
File metadata and controls
42 lines (41 loc) · 1.03 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
class ResourcePool {
constructor() {
this.pool = []
}
request() {
if (this.pool.length == 0) {
console.log("empty")
}
return this.pool.pop()
}
returnToPool(resource) {
this.pool.push(resource)
}
}
class AudioPool extends ResourcePool {
constructor(filepath, count) {
super()
this.filepath = filepath
this.count = count
while (this.pool.length < count) {
this.addToPool()
}
}
request() {
if (this.pool.length == 0) {
console.log("no more objects, need to create more")
while (this.pool.length < this.count) {
this.addToPool()
}
}
return this.pool.pop()
}
addToPool() {
var workaround = this
var audio = new Audio(this.filepath)
audio.onended = function() { // automatically return to pool when finished playing
workaround.returnToPool(audio)
}
this.pool.push(audio)
}
}