Skip to content

Commit

Permalink
Merge pull request #6961 from lostgeek/lobby-inactivity
Browse files Browse the repository at this point in the history
Pause lobby updates when lobby view is closed or window is inactive
  • Loading branch information
NoahTheDuke authored Jul 30, 2023
2 parents a41924c + 0e8a627 commit 6bc8391
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 24 deletions.
10 changes: 9 additions & 1 deletion src/clj/web/app_state.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

(defn register-user
[app-state uid user]
(assoc-in app-state [:users uid] (assoc user :uid uid)))
(assoc-in app-state [:users uid] (assoc user :uid uid :lobby-updates true)))

(defn uid->lobby
([uid] (uid->lobby (:lobbies @app-state) uid))
Expand Down Expand Up @@ -57,3 +57,11 @@
new-users (dissoc users uid)
_ (println "NEW USERS" new-users)]
(swap! app-state #(assoc %1 :users new-users))))

(defn pause-lobby-updates
[uid]
(swap! app-state assoc-in [:users uid :lobby-updates] false))

(defn continue-lobby-updates
[uid]
(swap! app-state assoc-in [:users uid :lobby-updates] true))
14 changes: 13 additions & 1 deletion src/clj/web/lobby.clj
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@
([]
(let [user-cache (:users @app-state/app-state)
uids (ws/connected-uids)
users (map #(get user-cache %) uids)]
users (map #(get user-cache %) uids)
users (filter #(:lobby-updates %) users)]
(broadcast-lobby-list users)))
([users]
(assert (or (sequential? users) (nil? users)) (str "Users must be a sequence: " (pr-str users)))
Expand Down Expand Up @@ -651,3 +652,14 @@
(if (and lobby (in-lobby? uid lobby))
(update-in lobbies [gameid :mute-spectators] not)
lobbies)))

(defmethod ws/-msg-handler :lobby/pause-updates
[{{user :user} :ring-req
uid :uid}]
(app-state/pause-lobby-updates uid))

(defmethod ws/-msg-handler :lobby/continue-updates
[{{user :user} :ring-req
uid :uid}]
(app-state/continue-lobby-updates uid)
(send-lobby-list uid))
27 changes: 21 additions & 6 deletions src/cljs/nr/lobby.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,27 @@
[load-replay-button s games current-game user]])])

(defn games-list-panel [state games current-game user visible-formats]
[:div.games
[button-bar state games current-game user visible-formats]
(if (= "angel-arena" (:room @state))
[angel-arena/game-list state {:games games
:current-game current-game}]
[game-list state user games current-game])])
(r/create-class
{:display-name "games-list"
:component-did-mount
(fn []
(ws/lobby-updates-continue!))
:component-will-unmount
(fn []
(ws/lobby-updates-pause!))

:reagent-render
(fn []
[:div.games
[button-bar state games current-game user visible-formats]
(if @ws/lobby-updates-state
(if (= "angel-arena" (:room @state))
[angel-arena/game-list state {:games games
:current-game current-game}]
[game-list state user games current-game])
[:div
"Lobby updates halted." ; this should never be visible
[:button {:on-click #(ws/lobby-updates-continue!)} "Reenable lobby updates"]])])}))

(defn right-panel
[state decks current-game user]
Expand Down
45 changes: 29 additions & 16 deletions src/cljs/nr/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[nr.navbar :refer [navbar navigate]]
[nr.routes :as routes]
[nr.status-bar :refer [status]]
[nr.ws :refer [start-router!]]
[nr.ws :refer [start-router! lobby-updates-pause! lobby-updates-continue!]]
[reagent-modals.modals :as reagent-modals]
[reagent.core :as r]
[reagent.dom :as rdom]
Expand All @@ -20,21 +20,34 @@
(.getAttribute (str "data-" tag))))

(defn pages []
(r/create-class
{:display-name "main-pages"
:component-did-mount
(fn []
(let [ver (get-server-data "version")
rid (get-server-data "replay-id")]
(swap! app-state assoc :app-version ver)
(swap! app-state assoc :replay-id rid)
(when rid
(navigate "/play"))))
:reagent-render
(fn []
[:div#main
[:div.item
[(-> @routes/current-view :data :view)]]])}))
(r/with-let
[visibilitychange-fn (fn []
(if (identical? (.-visibilityState js/document) "visible")
(lobby-updates-continue!)
(lobby-updates-pause!)))]
(r/create-class
{:display-name "main-pages"
:component-did-mount
(fn []
(let [ver (get-server-data "version")
rid (get-server-data "replay-id")]
(swap! app-state assoc :app-version ver)
(swap! app-state assoc :replay-id rid)
(when rid
(navigate "/play"))
(-> js/document (.addEventListener
"visibilitychange"
visibilitychange-fn))))
:component-will-unmount
(fn []
(-> js/document (.removeEventListener
"visibilitychange"
visibilitychange-fn)))
:reagent-render
(fn []
[:div#main
[:div.item
[(-> @routes/current-view :data :view)]]])})))

(defn main-window []
[:<>
Expand Down
10 changes: 10 additions & 0 deletions src/cljs/nr/ws.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[nr.ajax :refer [?csrf-token]]
[nr.appstate :refer [app-state current-gameid]]
[nr.utils :refer [non-game-toast]]
[reagent.core :as r]
[taoensso.sente :as sente :refer [start-client-chsk-router!]]))

(defonce lock (atom false))
Expand Down Expand Up @@ -70,3 +71,12 @@
(start-client-chsk-router!
ch-chsk
event-msg-handler-wrapper)))

(def lobby-updates-state (r/atom true))
(defn lobby-updates-pause! []
(ws-send! [:lobby/pause-updates])
(reset! lobby-updates-state false))

(defn lobby-updates-continue! []
(ws-send! [:lobby/continue-updates])
(reset! lobby-updates-state true))

0 comments on commit 6bc8391

Please sign in to comment.