wgge 2.0.35 → 2.0.38

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.
@@ -20,7 +20,7 @@ export default class ActivatedTreeNode {
20
20
  /**
21
21
  * @type boolean
22
22
  */
23
- isRemoved= false;
23
+ isRemoved = false;
24
24
 
25
25
  /**
26
26
  *
@@ -107,28 +107,29 @@ export default class ActivatedTreeNode {
107
107
  }
108
108
 
109
109
  activate() {
110
- if (!this.isActivated) {
111
- this.activateInternal();
112
-
113
- if (this.autoRegisterEvents) {
114
- const events = this.autoRegisterEvents;
115
- for (let i = 0, max = events.length; i < max; i++) {
116
- const event = events[i];
117
- event.node.addEventListener(event.name, event.handler);
118
- if (event.runOnActivate) {
119
- event.handler();
120
- }
121
- }
122
- }
110
+ if (this.isActivated) return;
123
111
 
124
- const children = this.children;
125
- for (let i = 0, max = children.length; i < max; i++) {
126
- children[i].activate();
112
+ this.activateInternal();
113
+
114
+ if (this.autoRegisterEvents) {
115
+ const events = this.autoRegisterEvents;
116
+ for (let i = 0, max = events.length; i < max; i++) {
117
+ const event = events[i];
118
+ event.node.addEventListener(event.name, event.handler);
119
+ if (event.runOnActivate) {
120
+ event.handler();
121
+ }
127
122
  }
123
+ }
128
124
 
129
- this.isActivated = true;
130
- this.afterActivatedInternal();
125
+ const children = this.children;
126
+ for (let i = 0, max = children.length; i < max; i++) {
127
+ children[i].activate();
131
128
  }
129
+
130
+ this.isActivated = true;
131
+ this.afterActivatedInternal();
132
+
132
133
  }
133
134
 
134
135
  activateInternal() {
@@ -140,23 +141,23 @@ export default class ActivatedTreeNode {
140
141
  }
141
142
 
142
143
  deactivate() {
143
- if (this.isActivated) {
144
- if (this.autoRegisterEvents) {
145
- const events = this.autoRegisterEvents;
146
- for (let i = 0, max = events.length; i < max; i++) {
147
- const event = events[i];
148
- event.node.removeEventListener(event.name, event.handler);
149
- }
150
- }
144
+ if (!this.isActivated) return;
151
145
 
152
- const children = this.children;
153
- for (let i = 0, max = children.length; i < max; i++) {
154
- children[i].deactivate();
146
+ if (this.autoRegisterEvents) {
147
+ const events = this.autoRegisterEvents;
148
+ for (let i = 0, max = events.length; i < max; i++) {
149
+ const event = events[i];
150
+ event.node.removeEventListener(event.name, event.handler);
155
151
  }
152
+ }
156
153
 
157
- this.deactivateInternal();
158
- this.isActivated = false;
154
+ const children = this.children;
155
+ for (let i = 0, max = children.length; i < max; i++) {
156
+ children[i].deactivate();
159
157
  }
158
+
159
+ this.deactivateInternal();
160
+ this.isActivated = false;
160
161
  }
161
162
 
162
163
  deactivateInternal() {
@@ -181,7 +182,7 @@ export default class ActivatedTreeNode {
181
182
  }
182
183
 
183
184
  addAutoEvents(node, events, handler, runOnActivate = false) {
184
- events.forEach((event) => this.addAutoEvent(node, event, handler, runOnActivate));
185
+ events.forEach((event, i) => this.addAutoEvent(node, event, handler, i === 0 && runOnActivate));
185
186
  }
186
187
 
187
188
  addAutoEventMultiple(nodes, event, handler, runOnActivate = false) {
@@ -1,46 +1,53 @@
1
1
  const DEBUG_EVENT_MANAGER = false;
2
2
 
3
3
  export default class EventManager {
4
- handlers;
5
4
 
6
- constructor() {
7
- this.handlers = [];
8
- }
5
+ handlers = new Map();
9
6
 
10
7
  addEventListener(eventName, eventHandler) {
11
- if (!this.handlers[eventName]) {
12
- this.handlers[eventName] = [];
8
+ if (!this.handlers.has(eventName)) {
9
+ this.handlers.set(eventName, [eventHandler]);
10
+ return;
13
11
  }
12
+ /*
14
13
  const index = this.handlers[eventName].indexOf(eventHandler);
15
14
  if (index >= 0) {
16
15
  if (DEBUG_EVENT_MANAGER) console.log('Event listener already registered.');
17
16
  return;
18
- }
19
- return this.handlers[eventName].push(eventHandler);
17
+ }*/
18
+ const handlers = this.handlers.get(eventName);
19
+ handlers.push(eventHandler);
20
20
  }
21
21
 
22
22
  removeEventListener(eventName, eventHandler) {
23
- if (!this.handlers[eventName]) {
24
- this.handlers[eventName] = [];
23
+ if (!this.handlers.has(eventName)) {
24
+ return;
25
+ }
26
+ const handlers = this.handlers.get(eventName);
27
+ const index = handlers.indexOf(eventHandler);
28
+ if (index === 0 && handlers.length === 0) {
29
+ this.handlers.delete(eventName);
30
+ return;
25
31
  }
26
- const index = this.handlers[eventName].indexOf(eventHandler);
27
32
  if (index >= 0) {
28
- this.handlers[eventName].splice(index, 1);
33
+ handlers.splice(index, 1);
29
34
  } else {
30
- if (DEBUG_EVENT_MANAGER) console.log('Event listener cannot be removed - not found.');
35
+ console.warn(`Event listener '${eventName}' cannot be removed - not found.`);
31
36
  }
32
37
  }
33
38
 
34
39
  triggerEvent(eventName, param) {
35
- if (!this.handlers[eventName]) {
40
+ if (!this.handlers.has(eventName)) {
36
41
  return;
37
42
  }
38
- const handlers = [...this.handlers[eventName]];
39
- handlers.forEach((item) => item(param));
43
+ const handlers = [...this.handlers.get(eventName)];
44
+ for (let i = 0, max = handlers.length; i < max; i++) {
45
+ handlers[i](param);
46
+ }
40
47
  }
41
48
 
42
49
  reset() {
43
- this.handlers = [];
50
+ this.handlers.clear();
44
51
  }
45
52
 
46
53
  }
@@ -28,10 +28,10 @@ export default class NodeWithEvents {
28
28
  this.eventManager.triggerEvent(eventName, param);
29
29
  }
30
30
  }
31
-
31
+ /*
32
32
  static isNodeWithEvents(node) {
33
33
  if (!node) return false;
34
34
  return (typeof node === 'object' && typeof node.addEventListener === 'function');
35
35
  }
36
-
36
+ */
37
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wgge",
3
- "version": "2.0.35",
3
+ "version": "2.0.38",
4
4
  "description": "Web GL Game Engine",
5
5
  "main": "index.js",
6
6
  "devDependencies": {