Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nenadalm committed Jan 18, 2024
1 parent 8e07837 commit d59a983
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 79 deletions.
51 changes: 0 additions & 51 deletions src/app/autosave.cljs

This file was deleted.

19 changes: 3 additions & 16 deletions src/app/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[app.config :as config]
[app.views :as views]
[app.events :as events]
[app.autosave :as autosave]))
[nenadalm.clojure-utils.re-frame.autosave :as autosave]
[nenadalm.clojure-utils.cljs :as cljs-utils]))

(defn mount-root []
(re-frame/clear-subscription-cache!)
Expand All @@ -25,24 +26,10 @@
(when-not config/debug?
(register-worker)))

(defn- prevent-screen-lock []
(when-some [wake-lock (.-wakeLock js/navigator)]
(-> wake-lock
(.request "screen")
(.then (fn []
(js/document.addEventListener
"visibilitychange"
(fn [_]
(when (= "visible" (.-visibilityState js/document))
(-> wake-lock
(.request "screen")
(.catch (fn [_] (js/alert "Cannot prevent screen from locking.")))))))))
(.catch (fn [_] (js/alert "Cannot prevent screen from locking."))))))

(defn ^:export init []
(dev-setup)
(prod-setup)
(prevent-screen-lock)
(cljs-utils/prevent-screen-lock)
(autosave/init "nenadalm.backgammon/autosave")
(re-frame/dispatch-sync [::events/init])
(mount-root))
Expand Down
10 changes: 3 additions & 7 deletions src/app/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
[clojure.edn :as edn]
[clojure.set :as set]
[re-frame.core :as re-frame]
[app.db :as db]))
[app.db :as db]
[nenadalm.clojure-utils.cljs :as cljs-utils]))

(def ^:private conjv (fnil conj []))

(re-frame/reg-cofx
:app-version
(fn [coeffects _]
(assoc coeffects
:app-version
(or (some-> "meta[name=app-version]"
js/document.querySelector
(.getAttribute "content"))
"unknown"))))
(assoc coeffects :app-version (cljs-utils/app-version))))

(re-frame/reg-cofx
:settings
Expand Down
2 changes: 1 addition & 1 deletion src/build/hook.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[build.create-manifest]
[build.create-index]
[build.create-worker]
[build.assets :as assets]))
[nenadalm.clojure-utils.assets :as assets]))

(defn hook
{:shadow.build/stage :flush}
Expand Down
20 changes: 20 additions & 0 deletions src/nenadalm/clojure_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Clojure utils

This repo contains various utils, guides, … on writing apps using Clojure. It’s not meant to be anything universal, just some stuff I would copy-paste among projects without it + some guides/notes on how I do some things.

## App version

App version should be specified using [meta element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta).

example
```html
<meta name="app-version" content="1">
```

## App state

Should be fully serializable to support autosave feature (`nenadalm.clojure-utils.re-frame.autosave`). Without this feature, app state is lost when app is closed (can easilly happen accidentaly).

## Cache busting

Shadow cljs [build hooks](https://shadow-cljs.github.io/docs/UsersGuide.html#build-hooks) should be used to render html and other resources to make everything work smoothly. `nenadalm.clojure-utils.assets` ns helps with asset names.
17 changes: 13 additions & 4 deletions src/build/assets.clj → src/nenadalm/clojure_utils/assets.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
(ns build.assets
(ns nenadalm.clojure-utils.assets
(:require
[clojure.java.io :as io]
[build.util :as u]))
[clojure.java.io :as io]))

(defn- sha-1 [s]
(let [c (java.security.MessageDigest/getInstance "sha-1")]
(.update c (.getBytes s "utf-8"))
(reduce (fn [s b] (str s (format "%02x" b))) "" (.digest c))))

(defn- file-hash [f]
(-> f
slurp
sha-1))

(defn- path [s]
(java.nio.file.Paths/get s (make-array java.lang.String 0)))
Expand Down Expand Up @@ -49,7 +58,7 @@
(defn- hash-assets [{:keys [public-dir assets]}]
(reduce
(fn [acc [asset src]]
(let [hash (u/file-hash src)
(let [hash (file-hash src)
asset-path (hashed-asset asset hash)
target (str public-dir "/" asset-path)]
(copy src target)
Expand Down
18 changes: 18 additions & 0 deletions src/nenadalm/clojure_utils/cljs.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns nenadalm.clojure-utils.cljs)

(defn- request-screen [wake-lock]
(.request wake-lock "screen"))

(defn prevent-screen-lock []
(when-some [wake-lock (.-wakeLock js/navigator)]
(-> (request-screen wake-lock)
(.then (fn []
(js/document.addEventListener
"visibilitychange"
(fn [_]
(when (= "visible" (.-visibilityState js/document))
(request-screen wake-lock)))))))))

(defn app-version []
(some-> (js/document.querySelector "meta[name=app-version]")
(.getAttribute "content")))
34 changes: 34 additions & 0 deletions src/nenadalm/clojure_utils/re_frame/autosave.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(ns nenadalm.clojure-utils.re-frame.autosave
(:require
[re-frame.core :as re-frame]
[clojure.edn :as edn]
[nenadalm.clojure-utils.re-frame.local-storage :as ls]))

(defn init [autosave-key]
(re-frame/reg-event-fx
::autosave
(fn [{:keys [db]}]
{::ls/set {autosave-key (pr-str db)}}))

(re-frame/reg-event-fx
::autosave-remove
(fn [_]
{::ls/remove [autosave-key]}))

(re-frame/reg-event-fx
::autosave-load
[(re-frame/inject-cofx ::ls/get {:autosave-str autosave-key})]
(fn [cofx]
(if-let [autosave (edn/read-string (:autosave-str cofx))]
{:db autosave
::ls/remove [autosave-key]}
{})))

(re-frame/dispatch [::autosave-load])
(js/document.addEventListener
"visibilitychange"
(fn []
(case (.-visibilityState js/document)
"visible" (re-frame/dispatch [::autosave-remove])
"hidden" (re-frame/dispatch [::autosave])
nil))))
24 changes: 24 additions & 0 deletions src/nenadalm/clojure_utils/re_frame/local_storage.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns nenadalm.clojure-utils.re-frame.local-storage
(:require
[re-frame.core :as re-frame]))

(re-frame/reg-fx
::set
(fn [kv]
(doseq [[k v] kv]
(js/window.localStorage.setItem k v))))

(re-frame/reg-cofx
::get
(fn [coeffects kv]
(reduce-kv
(fn [m k v]
(assoc m k (js/window.localStorage.getItem v)))
coeffects
kv)))

(re-frame/reg-fx
::remove
(fn [ks]
(doseq [k ks]
(js/window.localStorage.removeItem k))))

0 comments on commit d59a983

Please sign in to comment.