Skip to content

Commit

Permalink
Ensure context is always provided as React state.
Browse files Browse the repository at this point in the history
Prefer `:as` over `:refer` for uix.
  • Loading branch information
samcf committed Oct 13, 2024
1 parent f7c30bb commit 7b9cbb0
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/main/ogres/app/provider/cursor.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns ogres.app.provider.cursor
(:require [ogres.app.hooks :refer [use-publish use-query use-event-listener]]
[uix.core :as uix :refer [use-callback defui]]))
[uix.core :as uix :refer [defui]]))

(def ^:private handler-query
[[:bounds/self :default [0 0 0 0]]
Expand All @@ -15,7 +15,7 @@
{[cx cy] :camera/point
scale :camera/scale} :user/camera} result]
(use-event-listener js/window "pointermove"
(use-callback
(uix/use-callback
(fn [event]
(if-let [element (.. event -target (closest "#scene-drag"))]
(let [data (.-dataset element)]
Expand Down
11 changes: 5 additions & 6 deletions src/main/ogres/app/provider/dispatch.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
[ogres.app.events :refer [event-tx-fn]]
[ogres.app.provider.events :as events]
[ogres.app.provider.state :as state]
[uix.core :refer [$ create-context defui use-callback use-context]]))
[uix.core :as uix :refer [defui $]]))

(def ^:private context
(create-context nil))
(def ^:private context (uix/create-context))

(defn ^:private tx-fn [data event args]
(apply event-tx-fn data event args))

(defn ^:private use-dispatch-fn []
(let [publish (events/use-publish)
conn (use-context state/context)]
(use-callback
conn (uix/use-context state/context)]
(uix/use-callback
(fn [topic & args]
(apply publish topic args)
(let [tx-data [[:db.fn/call tx-fn topic args]]
Expand All @@ -23,7 +22,7 @@
(apply publish :tx/commit (list report))))) ^:lint/disable [publish])))

(defn use-dispatch []
(use-context context))
(uix/use-context context))

(defui provider [{:keys [children]}]
(let [dispatch (use-dispatch-fn)]
Expand Down
17 changes: 9 additions & 8 deletions src/main/ogres/app/provider/events.cljs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
(ns ogres.app.provider.events
(:require [uix.core :refer [$ defui create-context use-callback use-context use-effect]]))
(:require [uix.core :as uix :refer [defui $]]))

(def ^:private context (create-context (js/EventTarget.)))
(def ^:private context (uix/create-context (js/EventTarget.)))

(defn use-subscribe [topic f]
(let [target (use-context context)
(let [target (uix/use-context context)
topic (str topic)]
(use-effect
(uix/use-effect
(fn []
(let [handler (fn [event] (apply f (.-detail event)))]
(.addEventListener target topic handler)
(fn []
(.removeEventListener target topic handler)))) [target topic f])))

(defn use-publish []
(let [target (use-context context)]
(use-callback
(let [target (uix/use-context context)]
(uix/use-callback
(fn [topic & args]
(.dispatchEvent target (js/CustomEvent. (str topic) #js {"detail" args}))) [target])))

(defui provider [props]
($ context {:value (js/EventTarget.)}
(:children props)))
(let [[target] (uix/use-state (js/EventTarget.))]
($ context {:value target}
(:children props))))
4 changes: 2 additions & 2 deletions src/main/ogres/app/provider/idb.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[shadow.cljs.modern :refer (js-await)]
["@msgpack/msgpack" :as MessagePack]))

(def ^:private context (uix/create-context (js/Promise.resolve)))
(def ^:private context (uix/create-context))

(defn ^:private download [data filename]
(let [anchor (js/document.createElement "a")
Expand Down Expand Up @@ -155,7 +155,7 @@
"Provides a React context whose value is a Promise which resolves
with an instance of IDBDatabase, an IndexedDB database connection."
[props]
(let [req (initialize "ogres.app" 10)]
(let [[req] (uix/use-state (initialize "ogres.app" 10))]
($ context {:value req}
($ listeners)
(:children props))))
Expand Down
7 changes: 3 additions & 4 deletions src/main/ogres/app/provider/image.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

(def ^:private hash-fn "SHA-1")

(def ^:private image
(create-context {{} (constantly nil)}))
(def ^:private context (create-context))

(defn ^:private decode-digest
"Returns a hash string of the given digest array."
Expand Down Expand Up @@ -143,7 +142,7 @@
(.then
(fn [[_ _ data]]
(dispatch :token-images/change-thumbnail hash data rect)))))) [read dispatch conn write]))
($ image {:value [urls on-request]}
($ context {:value [urls on-request]}
(:children props))))

(defn ^:private process-file
Expand Down Expand Up @@ -229,7 +228,7 @@
($ :img {:src url}))
```"
[hash]
(let [[urls on-request] (use-context image)]
(let [[urls on-request] (use-context context)]
(use-effect
(fn [] (on-request hash)) [on-request hash])
(get urls hash)))
14 changes: 7 additions & 7 deletions src/main/ogres/app/provider/portal.cljs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
(ns ogres.app.provider.portal
(:refer-clojure :exclude [use])
(:require [uix.core :refer [defui $ create-context use-context use-state use-callback]]
(:require [uix.core :as uix :refer [defui $]]
[uix.dom :refer [create-portal]]))

(def ^:private context (create-context))
(def ^:private context (uix/create-context))

(defui provider
"Context provider that allows the user to define portal targets and associate
them with a label; other components within the provider can then render
into these targets by label."
[{:keys [children]}]
(let [[portals set-portals] (use-state {})
register (use-callback
(let [[portals set-portals] (uix/use-state {})
register (uix/use-callback
(fn [name ref]
(set-portals
(fn [portals]
Expand All @@ -23,8 +23,8 @@
This element will be used as the target node for rendering the contents
of `use`."
[{:keys [name children]}]
(let [[_ register] (use-context context)
callback (use-callback
(let [[_ register] (uix/use-context context)
callback (uix/use-callback
(fn [node]
(register name node)) [register name])]
(children {:ref callback})))
Expand All @@ -33,7 +33,7 @@
"Renders the given children into the portal target associated with the given
label; if the label is nil, renders the contents normally."
[{:keys [name children]}]
(let [[portals _] (use-context context)
(let [[portals _] (uix/use-context context)
node (get portals name)]
(if (some? node)
(create-portal children node)
Expand Down
8 changes: 4 additions & 4 deletions src/main/ogres/app/provider/release.cljs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(ns ogres.app.provider.release
(:require [clojure.string :refer [split-lines]]
[uix.core :refer [$ defui create-context use-effect use-state]]))
[uix.core :as uix :refer [defui $]]))

(def context (create-context))
(def context (uix/create-context))

(defui provider [{:keys [children]}]
(let [[releases set-releases] (use-state nil)]
(use-effect
(let [[releases set-releases] (uix/use-state nil)]
(uix/use-effect
(fn []
(-> (js/fetch "/releases.txt")
(.then (fn [res] (.text res)))
Expand Down
4 changes: 2 additions & 2 deletions src/main/ogres/app/provider/state.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
[:db/add -4 :camera/scene -2]
[:db/add -5 :db/ident :session]]))

(def context (uix/create-context (ds/conn-from-db (initial-data :host))))
(def context (uix/create-context))

(defui ^:private listeners []
(let [write (idb/use-writer "images")]
Expand Down Expand Up @@ -122,7 +122,7 @@
"Provides a DataScript in-memory database to the application and causes
re-renders when transactions are performed."
[{:keys [children type] :or {type :host}}]
(let [conn (ds/conn-from-db (initial-data type))]
(let [[conn] (uix/use-state (ds/conn-from-db (initial-data type)))]
($ context {:value conn}
(if (or (= type :host) (= type :view))
($ persistence {:type type}))
Expand Down
30 changes: 15 additions & 15 deletions src/main/ogres/app/provider/window.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
[goog.functions :refer [throttle]]
[ogres.app.hooks :refer [use-event-listener use-subscribe use-dispatch use-query]]
[ogres.app.provider.state :as provider.state]
[uix.core :refer [defui $ create-context use-callback use-context use-memo use-state use-effect]]))
[uix.core :as uix :refer [defui $]]))

(def ^:private context (create-context))
(def ^:private context (uix/create-context))

(def ^:private reader (t/reader :json {:handlers dst/read-handlers}))
(def ^:private writer (t/writer :json {:handlers dst/write-handlers}))
Expand All @@ -23,7 +23,7 @@
"Registers a DataScript listener in order to manage the view window, the
player's view of the scene." []
(let [dispatch (use-dispatch)
{:keys [view reset]} (use-context context)]
{:keys [view reset]} (uix/use-context context)]
(use-subscribe :share/initiate
(fn []
(if (nil? view)
Expand All @@ -39,7 +39,7 @@
(defui ^:private dispatcher
"Registers a DataScript listener in order to forward transactions from the
host window to the view window." []
(let [{:keys [view]} (use-context context)]
(let [{:keys [view]} (uix/use-context context)]
(use-subscribe :tx/commit
(fn [{tx-data :tx-data}]
(->>
Expand All @@ -51,9 +51,9 @@
"Registers an event handler to listen for application state changes in the
form of serialized EDN DataScript transactions. Unmarshals and transacts
those against the local DataScript connection." []
(let [conn (use-context provider.state/context)]
(let [conn (uix/use-context provider.state/context)]
(use-event-listener "AppStateTx"
(use-callback
(uix/use-callback
(fn [event]
(->>
(.-detail event)
Expand All @@ -66,17 +66,17 @@
of the form [x y width height]."
[{:keys [target type]}]
(let [dispatch (use-dispatch)
handler (use-memo
handler (uix/use-memo
#(throttle
(fn []
(if-let [element (.. target -document (querySelector ".layout-scene"))]
(->> (.getBoundingClientRect element)
(bounds->vector)
(dispatch :bounds/change type)))) 100) [dispatch type target])
[observer] (use-state (js/ResizeObserver. handler))
[scene set-scene] (use-state nil)]
[observer] (uix/use-state (js/ResizeObserver. handler))
[scene set-scene] (uix/use-state nil)]
(use-event-listener "resize" handler)
(use-effect
(uix/use-effect
(fn []
(if (= type :host)
(.dispatchEvent target (js/Event. "resize")))
Expand All @@ -86,7 +86,7 @@
(set-scene element)
(.requestAnimationFrame js/window f)))))))

(use-effect
(uix/use-effect
(fn []
(when scene
(.observe observer scene)
Expand All @@ -96,9 +96,9 @@
"Registers event handlers to listen for the host or view windows being
closed."
[]
(let [{:keys [view reset]} (use-context context)]
(let [{:keys [view reset]} (uix/use-context context)]
(use-event-listener view "visibilitychange"
(use-callback
(uix/use-callback
(fn []
(.. js/window (setTimeout #(when (.-closed view) (reset)) 200)))
[view reset]))
Expand All @@ -109,10 +109,10 @@
"Provides a reference to the view window, if any, and registers several
event handlers needed for them."
[]
(let [[screen set-screen] (use-state nil)
(let [[screen set-screen] (uix/use-state nil)
dispatch (use-dispatch)
result (use-query [:user/type :user/ready])
on-reset (use-callback
on-reset (uix/use-callback
(fn
([] (when-let [element screen]
(.close element)
Expand Down

0 comments on commit 7b9cbb0

Please sign in to comment.