forked from xeolabs/scenejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
184 lines (141 loc) · 4.55 KB
/
events.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @private
*/
var SceneJS_events = new (function () {
this.ERROR = 0;
this.RESET = 1; // SceneJS framework reset
this.NODE_CREATED = 2; // Scene has just been created
this.SCENE_CREATED = 3; // Scene has just been created
this.SCENE_COMPILING = 4; // Scene about to be compiled and drawn
this.SCENE_DESTROYED = 5; // Scene just been destroyed
this.OBJECT_COMPILING = 6;
this.WEBGL_CONTEXT_LOST = 7;
this.WEBGL_CONTEXT_RESTORED = 8;
this.RENDER = 9;
/* Priority queue for each type of event
*/
var events = [];
/**
* Registers a handler for the given event and returns a subscription handle
*
* The handler can be registered with an optional priority number which specifies the order it is
* called among the other handler already registered for the event.
*
* So, with n being the number of commands registered for the given event:
*
* (priority <= 0) - command will be the first called
* (priority >= n) - command will be the last called
* (0 < priority < n) - command will be called at the order given by the priority
* @private
* @param type Event type - one of the values in SceneJS_events
* @param command - Handler function that will accept whatever parameter object accompanies the event
* @param priority - Optional priority number (see above)
* @return {String} - Subscription handle
*/
this.addListener = function (type, command, priority) {
var list = events[type];
if (!list) {
list = [];
events[type] = list;
}
var handler = {
command:command,
priority:(priority == undefined) ? list.length : priority
};
var index = -1;
for (var i = 0, len = list.length; i < len; i++) {
if (!list[i]) {
index = i;
break;
}
}
if (index < 0) {
list.push(handler);
index = list.length - 1;
}
//
// for (var i = 0; i < list.length; i++) {
// if (list[i].priority > handler.priority) {
// list.splice(i, 0, handler);
// return i;
// }
// }
var handle = type + "." + index;
return handle;
};
/**
* Removes a listener
* @param handle Subscription handle
*/
this.removeListener = function (handle) {
var lastIdx = handle.lastIndexOf(".");
var type = parseInt(handle.substr(0, lastIdx));
var index = parseInt(handle.substr(lastIdx + 1));
var list = events[type];
if (!list) {
return;
}
delete list[index];
};
/**
* @private
*/
this.fireEvent = function (type, params) {
var list = events[type];
if (list) {
params = params || {};
for (var i = 0; i < list.length; i++) {
if (list[i]) {
list[i].command(params);
}
}
}
};
})();
/**
* Subscribe to SceneJS events
* @deprecated
*/
SceneJS.bind = function (name, func) {
switch (name) {
case "error" :
return SceneJS_events.addListener(SceneJS_events.ERROR, func);
break;
case "reset" :
return SceneJS_events.addListener(
SceneJS_events.RESET,
function () {
func();
});
break;
case "webglcontextlost" :
return SceneJS_events.addListener(
SceneJS_events.WEBGL_CONTEXT_LOST,
function (params) {
func(params);
});
break;
case "webglcontextrestored" :
return SceneJS_events.addListener(
SceneJS_events.WEBGL_CONTEXT_RESTORED,
function (params) {
func(params);
});
break;
default:
throw SceneJS_error.fatalError("SceneJS.bind - this event type not supported: '" + name + "'");
}
};
/* Subscribe to SceneJS events
* @deprecated
*/
SceneJS.onEvent = SceneJS.bind;
/* Unsubscribe from event
*/
SceneJS.unEvent = function (handle) {
return SceneJS_events.removeListener(handle);
};
SceneJS.subscribe = SceneJS.addListener = SceneJS.onEvent = SceneJS.bind;
SceneJS.unsubscribe = SceneJS.unEvent;
SceneJS.on = SceneJS.onEvent;
SceneJS.off = SceneJS.unEvent;