From 751998e980d01122f1eea18b7520a2e91a2d5298 Mon Sep 17 00:00:00 2001 From: Oskar Thoren Date: Thu, 4 Jan 2018 23:25:13 +0900 Subject: [PATCH] Intercept received messages and queue them - Exposes behavior under QUEUE_MESSAGE_ENABLED flag that is turned off in production. - Delays messages to allow for reordering of latecoming messages up to 500ms --- .env | 1 + .env.jenkins | 1 + .env.prod | 1 + src/status_im/chat/events.cljs | 1 + src/status_im/chat/events/queue_message.cljs | 30 ++++++++++++++++++++ src/status_im/protocol/handlers.cljs | 2 +- src/status_im/utils/config.cljs | 1 + 7 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/status_im/chat/events/queue_message.cljs diff --git a/.env b/.env index 971e63785c..491608bf4e 100644 --- a/.env +++ b/.env @@ -7,3 +7,4 @@ OFFLINE_INBOX_ENABLED=1 LOG_LEVEL=debug LOG_LEVEL_STATUS_GO=info JSC_ENABLED=1 +QUEUE_MESSAGE_ENABLED=1 \ No newline at end of file diff --git a/.env.jenkins b/.env.jenkins index 38fa94b86e..beeff5c1b6 100644 --- a/.env.jenkins +++ b/.env.jenkins @@ -7,3 +7,4 @@ OFFLINE_INBOX_ENABLED=1 LOG_LEVEL=debug LOG_LEVEL_STATUS_GO=info JSC_ENABLED=0 +QUEUE_MESSAGE_ENABLED=1 diff --git a/.env.prod b/.env.prod index 18f4650c3f..58e7942d0c 100644 --- a/.env.prod +++ b/.env.prod @@ -7,3 +7,4 @@ OFFLINE_INBOX_ENABLED=0 LOG_LEVEL=info LOG_LEVEL_STATUS_GO= JSC_ENABLED=0 +QUEUE_MESSAGE_ENABLED=0 \ No newline at end of file diff --git a/src/status_im/chat/events.cljs b/src/status_im/chat/events.cljs index f5020b7316..3d963d14d0 100644 --- a/src/status_im/chat/events.cljs +++ b/src/status_im/chat/events.cljs @@ -20,6 +20,7 @@ status-im.chat.events.commands status-im.chat.events.requests status-im.chat.events.animation + status-im.chat.events.queue-message status-im.chat.events.receive-message status-im.chat.events.sign-up status-im.chat.events.console diff --git a/src/status_im/chat/events/queue_message.cljs b/src/status_im/chat/events/queue_message.cljs new file mode 100644 index 0000000000..eca1d67bb6 --- /dev/null +++ b/src/status_im/chat/events/queue_message.cljs @@ -0,0 +1,30 @@ +(ns status-im.chat.events.queue-message + (:require-macros [cljs.core.async.macros :as async-macros]) + (:require [cljs.core.async :as async] + [re-frame.core :as re-frame] + [status-im.utils.config :as config] + [status-im.utils.pre-receiver :as pre-receiver] + [status-im.utils.handlers :as handlers])) + +;; We queue messaged before receiving them to allow lagging messages to catch up. +;; This ensures proper ordering from a user's POV the vast majority of the time. + +;; XXX(oskarth): Hacky def, consider better encapsulation +(when config/queue-message-enabled? + (def in-ch (pre-receiver/start! + {:delay-ms 500 + :reorder? true + :add-fn #(re-frame/dispatch [:chat-received-message/add %])}))) + +;; NOTE(oskarth): in-ch is assumed to exist +(re-frame/reg-fx + ::queue-message + (fn [message] + (async/put! in-ch message))) + +(handlers/register-handler-fx + :pre-received-message + (fn [_ [_ message]] + (if config/queue-message-enabled? + {::queue-message message} + {:dispatch [:chat-received-message/add message]}))) diff --git a/src/status_im/protocol/handlers.cljs b/src/status_im/protocol/handlers.cljs index d0c31f8965..d7fe485137 100644 --- a/src/status_im/protocol/handlers.cljs +++ b/src/status_im/protocol/handlers.cljs @@ -316,7 +316,7 @@ route-fx (case type (:message :group-message - :public-group-message) {:dispatch [:chat-received-message/add (transform-protocol-message message)]} + :public-group-message) {:dispatch [:pre-received-message (transform-protocol-message message)]} :pending (cond-> {::pending-messages-save message} chat-message (assoc :dispatch diff --git a/src/status_im/utils/config.cljs b/src/status_im/utils/config.cljs index 8d9f5d6d2e..e42b2c1277 100644 --- a/src/status_im/utils/config.cljs +++ b/src/status_im/utils/config.cljs @@ -30,3 +30,4 @@ keyword)) (def jsc-enabled? (enabled? (get-config :JSC_ENABLED 0))) +(def queue-message-enabled? (enabled? (get-config :QUEUE_MESSAGE_ENABLED 0)))