From 273af3f3aad574b17e9602ba5efc6077c0bf21f6 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 24 Jul 2019 14:30:48 +0100 Subject: [PATCH] refactor subscribe resolution --- .../real-time/app/coffee/DocumentUpdaterController.coffee | 5 +---- services/real-time/app/coffee/RoomManager.coffee | 5 +++++ services/real-time/app/coffee/WebsocketLoadBalancer.coffee | 5 +---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/services/real-time/app/coffee/DocumentUpdaterController.coffee b/services/real-time/app/coffee/DocumentUpdaterController.coffee index 6d6ec4b79d..2611d484ad 100644 --- a/services/real-time/app/coffee/DocumentUpdaterController.coffee +++ b/services/real-time/app/coffee/DocumentUpdaterController.coffee @@ -36,10 +36,7 @@ module.exports = DocumentUpdaterController = roomEvents.on 'doc-active', (doc_id) -> subscribePromises = for rclient in rclientSubList ChannelManager.subscribe rclient, "applied-ops", doc_id - subscribeResult = Promise.all(subscribePromises) - emitResult = (err) => this.emit("doc-subscribed-#{doc_id}", err) - subscribeResult.then () -> emitResult() - subscribeResult.catch (err) -> emitResult(err) + RoomManager.emitOnCompletion(subscribePromises, "doc-subscribed-#{doc_id}") roomEvents.on 'doc-empty', (doc_id) -> for rclient in rclientSubList ChannelManager.unsubscribe rclient, "applied-ops", doc_id diff --git a/services/real-time/app/coffee/RoomManager.coffee b/services/real-time/app/coffee/RoomManager.coffee index 08e65b5e52..152a290020 100644 --- a/services/real-time/app/coffee/RoomManager.coffee +++ b/services/real-time/app/coffee/RoomManager.coffee @@ -35,6 +35,11 @@ module.exports = RoomManager = entity = IdMap.get(id) @leaveEntity client, entity, id + emitOnCompletion: (promiseList, eventName) -> + result = Promise.all(promiseList) + result.then () -> RoomEvents.emit(eventName) + result.catch (err) -> RoomEvents.emit(eventName, err) + eventSource: () -> return RoomEvents diff --git a/services/real-time/app/coffee/WebsocketLoadBalancer.coffee b/services/real-time/app/coffee/WebsocketLoadBalancer.coffee index e8ff88aa7b..865249c63e 100644 --- a/services/real-time/app/coffee/WebsocketLoadBalancer.coffee +++ b/services/real-time/app/coffee/WebsocketLoadBalancer.coffee @@ -42,10 +42,7 @@ module.exports = WebsocketLoadBalancer = roomEvents.on 'project-active', (project_id) -> subscribePromises = for rclient in rclientSubList ChannelManager.subscribe rclient, "editor-events", project_id - subscribeResult = Promise.all(subscribePromises) - emitResult = (err) => this.emit("project-subscribed-#{project_id}", err) - subscribeResult.then () -> emitResult() - subscribeResult.catch (err) -> emitResult(err) + RoomManager.emitOnCompletion(subscribePromises, "project-subscribed-#{project_id}") roomEvents.on 'project-empty', (project_id) -> for rclient in rclientSubList ChannelManager.unsubscribe rclient, "editor-events", project_id