-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
116 additions
and
79 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |