-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.cljs
88 lines (70 loc) · 2.94 KB
/
core.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(ns appkit.citrus.core
(:require [appkit.citrus.reconciler :as r]
[appkit.citrus.cursor :as c]))
;; copied from https://github.com/roman01la/citrus
(defn reconciler
"Creates an instance of Reconciler
(citrus/reconciler {:state (atom {})
:handler handler
:effect-handlers {:http effects/http}
:batched-updates f
:chunked-updates f})
Arguments
config - a map of
state - app state atom
handler - event handler
effect-handlers - a hash of effects handlers
batched-updates - a function used to batch reconciler updates, defaults to `js/requestAnimationFrame`
chunked-updates - a function used to divide reconciler update into chunks, doesn't used by default
Returned value supports deref, watches and metadata.
The only supported option is `:meta`"
[{:keys [state handler effect-handlers batched-updates chunked-updates debug?]} & {:as options}]
(binding []
(let [watch-fns (volatile! {})
rec (r/Reconciler.
handler
effect-handlers
state
(volatile! [])
(volatile! nil)
(or batched-updates js/requestAnimationFrame)
chunked-updates
(:meta options)
debug?
watch-fns)]
(add-watch state (list rec :watch-fns)
(fn [_ _ oldv newv]
(when (not= oldv newv)
(doseq [w @watch-fns]
(let [[k watch-fn] w]
(watch-fn k rec oldv newv))))))
rec)))
(defn dispatch!
"Invoke an event on particular controller asynchronously
(citrus/dispatch! reconciler :users :load \"id\")
Arguments
reconciler - an instance of Reconciler
event - a dispatch value of a method defined in the controller
args - arguments to be passed into the controller"
[reconciler event & args]
(r/dispatch! reconciler event args))
(defn dispatch-sync!
"Invoke an event on particular controller synchronously
(citrus/dispatch! reconciler :users :load \"id\")
Arguments
reconciler - an instance of Reconciler
event - a dispatch value of a method defined in the controller
args - arguments to be passed into the controller"
[reconciler event & args]
(r/dispatch-sync! reconciler event args))
(defn subscription
"Create a subscription to state updates
(citrus/subscription reconciler [:users 0] (juxt [:fname :lname]))
Arguments
reconciler - an instance of Reconciler
path - a vector which describes a path into reconciler's atom value
reducer - an aggregate function which computes a materialized view of data behind the path"
([reconciler path]
(subscription reconciler path identity))
([reconciler path reducer]
(c/reduce-cursor-in reconciler path reducer)))