Skip to content

Commit 484669c

Browse files
committed
Added new ProcessQueue struct for classes that need this pattern a lot
1 parent f165acf commit 484669c

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

v3/src/structs/ProcessQueue.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Phaser.Structs.ProcessQueue
2+
3+
var Class = require('../utils/Class');
4+
5+
var ProcessQueue = new Class({
6+
7+
initialize:
8+
9+
function ProcessQueue ()
10+
{
11+
this._pending = [];
12+
this._active = [];
13+
this._destroy = [];
14+
15+
this._toProcess = 0;
16+
},
17+
18+
add: function (item)
19+
{
20+
this._pending.push(item);
21+
22+
this._toProcess++;
23+
24+
return this;
25+
},
26+
27+
remove: function (item)
28+
{
29+
this._destroy.push(item);
30+
31+
this._toProcess++;
32+
33+
return this;
34+
},
35+
36+
update: function ()
37+
{
38+
if (this._toProcess === 0)
39+
{
40+
// Quick bail
41+
return this._active;
42+
}
43+
44+
var list = this._destroy;
45+
var active = this._active;
46+
var i;
47+
var item;
48+
49+
// Clear the 'destroy' list
50+
for (i = 0; i < list.length; i++)
51+
{
52+
item = list[i];
53+
54+
// Remove from the 'active' array
55+
var idx = active.indexOf(item);
56+
57+
if (idx !== -1)
58+
{
59+
active.splice(idx, 1);
60+
}
61+
}
62+
63+
list.length = 0;
64+
65+
// Process the pending addition list
66+
// This stops callbacks and out of sync events from populating the active array mid-way during an update
67+
68+
list = this._pending;
69+
70+
for (i = 0; i < list.length; i++)
71+
{
72+
item = list[i];
73+
74+
this._active.push(item);
75+
}
76+
77+
list.length = 0;
78+
79+
this._toProcess = 0;
80+
81+
// The owner of this queue can now safely do whatever it needs to with the active list
82+
return this._active;
83+
},
84+
85+
getActive: function ()
86+
{
87+
return this._active;
88+
},
89+
90+
destroy: function ()
91+
{
92+
this._pending = [];
93+
this._active = [];
94+
this._destroy = [];
95+
}
96+
97+
});
98+
99+
module.exports = ProcessQueue;

v3/src/structs/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
module.exports = {
44

5+
List: require('./List'),
56
Map: require('./Map'),
7+
ProcessQueue: require('./ProcessQueue'),
68
RTree: require('./RTree'),
79
Set: require('./Set')
810

0 commit comments

Comments
 (0)