This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.cljs
115 lines (106 loc) · 4.37 KB
/
web.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(ns lww-element-set.web
(:require [reagent.core :as reagent :refer [atom cursor]]
[lww-element-set.core :as core]
[cljs-time.coerce :as tc]
[cljs-time.format :as tf]))
(def formatter (tf/formatter "HH:mm:ss.SSS"))
(defn format-time [t]
(tf/unparse formatter (tc/from-long t)))
(enable-console-print!)
(defn merge-replicas [{:keys [replica1 replica2 replica3] :as state}]
(assoc state :replica-merged (core/merge-replicas replica1 replica2 replica3)))
(def app-state (atom (let [replica1 (-> (core/make-replica)
(core/add "1")
(core/del "1")
(core/add "2"))
replica2 (-> (core/make-replica)
(core/add "1")
(core/del "1")
(core/add "2"))
replica3 (-> (core/make-replica)
(core/add "1")
(core/del "1")
(core/add "2"))]
(-> {:replica1 replica1
:replica2 replica2
:replica3 replica3}
merge-replicas))))
(defn replica-view []
(let [input-value (atom "")]
(fn [replica modifiable]
(let [dels (->> @replica
:del-set
(map (fn [[e t]]
(let [t (format-time t)]
(str t ": deleted '" e "'")))))
adds (->> @replica
:add-set
(map (fn [[e t]]
(let [t (format-time t)]
(str t ": added '" e "'")))))
entries (concat dels adds)]
[:td
[:div {:style {:margin :auto
:display :block
:max-width "200px"}}
[:div "log:"]
(for [entry (sort entries)]
^{:key entry}
[:div
entry])
[:div "elements:"]
(for [element (core/members @replica)]
^{:key element}
[:div element " "])
[:input {:type :text
:value @input-value
:on-change (fn [event]
(reset! input-value (.. event -target -value)))}]
[:div "controls:"]
(when modifiable
[:button {:on-click (fn [_]
(when (seq @input-value)
(swap! replica core/add @input-value)
(swap! app-state merge-replicas)))}
"add"])
(when modifiable
[:button {:on-click (fn [_]
(when (seq @input-value)
(swap! replica core/del @input-value)
(swap! app-state merge-replicas)))}
"del"])
[:button {:on-click (fn [_]
(js/alert (str "Element "
@input-value
(if (core/member? @input-value @replica)
" exist"
" does not exist"))))}
"query"]]]))))
(defn hello-world []
[:div
[:h3 {:style {:text-align :center}}
"These are records from three replicas of lww-element-set distributed datastructure"]
[:table {:style {:width "100%"}}
[:tr
[:th "Replica One"]
[:th "Replica Two"]
[:th "Replica Three"]]
[:tr
[replica-view (cursor app-state [:replica1]) true]
[replica-view (cursor app-state [:replica2]) true]
[replica-view (cursor app-state [:replica3]) true]]
[:tr
[:th]
[:th "Merged replica"]
[:th]]
[:tr
[:td]
[replica-view (cursor app-state [:replica-merged]) false]
[:td]]]])
(reagent/render-component [hello-world]
(. js/document (getElementById "app")))
(defn on-js-reload []
;; optionally touch your app-state to force rerendering depending on
;; your application
;; (swap! app-state update-in [:__figwheel_counter] inc)
)