Skip to content

Commit

Permalink
Reload sheets in response to "CHANGED" events
Browse files Browse the repository at this point in the history
Refs #53

It's happening!
  • Loading branch information
dhleong committed Oct 29, 2018
1 parent c90ef98 commit 477b4b7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/cljs/wish/events.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns wish.events
(:require-macros [wish.util.log :refer [log]])
(:require-macros [wish.util.log :as log :refer [log]])
(:require [clojure.string :as str]
[re-frame.core :refer [dispatch reg-event-db reg-event-fx
path
Expand Down Expand Up @@ -210,6 +210,7 @@
; delete the sheet source to trigger a reload
[:db :sheet-sources sheet-id] nil)))


; ======= sheet-related ====================================

(defn- reset-sheet-err
Expand Down Expand Up @@ -681,3 +682,16 @@
(if-not (= current-ids interested-ids)
(log "Drop un-interesting sesssion; was " interested-ids "; now " current-ids)
{:push/connect session-id})))

(reg-event-fx
:reload-changed!
[trim-v (inject-cofx ::inject/sub [:active-sheet-id])]
(fn [{:keys [active-sheet-id]} [changed-ids]]
(when (> (count changed-ids) 1)
; TODO:
(log/todo "Support reloading data sources as well as sheets"))

(when (contains? changed-ids active-sheet-id)
; trigger sheet data reload
{:load-sheet! active-sheet-id})))

46 changes: 42 additions & 4 deletions src/cljs/wish/push.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

(def ^:private push-url-base (str config/push-server "/" push-server-version "/push"))

(def ^:private reload-changed-throttle-ms 750)

; ======= session creation ================================

Expand Down Expand Up @@ -51,10 +52,40 @@
(let [id (keyword id)]
(log/todo "Create watch for sheet " (keyword id))))

;; "changed" handling

(defonce ^:private pending-changes (atom {:timer nil
:ids #{}}))

(defn- reload-changed []
(swap! pending-changes
(fn [{:keys [ids]}]
(>evt [:reload-changed! ids])

; reset state:
{:timer nil :ids #{}})))

(defmethod on-push! :changed
[{{:keys [id]} :data}]
(let [id (keyword id)]
(log/todo "Sheet changed" (keyword id))))
(let [changed-id (keyword id)]
(log "Sheet changed" changed-id)
(swap! pending-changes
(fn [{:keys [timer ids] :as old}]
(if (contains? ids changed-id)
; ignore the dup notification
old

; cancel any old timer and start over
(do
(when timer
(js/clearTimeout timer))

{:ids (conj ids changed-id)
:timer (js/setTimeout
reload-changed
reload-changed-throttle-ms)}))))))

;; fallback

(defmethod on-push! :default
[evt]
Expand All @@ -64,8 +95,15 @@
; ======= connect to a created session ====================

; NOTE event handler fns declared separately to improve hot-reload developer UX
(defn- on-error [e]
(log/warn "Error with push session" e))
(defn- on-error [evt]
; TODO on fatal errors, we should create a new session, maybe?
; An error is also emitted when the connection drops, but it
; will attempt to auto-reconnect....
; NOTE: readyState:
; 0 - CONNECTING
; 1 - OPEN
; 2 - CLOSED
(log/warn "Error with push session; state=" (-> evt (.-target) (.-readyState))))

(defn- on-message [evt]
(let [data (-> (.-data evt)
Expand Down

0 comments on commit 477b4b7

Please sign in to comment.