From 277ec71a5b7db19259dee339897c07fdb6f7beec Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 24 Jul 2019 15:43:48 +0100 Subject: [PATCH] subscribe to doc updates before requesting doc content --- .../app/coffee/WebsocketController.coffee | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/services/real-time/app/coffee/WebsocketController.coffee b/services/real-time/app/coffee/WebsocketController.coffee index 22ea7e7a0c..fce505c1bc 100644 --- a/services/real-time/app/coffee/WebsocketController.coffee +++ b/services/real-time/app/coffee/WebsocketController.coffee @@ -92,33 +92,36 @@ module.exports = WebsocketController = AuthorizationManager.assertClientCanViewProject client, (error) -> return callback(error) if error? - DocumentUpdaterManager.getDocument project_id, doc_id, fromVersion, (error, lines, version, ranges, ops) -> + # ensure the per-doc applied-ops channel is subscribed before sending the + # doc to the client, so that no events are missed. + RoomManager.joinDoc client, doc_id, (error) -> return callback(error) if error? + DocumentUpdaterManager.getDocument project_id, doc_id, fromVersion, (error, lines, version, ranges, ops) -> + return callback(error) if error? - # Encode any binary bits of data so it can go via WebSockets - # See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html - encodeForWebsockets = (text) -> unescape(encodeURIComponent(text)) - escapedLines = [] - for line in lines - try - line = encodeForWebsockets(line) - catch err - logger.err {err, project_id, doc_id, fromVersion, line, client_id: client.id}, "error encoding line uri component" - return callback(err) - escapedLines.push line - if options.encodeRanges - try - for comment in ranges?.comments or [] - comment.op.c = encodeForWebsockets(comment.op.c) if comment.op.c? - for change in ranges?.changes or [] - change.op.i = encodeForWebsockets(change.op.i) if change.op.i? - change.op.d = encodeForWebsockets(change.op.d) if change.op.d? - catch err - logger.err {err, project_id, doc_id, fromVersion, ranges, client_id: client.id}, "error encoding range uri component" - return callback(err) + # Encode any binary bits of data so it can go via WebSockets + # See http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html + encodeForWebsockets = (text) -> unescape(encodeURIComponent(text)) + escapedLines = [] + for line in lines + try + line = encodeForWebsockets(line) + catch err + logger.err {err, project_id, doc_id, fromVersion, line, client_id: client.id}, "error encoding line uri component" + return callback(err) + escapedLines.push line + if options.encodeRanges + try + for comment in ranges?.comments or [] + comment.op.c = encodeForWebsockets(comment.op.c) if comment.op.c? + for change in ranges?.changes or [] + change.op.i = encodeForWebsockets(change.op.i) if change.op.i? + change.op.d = encodeForWebsockets(change.op.d) if change.op.d? + catch err + logger.err {err, project_id, doc_id, fromVersion, ranges, client_id: client.id}, "error encoding range uri component" + return callback(err) - AuthorizationManager.addAccessToDoc client, doc_id - RoomManager.joinDoc client, doc_id, (err) -> + AuthorizationManager.addAccessToDoc client, doc_id logger.log {user_id, project_id, doc_id, fromVersion, client_id: client.id}, "client joined doc" callback null, escapedLines, version, ops, ranges