@urso/core 0.5.13 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urso/core",
3
- "version": "0.5.13",
3
+ "version": "0.6.1",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
package/src/js/app.js CHANGED
@@ -1,4 +1,5 @@
1
1
  class App {
2
+
2
3
  constructor() {
3
4
  this.version = 'APP_VERSION';
4
5
 
@@ -247,6 +247,23 @@ class LibHelper {
247
247
  return newObj;
248
248
  }
249
249
 
250
+ /**
251
+ * rename objects key
252
+ * @param {Object} obj
253
+ * @param {String} oldKey
254
+ * @param {String} newKey
255
+ */
256
+ renameObjectsKey(obj, oldKey, newKey) {
257
+ if (oldKey !== newKey) {
258
+ Object.defineProperty(
259
+ obj, newKey,
260
+ Object.getOwnPropertyDescriptor(obj, oldKey)
261
+ );
262
+
263
+ delete obj[oldKey];
264
+ }
265
+ }
266
+
250
267
  /**
251
268
  * clone object
252
269
  * @param {Object} obj
@@ -1,31 +1,64 @@
1
1
  class ObjectPoolMember {
2
+
2
3
  free = true;
4
+ putCacheTimeKey = 0;
5
+ key = null; //element branch;
6
+ branchKey = 0; //element branch key
3
7
  data = null;
4
8
 
5
- constructor(data) {
6
- this.data = data
9
+ constructor(data, key, branchKey) {
10
+ this.data = data;
11
+ this.key = key;
12
+ this.branchKey = branchKey;
7
13
  }
8
14
  }
9
15
 
16
+
17
+ /**
18
+ * @param {Function} constructorFunction - function to constract element
19
+ * @param {Function} constructorFunction - function to reset element when put back to pool
20
+ * @param {Number} [initialSize] - initial pool size. Use only if you not using keys when constract
21
+ * @param {Number} [maxSize] - max pool size. Old elements will be removed from pool
22
+ */
10
23
  class LibObjectPool {
11
- pool = {};
12
- resetFunction = () => { }
13
- constructorFunction = () => { }
14
24
 
15
- constructor(constructorFunction, resetFunction = (obj) => obj, initialSize = 0) { //todo initialSize
25
+ _pool = {};
26
+ resetFunction = () => { };
27
+ constructorFunction = () => { };
28
+ _maxSize = 0;
29
+
30
+ _lastBranchKeys = {};
31
+ _putTimeCache = {};
32
+
33
+ constructor(constructorFunction, resetFunction = (obj) => obj, initialSize = 0, maxSize = 0) {
16
34
  this.resetFunction = resetFunction;
17
35
  this.constructorFunction = constructorFunction;
36
+ this._maxSize = maxSize;
37
+
38
+ this._createInitial(initialSize);
18
39
  }
19
40
 
41
+ /**
42
+ * get element from pool
43
+ * @param {String | Number} key - element key to get
44
+ * @param {mixed} [additionalCreateArguments] - additional create Arguments
45
+ * @returns {ObjectPoolMember}
46
+ */
20
47
  getElement(key = 'default', additionalCreateArguments) {
21
48
  this._checkPoolKey(key);
22
- const poolArray = this.pool[key];
49
+ const poolBranch = this._pool[key];
23
50
 
51
+ for (const poolObject of Object.values(poolBranch)) {
52
+ if (poolObject.free) {
53
+ poolObject.free = false;
24
54
 
25
- for (let i = 0; i < poolArray.length; i++) {
26
- if (poolArray[i].free) {
27
- poolArray[i].free = false;
28
- return poolArray[i];
55
+ //remove from putTimeCache
56
+ if (poolObject.putCacheTimeKey) {
57
+ delete this._putTimeCache[poolObject.putCacheTimeKey];
58
+ poolObject.putCacheTimeKey = 0;
59
+ }
60
+
61
+ return poolObject;
29
62
  }
30
63
  }
31
64
 
@@ -34,21 +67,137 @@ class LibObjectPool {
34
67
  return newObj;
35
68
  }
36
69
 
70
+ /**
71
+ * put element back to pool (resetFunction will call)
72
+ * @param {ObjectPoolMember} element
73
+ */
37
74
  putElement(element) {
38
75
  element.free = true;
39
76
  this.resetFunction(element.data);
77
+
78
+ if (this._maxSize) {
79
+ //lets write put time cache
80
+ const putCacheTimeKey = this._getElementBranchKey('_putTimeCache', this._putTimeCache);
81
+ this._putTimeCache[putCacheTimeKey] = element;
82
+ element.putCacheTimeKey = putCacheTimeKey;
83
+ }
84
+
85
+ this._checkMaxSize();
86
+ }
87
+
88
+ /**
89
+ * check max pool size
90
+ */
91
+ _checkMaxSize() {
92
+ if (!this._maxSize)
93
+ return;
94
+
95
+ //remove oldest inactive element
96
+ const poolSize = this._getPoolSize();
97
+
98
+ if (poolSize > this._maxSize) {
99
+ this._removeOldestInactiveElement();
100
+ }
101
+
102
+ }
103
+
104
+ /**
105
+ * remove oldest inactive element
106
+ */
107
+ _removeOldestInactiveElement() {
108
+ const keyToRemove = Object.keys(this._putTimeCache)[0];
109
+ const element = this._putTimeCache[keyToRemove];
110
+
111
+ //remove from pool
112
+ delete this._pool[element.key][element.branchKey];
113
+
114
+ //reset element
115
+ element.free = false;
116
+ element.putCacheTimeKey = 0;
117
+ element.key = null;
118
+ element.branchKey = 0;
119
+ element.data = null;
120
+
121
+ //remove from putTimeCache
122
+ delete this._putTimeCache[keyToRemove];
123
+ }
124
+
125
+ /**
126
+ * pool size (all branches)
127
+ */
128
+ _getPoolSize() {
129
+ let size = 0;
130
+
131
+ for (const branch of Object.values(this._pool)) {
132
+ size += Object.keys(branch).length;
133
+ }
134
+
135
+ return size;
40
136
  }
41
137
 
138
+ /**
139
+ * create new element for pool
140
+ * @param {String | Number} key - element key to create
141
+ * @param {mixed} [additionalCreateArguments] - additional create Arguments
142
+ * @returns {ObjectPoolMember}
143
+ */
42
144
  _createElement(key, additionalCreateArguments = null) {
43
145
  const newObj = this.resetFunction(this.constructorFunction(key, additionalCreateArguments));
44
- const poolMember = new ObjectPoolMember(newObj);
45
- this.pool[key].push(poolMember);
146
+ const branchKey = this._getElementBranchKey(key, this._pool[key]);
147
+ const poolMember = new ObjectPoolMember(newObj, key, branchKey);
148
+ this._pool[key][branchKey] = poolMember;
46
149
  return poolMember;
47
150
  }
48
151
 
152
+ /**
153
+ * get element branch key
154
+ * @param {String | Number} key - element key to get
155
+ * @returns {Nymber}
156
+ */
157
+ _getElementBranchKey(key, poolBranch) {
158
+ let branchKey = Urso.time.get();
159
+
160
+ //prevent duplicates
161
+ if (poolBranch[branchKey]) {
162
+ branchKey = this._lastBranchKeys[key] + 1;
163
+ }
164
+
165
+ this._lastBranchKeys[key] = branchKey;
166
+ return branchKey;
167
+ }
168
+
169
+ /**
170
+ * create initial pool objects
171
+ * @param {Number} initialSize
172
+ */
173
+ _createInitial(initialSize) {
174
+ if (!initialSize)
175
+ return;
176
+
177
+ let newObjectsArray = [];
178
+
179
+ for (let index = 0; index < initialSize; index++) {
180
+ newObjectsArray.push(this.getElement());
181
+ }
182
+
183
+ newObjectsArray.forEach(newObject => { this.putElement(newObject); });
184
+ }
185
+
186
+ /**
187
+ * check exist key namespace
188
+ * @param {String | Number} key - element key
189
+ */
49
190
  _checkPoolKey(key) {
50
- if (!this.pool[key])
51
- this.pool[key] = [];
191
+ if (!this._pool[key])
192
+ this._pool[key] = {};
193
+ }
194
+
195
+ /**
196
+ * debug get pool data
197
+ * @returns {Array}
198
+ */
199
+ _debugGetPoolData() {
200
+ return this._pool;
52
201
  }
53
202
  }
54
203
 
@@ -1,4 +1,5 @@
1
1
  class ModulesAssetsModelsImage extends Urso.Core.Modules.Assets.BaseModel {
2
+
2
3
  constructor(params) {
3
4
  super(params);
4
5
  this.type = Urso.types.assets.IMAGE;
@@ -11,4 +12,4 @@ class ModulesAssetsModelsImage extends Urso.Core.Modules.Assets.BaseModel {
11
12
  }
12
13
  }
13
14
 
14
- module.exports = ModulesAssetsModelsImage;
15
+ module.exports = ModulesAssetsModelsImage;
@@ -1,4 +1,5 @@
1
1
  class ModulesAssetsService {
2
+
2
3
  constructor() {
3
4
  this.singleton = true;
4
5
 
@@ -5,6 +5,7 @@ class ModulesInstancesController {
5
5
  constructor() {
6
6
  this._modes = [];
7
7
  this._cache = {};
8
+ this._counter = 0;
8
9
 
9
10
  this.getInstance = this.getInstance.bind(this);
10
11
  this.getByPath = this.getByPath.bind(this);
@@ -73,6 +74,8 @@ class ModulesInstancesController {
73
74
  if (instance.singleton)
74
75
  classObject._instance = instance;
75
76
 
77
+ instance._iuid = this._getUid();
78
+
76
79
  this._launchDefaultFunctions(instance);
77
80
 
78
81
  return instance;
@@ -81,6 +84,11 @@ class ModulesInstancesController {
81
84
  return this._findClass({ path, callback, noModes, modeName });
82
85
  }
83
86
 
87
+ _getUid() {
88
+ this._counter++;
89
+ return 'instance' + this._counter;
90
+ }
91
+
84
92
  _setComonFunctions(classObject, path) {
85
93
  classObject.prototype.getInstance = this._entityGetInstance(path);
86
94
  classObject.prototype.addListener = this._entityAddListener;
@@ -133,7 +141,8 @@ class ModulesInstancesController {
133
141
  if (!callerObject.__entities)
134
142
  callerObject.__entities = [];
135
143
 
136
- callerObject.__entities.push(classObject);
144
+ if (!callerObject.__entities.includes(classObject))
145
+ callerObject.__entities.push(classObject);
137
146
  }
138
147
 
139
148
  _entityAddListener() {
@@ -1,4 +1,5 @@
1
1
  class ModulesObjectsModelsComponent extends Urso.Core.Modules.Objects.BaseModel {
2
+
2
3
  constructor(params) {
3
4
  super(params);
4
5
 
@@ -42,4 +43,4 @@ class ModulesObjectsModelsComponent extends Urso.Core.Modules.Objects.BaseModel
42
43
  }
43
44
  }
44
45
 
45
- module.exports = ModulesObjectsModelsComponent;
46
+ module.exports = ModulesObjectsModelsComponent;
@@ -191,7 +191,7 @@ class ModulesObjectsService {
191
191
  this.destroy(object.contents[0], true);
192
192
 
193
193
  object._customDestroy();
194
- object._baseObject && object._baseObject.destroy();
194
+ object._baseObject && object._baseObject.destroy({children: true});
195
195
  this._removeFromCache(object);
196
196
  this.getInstance('Styles').removeFromCache(object);
197
197