forked from google/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpools.js
More file actions
138 lines (119 loc) · 3.91 KB
/
Copy pathpools.js
File metadata and controls
138 lines (119 loc) · 3.91 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
goog.provide('app.shared.pools');
app.shared.pools = (function() {
const objects = [];
return {
mixin(obj, options) {
options = options || {};
obj.pool_ = [];
objects.push(obj);
/**
* Get an object from the pool or create a new one.
* @param {Object} owner An owner which is passed to the constructor.
* @return {Object} A new or reused object. Or null if a fixed pool is empty.
*/
obj.pop = function(owner) {
ensurePooled_(owner);
// Get instance from the pool and initialize it
const instance = obj.pool_.shift();
return initInstance_(instance, arguments);
};
/**
* Get a random object from the pool or create a new one.
* @param {Object} owner An owner which is passed to the constructor.
* @return {Object} A new or reused object. Or null if a fixed pool is empty.
*/
obj.popRandom = function(owner) {
ensurePooled_(owner);
const randomIndex = Math.floor(Math.random() * obj.pool_.length);
const instance = obj.pool_[randomIndex];
const lastInstance = obj.pool_.pop();
if (lastInstance && instance !== lastInstance) {
obj.pool_[randomIndex] = lastInstance;
}
return initInstance_(instance, arguments);
};
/**
* Add an object to the pool.
*/
obj.pool = function(owner) {
// Create new instance
const instance = new obj(owner);
// Add to pool
obj.pool_.push(instance);
};
/**
* Return an used object for reuse.
* @param {Object} instance The instance of the object.
*/
obj.push = function(instance) {
if (instance.onDispose) {
instance.onDispose();
}
obj.pool_.push(instance);
};
/**
* Prototype method to return the object for reuse.
*/
obj.prototype.remove = function() {
obj.push(this);
};
/**
* Inheritable handlers for initializing and disposing an object
* when leaving or entering the pool.
*/
obj.prototype.onInit = obj.prototype.onInit || function() {};
obj.prototype.onDispose = obj.prototype.onDispose || function() {};
// Private functions
/**
* Conditionally pools an item for use if needed and allowed.
* @param {Object} owner
* @private
*/
function ensurePooled_(owner) {
if (!options.fixed && obj.pool_.length === 0) {
obj.pool(owner);
}
}
/**
* Initialises a pooled instance if one exists.
* @param {Object} instance
* @param {!Arguments} popArgs
* @return {Object}
* @private
*/
function initInstance_(instance, popArgs) {
if (instance) {
instance.onInit.apply(instance,
Array.prototype.slice.call(popArgs, 1));
}
return instance || null;
}
},
empty() {
for (let i = 0; i < objects.length; i++) {
objects[i].pool_ = [];
}
}
};
}());
/** @interface */
app.shared.pools.PoolType = function() {};
/** @param {...*} var_args */
app.shared.pools.PoolType.onInit = function(var_args) {};
app.shared.pools.PoolType.onDispose = function(var_args) {};
app.shared.pools.PoolType.remove = function() {};