Skip to content

Instantly share code, notes, and snippets.

@nivekuil
Created September 4, 2023 19:02
Show Gist options
  • Save nivekuil/5518e363785763f1c1bbf4d514594a0e to your computer and use it in GitHub Desktop.
Save nivekuil/5518e363785763f1c1bbf4d514594a0e to your computer and use it in GitHub Desktop.
electric odoyle
(ns app.todo-list
(:require contrib.str
[hyperfiddle.electric :as e]
[hyperfiddle.electric-dom2 :as dom]
[hyperfiddle.electric-ui4 :as ui]
[odoyle.rules :as o]
[missionary.core :as y])
#?(:clj (:import [clojure.lang IFn IDeref])))
(defn rule->subject [*s whats then]
(fn [!]
(! nil)
(let [rule-name (keyword (str (rand)))]
(swap! *s o/add-rule
(o/->rule rule-name
{:what whats
:then (fn [s match]
(! (then s rule-name)))}))
(fn [] (swap! *s o/remove-rule rule-name)))))
(def *s (atom (o/->session)))
(defn q [whats]
(y/observe (rule->subject *s whats o/query-all)))
(defn q! [x]
(swap! *s (fn [s] (o/fire-rules (reduce-kv (fn [m k v] (o/insert m k v)) s x)))))
(e/defn Todo-list []
(e/client
(e/for [{:keys [id text editing?]} (new (q '[[id ::node-type :problem]
[id ::node-text text]
[id ::editing? editing?]]))]
(dom/div (if editing?
(ui/input text (e/fn [x] (q! {id {::node-text x}})))
text)))
(ui/button
(e/fn [] (q! {(rand-int 9999999999)
{::node-type :problem
::node-text "text"
::editing? true}}))
(dom/text "click"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment