Skip to content

Instantly share code, notes, and snippets.

@nivekuil
nivekuil / gist:f3698345ac51f6adf8039b475288461a
Created October 14, 2023 18:59
clojurescript relative time
#?(:cljs (def relative-time-formatter
(js/Intl.RelativeTimeFormat. "en" #js{:numeric "auto"})))
(defn timeago [time]
#?(:clj (str time)
:cljs (let [seconds-ago (t/seconds (t/between (t/now) time))
[divide unit] (condp #(> %1 (abs %2)) seconds-ago
60 [1 "second"]
3600 [60 "minute"]
86400 [3600 "hour"]
[86460 "day"])]
(dom/div
(let [!cs (atom [0 1 2 3 4]) cs (e/watch !cs)]
(e/for [c cs]
(ui/button
(e/fn [] (reset! !cs (vec (remove #{c} cs))))
(dom/props {:class "fade-in"})
(dom/text "REMOVE " c)))
(ui/button
(e/fn [] (swap! !cs conj (inc (peek cs))))
(dom/text "ADD"))))
@nivekuil
nivekuil / flowdiff.cljc
Last active January 9, 2024 06:01
missionary flow managing effects from diffs
;; Essentially we want to make a tee, passing through diffs for rendering but also taking advantage of missionary's supervision
;; for mount/unmount effects. Thus the diffs have to have a concept of corresponding to an identity. 3rd try is the charm?
(def sender (y/observe (fn [!] (def send! !) #(prn "sender dead"))))
(defn boot [flow]((y/reduce {}nil flow) prn prn))
(def !state (atom 0))
(def flow (y/ap
(let [diff (y/?= sender)
res (y/?> (y/observe (fn[!]
@nivekuil
nivekuil / fasthash.cljs
Created January 11, 2024 13:42
fast hash types
(deftype IdAttr [id attr ^:mutable ^number _hasheq]
IEquiv
(-equiv
[this that]
(boolean
(or (identical? this that)
(and (instance? IdAttr that)
(= id (.-id ^IdAttr that))
(= attr (.-attr ^IdAttr that))))))