Browse Source

Improve initial rendering performance

dev_h
Ophir LOJKINE 6 years ago
parent
commit
88aa591e9a
3 changed files with 37 additions and 31 deletions
  1. 32
    25
      client-data/js/board.js
  2. 4
    3
      server/boardData.js
  3. 1
    3
      server/sockets.js

+ 32
- 25
client-data/js/board.js View File

166
 //is loaded. keys : the name of the tool, values : array of messages for this tool
166
 //is loaded. keys : the name of the tool, values : array of messages for this tool
167
 Tools.pendingMessages = {};
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
 	if (!message.tool && !message._children) {
200
 	if (!message.tool && !message._children) {
197
 		console.error("Received a badly formatted message (no tool). ", message);
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
 Tools.unreadMessagesCount = 0;
208
 Tools.unreadMessagesCount = 0;
202
 Tools.newUnreadMessage = function () {
209
 Tools.newUnreadMessage = function () {

+ 4
- 3
server/boardData.js View File

138
  * @param {string} [id] - Identifier of the first element to get.
138
  * @param {string} [id] - Identifier of the first element to get.
139
  * @param {BoardData~processData} callback - Function to be called with each piece of data read
139
  * @param {BoardData~processData} callback - Function to be called with each piece of data read
140
  */
140
  */
141
-BoardData.prototype.getAll = function (id, callback) {
142
-	if (!callback) callback = id;
141
+BoardData.prototype.getAll = function (id) {
142
+	var results = [];
143
 	for (var i in this.board) {
143
 	for (var i in this.board) {
144
 		if (!id || i > id) {
144
 		if (!id || i > id) {
145
-			callback(this.board[i]);
145
+			results.push(this.board[i]);
146
 		}
146
 		}
147
 	}
147
 	}
148
+	return results;
148
 };
149
 };
149
 
150
 
150
 /**
151
 /**

+ 1
- 3
server/sockets.js View File

50
 
50
 
51
 		//Send all the board's data as soon as it's loaded
51
 		//Send all the board's data as soon as it's loaded
52
 		var sendIt = function () {
52
 		var sendIt = function () {
53
-			board_data.getAll(function (data) {
54
-				socket.emit("broadcast", data);
55
-			});
53
+			socket.emit("broadcast", { _children: board_data.getAll() });
56
 		};
54
 		};
57
 
55
 
58
 		if (board_data.ready) sendIt();
56
 		if (board_data.ready) sendIt();

Loading…
Cancel
Save