Skip to content

Commit 513505f

Browse files
committed
Add reinit!, to reset a form to its initial state
1 parent db65508 commit 513505f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
## Added
44

5-
## Fixed
6-
7-
## Changed
5+
- add `reinit!` (since `reset!` was taken), to reset a form to its initial state
86

97
# 0.1.4 (2023-09-06 / 85cc0b6)
108

119
## Added
1210

13-
- `with-form` / `deform`
11+
- `with-form` / `deform`

src/lambdaisland/morf.cljc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@
1818
formdef)]
1919
[@p form]))
2020

21+
(defprotocol IMorf
22+
(reinit! [r]))
23+
2124
(defn- compute-bindings [binding form placeholders init]
2225
(let [lookup (gensym "lookup")]
2326
(conj (into []
2427
(mapcat (fn [p]
25-
[p `(reagent.core/atom ~(get init p))])
28+
[p `(let [init# ~(get init p)]
29+
(cljs.core/specify! (reagent.core/atom init#)
30+
IMorf
31+
(~'reinit! [this#] (reset! this# init#))))])
2632
placeholders))
33+
2734
lookup
2835
(zipmap
2936
(map (comp keyword str) placeholders)
@@ -32,10 +39,13 @@
3239
binding
3340
`(cljs.core/specify! (reagent.core/reaction ~form)
3441
cljs.core/ILookup
35-
3642
(~'-lookup
3743
([_# k#] (~'-lookup ~lookup k#))
38-
([_# k# not-found#] (~'-lookup ~lookup k# not-found#)))))))
44+
([_# k# not-found#] (~'-lookup ~lookup k# not-found#)))
45+
IMorf
46+
(~'reinit! [_#]
47+
(doseq [p# ~(into [] placeholders)]
48+
(reinit! p#)))))))
3949

4050
#?(:clj
4151
(defmacro with-form [[binding formdef & {:keys [init]}] & body]
@@ -49,5 +59,4 @@
4959
(let [[placeholders form] (find-placeholders formdef)]
5060
`(do
5161
~@(for [[bind form] (partition 2 (compute-bindings binding form placeholders init))]
52-
`(def ~bind ~form))
53-
))))
62+
`(def ~bind ~form))))))

0 commit comments

Comments
 (0)