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
This commit is contained in:
Oskar Thoren 2018-01-04 23:25:13 +09:00
parent fa61ef8e1d
commit 751998e980
No known key found for this signature in database
GPG Key ID: 5128AB0637CD85AF
7 changed files with 36 additions and 1 deletions

1
.env
View File

@ -7,3 +7,4 @@ OFFLINE_INBOX_ENABLED=1
LOG_LEVEL=debug
LOG_LEVEL_STATUS_GO=info
JSC_ENABLED=1
QUEUE_MESSAGE_ENABLED=1

View File

@ -7,3 +7,4 @@ OFFLINE_INBOX_ENABLED=1
LOG_LEVEL=debug
LOG_LEVEL_STATUS_GO=info
JSC_ENABLED=0
QUEUE_MESSAGE_ENABLED=1

View File

@ -7,3 +7,4 @@ OFFLINE_INBOX_ENABLED=0
LOG_LEVEL=info
LOG_LEVEL_STATUS_GO=
JSC_ENABLED=0
QUEUE_MESSAGE_ENABLED=0

View File

@ -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

View File

@ -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]})))

View File

@ -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

View File

@ -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)))