-
Notifications
You must be signed in to change notification settings - Fork 4
/
reacl.cljs
48 lines (41 loc) · 1.72 KB
/
reacl.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(ns reacl-c.main.reacl
"Functions for using Reacl-c within a Reacl application or library."
(:require [reacl-c.main.react :as main-react]
[reacl-c.core :as core]
[reacl-c.impl.reacl0 :as reacl0]
[active.clojure.functions :as f]
[reacl2.core :as reacl :include-macros true]))
(let [set-state (fn [this state callback]
(reacl0/send-message! this [::set-state state] callback))
handle-action (fn [this action callback]
(reacl0/send-message! this [::handle-action action] callback))]
(reacl/defclass ^:private runner this state [item]
refs [self]
render
(-> (main-react/embed item
{:state state
:set-state! (f/partial set-state this)
:handle-action! (f/partial handle-action this)})
(reacl/refer self))
handle-message
(fn [msg]
(cond
(and (vector? msg) (= ::set-state (first msg)))
(reacl/return :app-state (second msg))
(and (vector? msg) (= ::handle-action (first msg)))
(reacl/return :action (second msg))
:else
(let [comp (reacl/get-dom self)]
(main-react/send-message! comp msg) ;; TODO: callback?
(reacl/return))))))
(defn embed
"Returns a Reacl component embedding the given item with the given
Reacl `state binding`.
Messages sent to the returned component are passed to the given
item. Actions and effects emitted from the item are emitted from the
returned component. To have effects being executed implicitly,
use [[reacl-c.main/execute-effects]]."
([item]
(runner (reacl/use-app-state nil) item))
([binding item]
(runner binding item)))