|
@@ -166,37 +166,44 @@ Tools.drawAndSend = function (data) {
|
166
|
166
|
//is loaded. keys : the name of the tool, values : array of messages for this tool
|
167
|
167
|
Tools.pendingMessages = {};
|
168
|
168
|
|
169
|
|
-//Receive draw instructions from the server
|
170
|
|
-Tools.socket.on("broadcast", function (message) {
|
171
|
|
- function messageForTool(message) {
|
172
|
|
- var name = message.tool,
|
173
|
|
- tool = Tools.list[name];
|
174
|
|
- if (tool) {
|
175
|
|
- Tools.applyHooks(Tools.messageHooks, message);
|
176
|
|
- tool.draw(message, false);
|
177
|
|
- } else {
|
178
|
|
- ///We received a message destinated to a tool that we don't have
|
179
|
|
- //So we add it to the pending messages
|
180
|
|
- if (!Tools.pendingMessages[name]) Tools.pendingMessages[name] = [message];
|
181
|
|
- else Tools.pendingMessages[name].push(message);
|
182
|
|
- }
|
|
169
|
+// Send a message to the corresponding tool
|
|
170
|
+function messageForTool(message) {
|
|
171
|
+ var name = message.tool,
|
|
172
|
+ tool = Tools.list[name];
|
|
173
|
+ if (tool) {
|
|
174
|
+ Tools.applyHooks(Tools.messageHooks, message);
|
|
175
|
+ tool.draw(message, false);
|
|
176
|
+ } else {
|
|
177
|
+ ///We received a message destinated to a tool that we don't have
|
|
178
|
+ //So we add it to the pending messages
|
|
179
|
+ if (!Tools.pendingMessages[name]) Tools.pendingMessages[name] = [message];
|
|
180
|
+ else Tools.pendingMessages[name].push(message);
|
183
|
181
|
}
|
|
182
|
+}
|
184
|
183
|
|
185
|
|
- //Check if the message is in the expected format
|
186
|
|
- if (message.tool) {
|
187
|
|
- messageForTool(message);
|
188
|
|
- }
|
189
|
|
- if (message._children) {
|
190
|
|
- for (var i = 0; i < message._children.length; i++) {
|
191
|
|
- //Apply hooks on children too
|
192
|
|
- var msg = message._children[i];
|
193
|
|
- messageForTool(msg);
|
194
|
|
- }
|
|
184
|
+// Apply the function to all arguments by batches
|
|
185
|
+function batchCall(fn, args) {
|
|
186
|
+ var BATCH_SIZE = 512;
|
|
187
|
+ if (args.length > 0) {
|
|
188
|
+ var batch = args.slice(0, BATCH_SIZE);
|
|
189
|
+ var rest = args.slice(BATCH_SIZE);
|
|
190
|
+ for (var i = 0; i < batch.length; i++) fn(batch[i]);
|
|
191
|
+ requestAnimationFrame(batchCall.bind(null, fn, rest));
|
195
|
192
|
}
|
|
193
|
+}
|
|
194
|
+
|
|
195
|
+// Call messageForTool recursively on the message and its children
|
|
196
|
+function handleMessage(message) {
|
|
197
|
+ //Check if the message is in the expected format
|
|
198
|
+ if (message.tool) messageForTool(message);
|
|
199
|
+ if (message._children) batchCall(handleMessage, message._children);
|
196
|
200
|
if (!message.tool && !message._children) {
|
197
|
201
|
console.error("Received a badly formatted message (no tool). ", message);
|
198
|
202
|
}
|
199
|
|
-});
|
|
203
|
+}
|
|
204
|
+
|
|
205
|
+//Receive draw instructions from the server
|
|
206
|
+Tools.socket.on("broadcast", handleMessage);
|
200
|
207
|
|
201
|
208
|
Tools.unreadMessagesCount = 0;
|
202
|
209
|
Tools.newUnreadMessage = function () {
|