-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathcore.cljs
More file actions
1360 lines (1221 loc) · 42.3 KB
/
core.cljs
File metadata and controls
1360 lines (1221 loc) · 42.3 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns om.core
(:require-macros om.core)
(:require [cljsjs.react]
[om.dom :as dom :include-macros true]
[goog.object :as gobj]
[goog.dom :as gdom]
[goog.dom.dataset :as gdomdata])
(:import [goog.ui IdGenerator]))
(def ^{:dynamic true :private true} *parent* nil)
(def ^{:dynamic true :private true} *instrument* nil)
(def ^{:dynamic true :private true} *descriptor* nil)
(def ^{:dynamic true :private true} *state* nil)
(def ^{:dynamic true :private true} *root-key* nil)
;; =============================================================================
;; React Life Cycle Protocols
;;
;; http://facebook.github.io/react/docs/component-specs.html
(defprotocol IDisplayName
(display-name [this]))
(defprotocol IInitState
(init-state [this]))
(defprotocol IShouldUpdate
(should-update [this next-props next-state]))
(defprotocol IWillMount
(will-mount [this]))
(defprotocol IDidMount
(did-mount [this]))
(defprotocol IWillUnmount
(will-unmount [this]))
(defprotocol IWillUpdate
(will-update [this next-props next-state]))
(defprotocol IDidUpdate
(did-update [this prev-props prev-state]))
(defprotocol IWillReceiveProps
(will-receive-props [this next-props]))
(defprotocol IRender
(render [this]))
(defprotocol IRenderProps
(render-props [this props state]))
(defprotocol IRenderState
(render-state [this state]))
;; marker protocol, if set component will check equality of current
;; and render state
(defprotocol ICheckState)
;; =============================================================================
;; Om Protocols
(defprotocol IOmSwap
(-om-swap! [this cursor korks f tag]))
(defprotocol IGetState
(-get-state [this] [this ks]))
(defprotocol IGetRenderState
(-get-render-state [this] [this ks]))
(defprotocol ISetState
(-set-state! [this val render] [this ks val render]))
;; PRIVATE render queue, for components that use local state
;; and independently addressable components
(defprotocol IRenderQueue
(-get-queue [this])
(-queue-render! [this c])
(-empty-queue! [this]))
(defprotocol IValue
(-value [x]))
(extend-type default
IValue
(-value [x] x))
(defprotocol ICursor
(-path [cursor])
(-state [cursor]))
(defprotocol IToCursor
(-to-cursor [value state] [value state path]))
(defprotocol ICursorDerive
(-derive [cursor derived state path]))
(declare to-cursor)
(extend-type default
ICursorDerive
(-derive [this derived state path]
(to-cursor derived state path)))
(defn path [cursor]
(-path cursor))
(defn value [cursor]
(-value cursor))
(defn state [cursor]
(-state cursor))
(defprotocol ITransact
(-transact! [cursor korks f tag]))
;; PRIVATE
(defprotocol INotify
(-listen! [x key tx-listen])
(-unlisten! [x key])
(-notify! [x tx-data root-cursor]))
;; PRIVATE
(defprotocol IRootProperties
(-set-property! [this id p val])
(-remove-property! [this id p])
(-remove-properties! [this id])
(-get-property [this id p]))
;; PRIVATE
(defprotocol IRootKey
(-root-key [cursor]))
(defprotocol IAdapt
(-adapt [this other]))
(extend-type default
IAdapt
(-adapt [_ other]
other))
(defn adapt [x other]
(-adapt x other))
(defprotocol IOmRef
(-add-dep! [this c])
(-remove-dep! [this c])
(-refresh-deps! [this])
(-get-deps [this]))
(declare notify*)
(defn transact*
([state cursor korks f tag]
(let [old-state @state
path (into (om.core/path cursor) korks)
ret (cond
(satisfies? IOmSwap state) (-om-swap! state cursor korks f tag)
(empty? path) (swap! state f)
:else (swap! state update-in path f))]
(when-not (= ret ::defer)
(let [tx-data {:path path
:old-value (get-in old-state path)
:new-value (get-in @state path)
:old-state old-state
:new-state @state}]
(if-not (nil? tag)
(notify* cursor (assoc tx-data :tag tag))
(notify* cursor tx-data)))))))
(defn cursor? [x]
(satisfies? ICursor x))
(defn component? [x]
(aget x "isOmComponent"))
(defn ^:private children [node]
(let [c (.. node -props -children)]
(if (ifn? c)
(set! (.. node -props -children) (c node))
c)))
(defn get-props
"Given an owning Pure node return the Om props. Analogous to React
component props."
([x]
{:pre [(component? x)]}
(aget (.-props x) "__om_cursor"))
([x korks]
{:pre [(component? x)]}
(let [korks (if (sequential? korks) korks [korks])]
(cond-> (aget (.-props x) "__om_cursor")
(seq korks) (get-in korks)))))
(defn get-state
"Returns the component local state of an owning component. owner is
the component. An optional key or sequence of keys may be given to
extract a specific value. Always returns pending state."
([owner]
{:pre [(component? owner)]}
(-get-state owner))
([owner korks]
{:pre [(component? owner)]}
(let [ks (if (sequential? korks) korks [korks])]
(-get-state owner ks))))
(defn get-shared
"Takes an owner and returns a map of global shared values for a
render loop. An optional key or sequence of keys may be given to
extract a specific value."
([owner]
(when-not (nil? owner)
(aget (.-props owner) "__om_shared")))
([owner korks]
(cond
(not (sequential? korks))
(get (get-shared owner) korks)
(empty? korks)
(get-shared owner)
:else
(get-in (get-shared owner) korks))))
(defn ^:private merge-pending-state [owner]
(let [state (.-state owner)]
(when-let [pending-state (aget state "__om_pending_state")]
(doto state
(aset "__om_prev_state" (aget state "__om_state"))
(aset "__om_state" pending-state)
(aset "__om_pending_state" nil)))))
(defn ^:private merge-props-state
([owner] (merge-props-state owner nil))
([owner props]
(let [props (or props (.-props owner))]
(when-let [props-state (aget props "__om_state")]
(let [state (.-state owner)]
(aset state "__om_pending_state"
(merge (or (aget state "__om_pending_state")
(aget state "__om_state"))
props-state))
(aset props "__om_state" nil))))))
(defn ref-changed? [ref]
(let [val (value ref)
val' (get-in @(state ref) (path ref) ::not-found)]
(not= val val')))
(defn update-refs [c]
(let [cstate (.-state c)
refs (aget cstate "__om_refs")]
(when-not (zero? (count refs))
(aset cstate "__om_refs"
(into #{}
(filter nil?
(map
(fn [ref]
(let [ref-val (value ref)
ref-state (state ref)
ref-path (path ref)
ref-val' (get-in @ref-state ref-path ::not-found)]
(when (not= ref-val ::not-found)
(if (not= ref-val ref-val')
(adapt ref (to-cursor ref-val' ref-state ref-path))
ref))))
refs)))))))
(declare unobserve)
(def pure-methods
{:isOmComponent true
:getDisplayName
(fn []
(this-as this
(let [c (children this)]
(when (satisfies? IDisplayName c)
(display-name c)))))
:getInitialState
(fn []
(this-as this
(let [c (children this)
props (.-props this)
istate (or (aget props "__om_init_state") {})
id (::id istate)
ret #js {:__om_id (or id (.getNextUniqueId (.getInstance IdGenerator)))
:__om_state
(merge
(when (satisfies? IInitState c)
(init-state c))
(dissoc istate ::id))}]
(aset props "__om_init_state" nil)
ret)))
:shouldComponentUpdate
(fn [next-props next-state]
(this-as this
(let [props (.-props this)
state (.-state this)
c (children this)]
;; need to merge in props state first
(merge-props-state this next-props)
(if (satisfies? IShouldUpdate c)
(should-update c
(get-props #js {:props next-props :isOmComponent true})
(-get-state this))
(let [cursor (aget props "__om_cursor")
next-cursor (aget next-props "__om_cursor")]
(cond
(not= (-value cursor) (-value next-cursor))
true
(and (cursor? cursor) (cursor? next-cursor)
(not= (-path cursor) (-path next-cursor )))
true
(not= (-get-state this) (-get-render-state this))
true
(and (not (zero? (count (aget state "__om_refs"))))
(some #(ref-changed? %) (aget state "__om_refs")))
true
(not (== (aget props "__om_index") (aget next-props "__om_index")))
true
:else false))))))
:componentWillMount
(fn []
(this-as this
(merge-props-state this)
(let [c (children this)]
(when (satisfies? IWillMount c)
(will-mount c)))
(merge-pending-state this)))
:componentDidMount
(fn []
(this-as this
(let [c (children this)
cursor (aget (.-props this) "__om_cursor")]
(when (satisfies? IDidMount c)
(did-mount c)))))
:componentWillUnmount
(fn []
(this-as this
(let [c (children this)
cursor (aget (.-props this) "__om_cursor")]
(when (satisfies? IWillUnmount c)
(will-unmount c))
(when-let [refs (seq (aget (.-state this) "__om_refs"))]
(doseq [ref refs]
(unobserve this ref))))))
:componentWillUpdate
(fn [next-props next-state]
(this-as this
(let [c (children this)]
(when (satisfies? IWillUpdate c)
(let [state (.-state this)]
(will-update c
(get-props #js {:props next-props :isOmComponent true})
(-get-state this)))))
(merge-pending-state this)
(update-refs this)))
:componentDidUpdate
(fn [prev-props prev-state]
(this-as this
(let [c (children this)]
(when (satisfies? IDidUpdate c)
(let [state (.-state this)]
(did-update c
(get-props #js {:props prev-props :isOmComponent true})
(or (aget state "__om_prev_state")
(aget state "__om_state")))))
(aset (.-state this) "__om_prev_state" nil))))
:componentWillReceiveProps
(fn [next-props]
(this-as this
(let [c (children this)]
(when (satisfies? IWillReceiveProps c)
(will-receive-props c
(get-props #js {:props next-props :isOmComponent true}))))))
:render
(fn []
(this-as this
(let [c (children this)
props (.-props this)]
(binding [*parent* this
*state* (aget props "__om_app_state")
*instrument* (aget props "__om_instrument")
*descriptor* (aget props "__om_descriptor")
*root-key* (aget props "__om_root_key")]
(cond
(satisfies? IRender c)
(render c)
(satisfies? IRenderProps c)
(render-props c (aget props "__om_cursor") (get-state this))
(satisfies? IRenderState c)
(render-state c (get-state this))
:else c)))))})
(defn specify-state-methods! [obj]
(specify! obj
ISetState
(-set-state!
([this val render]
(let [props (.-props this)
app-state (aget props "__om_app_state")]
(aset (.-state this) "__om_pending_state" val)
(when (and (not (nil? app-state)) render)
(-queue-render! app-state this))))
([this ks val render]
(let [props (.-props this)
state (.-state this)
pstate (-get-state this)
app-state (aget props "__om_app_state")]
(aset state "__om_pending_state" (assoc-in pstate ks val))
(when (and (not (nil? app-state)) render)
(-queue-render! app-state this)))))
IGetRenderState
(-get-render-state
([this]
(aget (.-state this) "__om_state"))
([this ks]
(get-in (-get-render-state this) ks)))
IGetState
(-get-state
([this]
(let [state (.-state this)]
(or (aget state "__om_pending_state")
(aget state "__om_state"))))
([this ks]
(get-in (-get-state this) ks)))))
(def pure-descriptor
(specify-state-methods! (clj->js pure-methods)))
;; =============================================================================
;; EXPERIMENTAL: No Local State
(declare get-node)
(defn react-id [x]
(let [id (gdomdata/get (get-node x) "reactid")]
(assert id)
id))
(defn get-gstate [owner]
(aget (.-props owner) "__om_app_state"))
(defn no-local-merge-pending-state [owner]
(let [gstate (get-gstate owner)
spath [:state-map (react-id owner)]
states (get-in @gstate spath)]
(when (:pending-state states)
(swap! gstate update-in spath
(fn [states]
(-> states
(assoc :previous-state (:render-state states))
(assoc :render-state
(merge (:render-state states) (:pending-state states)))
(dissoc :pending-state)))))))
(declare mounted?)
(def no-local-state-methods
(assoc pure-methods
:getInitialState
(fn []
(this-as this
(let [c (children this)
props (.-props this)
istate (or (aget props "__om_init_state") {})
om-id (or (:om.core/id istate)
(.getNextUniqueId (.getInstance IdGenerator)))
state (merge (dissoc istate ::id)
(when (satisfies? IInitState c)
(init-state c)))]
(aset props "__om_init_state" nil)
#js {:__om_id om-id})))
:componentDidMount
(fn []
(this-as this
(let [c (children this)
cursor (aget (.-props this) "__om_cursor")
spath [:state-map (react-id this) :render-state]]
(swap! (get-gstate this) assoc-in spath state)
(when (satisfies? IDidMount c)
(did-mount c)))))
:componentWillMount
(fn []
(this-as this
(merge-props-state this)
(let [c (children this)]
(when (satisfies? IWillMount c)
(will-mount c)))
;; TODO: cannot merge state until mounted?
(when (mounted? this)
(no-local-merge-pending-state this))))
:componentWillUnmount
(fn []
(this-as this
(let [c (children this)]
(when (satisfies? IWillUnmount c)
(will-unmount c))
(swap! (get-gstate this) update-in [:state-map] dissoc (react-id this))
(when-let [refs (seq (aget (.-state this) "__om_refs"))]
(doseq [ref refs]
(unobserve this ref))))))
:componentWillUpdate
(fn [next-props next-state]
(this-as this
(let [props (.-props this)
c (children this)]
(when (satisfies? IWillUpdate c)
(let [state (.-state this)]
(will-update c
(get-props #js {:props next-props :isOmComponent true})
(-get-state this)))))
(no-local-merge-pending-state this)
(update-refs this)))
:componentDidUpdate
(fn [prev-props prev-state]
(this-as this
(let [c (children this)
gstate (get-gstate this)
states (get-in @gstate [:state-map (react-id this)])
spath [:state-map (react-id this)]]
(when (satisfies? IDidUpdate c)
(let [state (.-state this)]
(did-update c
(get-props #js {:props prev-props :isOmComponent true})
(or (:previous-state states)
(:render-state states)))))
(when (:previous-state states)
(swap! gstate update-in spath dissoc :previous-state)))))))
(defn no-local-descriptor [methods]
(specify! (clj->js methods)
ISetState
(-set-state!
([this val render]
(let [gstate (get-gstate this)
spath [:state-map (react-id this) :pending-state]]
(swap! (get-gstate this) assoc-in spath val)
(when (and (not (nil? gstate)) render)
(-queue-render! gstate this))))
([this ks val render]
(-set-state! this (assoc-in (-get-state this) ks val) render)))
IGetRenderState
(-get-render-state
([this]
(let [spath [:state-map (react-id this) :render-state]]
(get-in @(get-gstate this) spath)))
([this ks]
(get-in (-get-render-state this) ks)))
IGetState
(-get-state
([this]
(if (mounted? this)
(let [spath [:state-map (react-id this)]
states (get-in @(get-gstate this) spath)]
(or (:pending-state states)
(:render-state states)))
;; TODO: means state cannot be written to until mounted - David
(aget (.-props this) "__om_init_state")))
([this ks]
(get-in (-get-state this) ks)))))
;; =============================================================================
;; Cursors
(defn valid? [x]
(if (satisfies? ICursor x)
(not (keyword-identical? @x ::invalid))
true))
(deftype MapCursor [value state path]
IWithMeta
(-with-meta [_ new-meta]
(MapCursor. (with-meta value new-meta) state path))
IMeta
(-meta [_] (meta value))
IDeref
(-deref [this]
(get-in @state path ::invalid))
IValue
(-value [_] value)
ICursor
(-path [_] path)
(-state [_] state)
ITransact
(-transact! [this korks f tag]
(transact* state this korks f tag))
ICloneable
(-clone [_]
(MapCursor. value state path))
ICounted
(-count [_]
(-count value))
ICollection
(-conj [_ o]
(MapCursor. (-conj value o) state path))
;; EXPERIMENTAL
IEmptyableCollection
(-empty [_]
(MapCursor. (empty value) state path))
ILookup
(-lookup [this k]
(-lookup this k nil))
(-lookup [this k not-found]
(let [v (-lookup value k ::not-found)]
(if-not (= v ::not-found)
(-derive this v state (conj path k))
not-found)))
IFn
(-invoke [this k]
(-lookup this k))
(-invoke [this k not-found]
(-lookup this k not-found))
ISeqable
(-seq [this]
(when (pos? (count value))
(map (fn [[k v]] [k (-derive this v state (conj path k))]) value)))
IAssociative
(-contains-key? [_ k]
(-contains-key? value k))
(-assoc [_ k v]
(MapCursor. (-assoc value k v) state path))
IMap
(-dissoc [_ k]
(MapCursor. (-dissoc value k) state path))
IEquiv
(-equiv [_ other]
(if (cursor? other)
(= value (-value other))
(= value other)))
IHash
(-hash [_]
(hash value))
IKVReduce
(-kv-reduce [_ f init]
(-kv-reduce value f init))
IPrintWithWriter
(-pr-writer [_ writer opts]
(-pr-writer value writer opts)))
(deftype IndexedCursor [value state path]
ISequential
IDeref
(-deref [this]
(get-in @state path ::invalid))
IWithMeta
(-with-meta [_ new-meta]
(IndexedCursor. (with-meta value new-meta) state path))
IMeta
(-meta [_] (meta value))
IValue
(-value [_] value)
ICursor
(-path [_] path)
(-state [_] state)
ITransact
(-transact! [this korks f tag]
(transact* state this korks f tag))
ICloneable
(-clone [_]
(IndexedCursor. value state path))
ICounted
(-count [_]
(-count value))
ICollection
(-conj [_ o]
(IndexedCursor. (-conj value o) state path))
;; EXPERIMENTAL
IEmptyableCollection
(-empty [_]
(IndexedCursor. (empty value) state path))
ILookup
(-lookup [this n]
(-nth this n nil))
(-lookup [this n not-found]
(-nth this n not-found))
IFn
(-invoke [this k]
(-lookup this k))
(-invoke [this k not-found]
(-lookup this k not-found))
IIndexed
(-nth [this n]
(-derive this (-nth value n) state (conj path n)))
(-nth [this n not-found]
(if (< n (-count value))
(-derive this (-nth value n not-found) state (conj path n))
not-found))
ISeqable
(-seq [this]
(when (pos? (count value))
(map (fn [v i] (-derive this v state (conj path i))) value (range))))
IAssociative
(-contains-key? [_ k]
(-contains-key? value k))
(-assoc [this n v]
(-derive this (-assoc-n value n v) state path))
IStack
(-peek [this]
(-derive this (-peek value) state path))
(-pop [this]
(-derive this (-pop value) state path))
IEquiv
(-equiv [_ other]
(if (cursor? other)
(= value (-value other))
(= value other)))
IHash
(-hash [_]
(hash value))
IKVReduce
(-kv-reduce [_ f init]
(-kv-reduce value f init))
IPrintWithWriter
(-pr-writer [_ writer opts]
(-pr-writer value writer opts)))
(defn ^:private to-cursor* [val state path]
(specify val
IDeref
(-deref [this]
(get-in @state path ::invalid))
ICursor
(-path [_] path)
(-state [_] state)
ITransact
(-transact! [this korks f tag]
(transact* state this korks f tag))
IEquiv
(-equiv [_ other]
(if (cursor? other)
(= val (-value other))
(= val other)))))
(defn ^:private to-cursor
([val] (to-cursor val nil []))
([val state] (to-cursor val state []))
([val state path]
(cond
(cursor? val) val
(satisfies? IToCursor val) (-to-cursor val state path)
(indexed? val) (IndexedCursor. val state path)
(map? val) (MapCursor. val state path)
(satisfies? ICloneable val) (to-cursor* val state path)
:else val)))
(defn notify* [cursor tx-data]
(let [state (-state cursor)]
(-notify! state tx-data (to-cursor @state state))))
;; =============================================================================
;; Ref Cursors
(declare commit! id refresh-props!)
(defn root-cursor
"Given an application state atom return a root cursor for it."
[atom]
{:pre [(satisfies? IDeref atom)]}
(to-cursor @atom atom []))
(def _refs (atom {}))
(defn ref-sub-cursor [x parent]
(specify x
ICloneable
(-clone [this]
(ref-sub-cursor (clone x) parent))
IAdapt
(-adapt [this other]
(ref-sub-cursor (adapt x other) parent))
ICursorDerive
(-derive [this derived state path]
(let [cursor' (to-cursor derived state path)]
(if (cursor? cursor')
(adapt this cursor')
cursor')))
ITransact
(-transact! [cursor korks f tag]
(commit! cursor korks f)
(-refresh-deps! parent))))
(defn ref-cursor? [x]
(satisfies? IOmRef x))
(defn ref-cursor
"Given a cursor return a reference cursor that inherits all of the
properties and methods of the cursor. Reference cursors may be
observed via om.core/observe."
[cursor]
{:pre [(cursor? cursor)]}
(if (ref-cursor? cursor)
cursor
(let [path (path cursor)
storage (get
(swap! _refs update-in
[path] (fnil identity (atom {})))
path)]
(specify cursor
ICursorDerive
(-derive [this derived state path]
(let [cursor' (to-cursor derived state path)]
(if (cursor? cursor')
(ref-sub-cursor cursor' this)
cursor')))
IOmRef
(-add-dep! [_ c]
(swap! storage assoc (id c) c))
(-remove-dep! [_ c]
(let [m (swap! storage dissoc (id c))]
(when (zero? (count m))
(swap! _refs dissoc path))))
(-refresh-deps! [_]
(doseq [c (vals @storage)]
(-queue-render! (state cursor) c)))
(-get-deps [_]
@storage)
ITransact
(-transact! [cursor korks f tag]
(commit! cursor korks f)
(-refresh-deps! cursor))))))
(defn add-ref-to-component! [c ref]
(let [state (.-state c)
refs (or (aget state "__om_refs") #{})]
(when-not (contains? refs ref)
(aset state "__om_refs" (conj refs ref)))))
(defn remove-ref-from-component! [c ref]
(let [state (.-state c)
refs (aget state "__om_refs")]
(when (contains? refs ref)
(aset state "__om_refs" (disj refs ref)))))
(defn observe
"Given a component and a reference cursor have the component observe
the reference cursor for any data changes."
[c ref]
{:pre [(component? c) (cursor? ref) (ref-cursor? ref)]}
(add-ref-to-component! c ref)
(-add-dep! ref c)
ref)
(defn unobserve [c ref]
(remove-ref-from-component! c ref)
(-remove-dep! ref c)
ref)
;; =============================================================================
;; API
(def ^:private refresh-queued false)
(def ^:private refresh-set (atom #{}))
(defn ^:private get-renderT [state]
(or (.-om$render$T state) 0))
(defn render-all
"Force a render of *all* roots. Usage of this function is almost
never recommended."
([] (render-all nil))
([state]
(set! refresh-queued false)
(doseq [f @refresh-set] (f))
(when-not (nil? state)
(set! (.-om$render$T state) (inc (get-renderT state))))))
(def ^:private roots (atom {}))
(defn ^:private valid-component? [x f]
(assert
(or (satisfies? IRender x)
(satisfies? IRenderProps x)
(satisfies? IRenderState x))
(str "Invalid Om component fn, " (.-name f)
" does not return valid instance")))
(defn ^:private valid-opts? [m]
(every? #{:key :react-key :key-fn :fn :init-state :state
:opts :shared ::index :instrument :descriptor}
(keys m)))
(defn id [owner]
(aget (.-state owner) "__om_id"))
(defn get-descriptor
([f] (get-descriptor f nil))
([f descriptor]
(let [rdesc (or descriptor *descriptor* pure-descriptor)]
(when (or (nil? (gobj/get f "om$descriptor"))
(not (identical? rdesc (gobj/get f "om$tag"))))
(let [factory (js/React.createFactory (js/React.createClass rdesc))]
(gobj/set f "om$descriptor" factory)
(gobj/set f "om$tag" rdesc))))
(gobj/get f "om$descriptor")))
(defn getf
([f cursor]
(if (instance? MultiFn f)
(let [dv ((.-dispatch-fn f) cursor nil)]
(get-method f dv))
f))
([f cursor opts]
(if (instance? MultiFn f)
(let [dv ((.-dispatch-fn f) cursor nil opts)]
(get-method f dv))
f)))
(defn build*
([f cursor] (build* f cursor nil))
([f cursor m]
{:pre [(ifn? f) (or (nil? m) (map? m))]}
(assert (valid-opts? m)
(apply str "build options contains invalid keys, only :key, :key-fn :react-key, "
":fn, :init-state, :state, and :opts allowed, given "
(interpose ", " (keys m))))
(cond
(nil? m)
(let [shared (get-shared *parent*)
ctor (get-descriptor (getf f cursor))]
(ctor #js {:__om_cursor cursor
:__om_shared shared
:__om_root_key *root-key*
:__om_app_state *state*
:__om_descriptor *descriptor*
:__om_instrument *instrument*
:children
(fn [this]
(let [ret (f cursor this)]
(valid-component? ret f)
ret))}))
:else
(let [{:keys [key key-fn state init-state opts]} m
dataf (get m :fn)
cursor' (if-not (nil? dataf)
(if-let [i (::index m)]
(dataf cursor i)
(dataf cursor))
cursor)
rkey (cond
(not (nil? key)) (get cursor' key)
(not (nil? key-fn)) (key-fn cursor')
:else (get m :react-key))
shared (or (:shared m) (get-shared *parent*))
ctor (get-descriptor (getf f cursor' opts) (:descriptor m))]
(ctor #js {:__om_cursor cursor'
:__om_index (::index m)
:__om_init_state init-state
:__om_state state
:__om_shared shared
:__om_root_key *root-key*
:__om_app_state *state*
:__om_descriptor *descriptor*
:__om_instrument *instrument*
:key (or rkey js/undefined) ;; annoying
:children
(if (nil? opts)
(fn [this]
(let [ret (f cursor' this)]
(valid-component? ret f)
ret))
(fn [this]
(let [ret (f cursor' this opts)]
(valid-component? ret f)
ret)))})))))
(defn build
"Builds an Om component. Takes an IRender/IRenderState instance
returning function f, a value, and an optional third argument
which may be a map of build specifications.
f - is a function of 2 or 3 arguments. The first argument can be
any value and the second argument will be the owning pure node.
If a map of options m is passed in this will be the third
argument. f must return at a minimum an IRender or IRenderState
instance, this instance may implement other React life cycle
protocols.
x - any value
m - a map the following keys are allowed:
:key - a keyword that should be used to look up the key used by
React itself when rendering sequential things.
:key-fn - a function that should be used to look up the key used by
React itself when rendering sequential things.
:react-key - an explicit react key