/
ratom.cljs
632 lines (519 loc) · 17.2 KB
/
ratom.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
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
(ns ^{:mranderson/inlined true} re-frisk.inlined-deps.reagent.v1v0v0.reagent.ratom
(:refer-clojure :exclude [atom])
(:require-macros [re-frisk.inlined-deps.reagent.v1v0v0.reagent.ratom])
(:require [re-frisk.inlined-deps.reagent.v1v0v0.reagent.impl.util :as util]
[re-frisk.inlined-deps.reagent.v1v0v0.reagent.debug :refer-macros [warn dev?]]
[re-frisk.inlined-deps.reagent.v1v0v0.reagent.impl.batching :as batch]
[clojure.set :as s]
[goog.object :as obj]))
(set! *warn-on-infer* false)
(declare flush!)
(declare ^:dynamic *ratom-context*)
(defonce ^boolean debug false)
(defonce ^:private generation 0)
(defonce ^:private -running (clojure.core/atom 0))
(defn ^boolean reactive? []
(some? *ratom-context*))
;;; Utilities
(defn running []
(+ @-running))
(defn- ^number arr-len [x]
(if (nil? x) 0 (alength x)))
(defn- ^boolean arr-eq [x y]
(let [len (arr-len x)]
(and (== len (arr-len y))
(loop [i 0]
(or (== i len)
(if (identical? (aget x i) (aget y i))
(recur (inc i))
false))))))
(defn- in-context
"When f is executed, if (f) derefs any ratoms, they are then added to 'obj.captured'(*ratom-context*).
See function notify-deref-watcher! to know how *ratom-context* is updated"
[obj f]
(binding [*ratom-context* obj]
(f)))
(defn- deref-capture
"Returns `(in-context f r)`. Calls `_update-watching` on r with any
`deref`ed atoms captured during `in-context`, if any differ from the
`watching` field of r. Clears the `dirty?` flag on r.
Inside '_update-watching' along with adding the ratoms in 'r.watching' of reaction,
the reaction is also added to the list of watches on each ratoms f derefs."
[f ^clj r]
(set! (.-captured r) nil)
(when (dev?)
(set! (.-ratomGeneration r) (set! generation (inc generation))))
(let [res (in-context r f)
c (.-captured r)]
(set! (.-dirty? r) false)
;; Optimize common case where derefs occur in same order
(when-not (arr-eq c (.-watching r))
(._update-watching r c))
res))
(defn- notify-deref-watcher!
"Add `derefed` to the `captured` field of `*ratom-context*`.
See also `in-context`"
[derefed]
(when-some [^clj r *ratom-context*]
(let [c (.-captured r)]
(if (nil? c)
(set! (.-captured r) (array derefed))
(.push c derefed)))))
(defn- check-watches [old new]
(when debug
(swap! -running + (- (count new) (count old))))
new)
(defn- add-w [^clj this key f]
(let [w (.-watches this)]
(set! (.-watches this) (check-watches w (assoc w key f)))
(set! (.-watchesArr this) nil)))
(defn- remove-w [^clj this key]
(let [w (.-watches this)]
(set! (.-watches this) (check-watches w (dissoc w key)))
(set! (.-watchesArr this) nil)))
(defn- notify-w [^clj this old new]
(let [w (.-watchesArr this)
a (if (nil? w)
;; Copy watches to array for speed
(->> (.-watches this)
(reduce-kv #(doto %1 (.push %2) (.push %3)) #js[])
(set! (.-watchesArr this)))
w)
len (alength a)]
(loop [i 0]
(when (< i len)
(let [k (aget a i)
f (aget a (inc i))]
(f k this old new))
(recur (+ 2 i))))))
(defn- pr-atom [a writer opts s v]
(-write writer (str "#object[re-frisk.inlined-deps.reagent.v1v0v0.reagent.ratom." s " "))
(pr-writer (binding [*ratom-context* nil] v) writer opts)
(-write writer "]"))
;;; Queueing
(defonce ^:private rea-queue nil)
(defn- rea-enqueue [r]
(when (nil? rea-queue)
(set! rea-queue (array))
(batch/schedule))
(.push rea-queue r))
;;; Atom
(defprotocol IReactiveAtom)
(deftype RAtom [^:mutable state meta validator ^:mutable watches]
IAtom
IReactiveAtom
IEquiv
(-equiv [o other] (identical? o other))
IDeref
(-deref [this]
(notify-deref-watcher! this)
state)
IReset
(-reset! [a new-value]
(when-not (nil? validator)
(assert (validator new-value) "Validator rejected reference state"))
(let [old-value state]
(set! state new-value)
(when-not (nil? watches)
(notify-w a old-value new-value))
new-value))
ISwap
(-swap! [a f] (-reset! a (f state)))
(-swap! [a f x] (-reset! a (f state x)))
(-swap! [a f x y] (-reset! a (f state x y)))
(-swap! [a f x y more] (-reset! a (apply f state x y more)))
IWithMeta
(-with-meta [_ new-meta] (RAtom. state new-meta validator watches))
IMeta
(-meta [_] meta)
IPrintWithWriter
(-pr-writer [a w opts] (pr-atom a w opts "RAtom" {:val (-deref a)}))
IWatchable
(-notify-watches [this old new] (notify-w this old new))
(-add-watch [this key f] (add-w this key f))
(-remove-watch [this key] (remove-w this key))
IHash
(-hash [this] (goog/getUid this)))
(defn atom
"Like clojure.core/atom, except that it keeps track of derefs."
([x] (->RAtom x nil nil nil))
([x & {:keys [meta validator]}] (->RAtom x meta validator nil)))
;;; track
(declare make-reaction)
(defn- cached-reaction [f ^clj o k ^clj obj destroy]
(let [m (.-reagReactionCache o)
m (if (nil? m) {} m)
r (m k nil)]
(cond
(some? r) (-deref r)
(nil? *ratom-context*) (f)
:else (let [r (make-reaction
f :on-dispose (fn [x]
(when debug (swap! -running dec))
(as-> (.-reagReactionCache o) _
(dissoc _ k)
(set! (.-reagReactionCache o) _))
(when (some? obj)
(set! (.-reaction obj) nil))
(when (some? destroy)
(destroy x))))
v (-deref r)]
(set! (.-reagReactionCache o) (assoc m k r))
(when debug (swap! -running inc))
(when (some? obj)
(set! (.-reaction obj) r))
v))))
(deftype Track [f args ^:mutable reaction]
IReactiveAtom
IDeref
(-deref [this]
(if-some [r reaction]
(-deref r)
(cached-reaction #(apply f args) f args this nil)))
IEquiv
(-equiv [_ ^clj other]
(and (instance? Track other)
(= f (.-f other))
(= args (.-args other))))
IHash
(-hash [_] (hash [f args]))
IPrintWithWriter
(-pr-writer [a w opts] (pr-atom a w opts "Track" {:val (-deref a)
:f f})))
(defn make-track [f args]
(Track. f args nil))
(defn make-track! [f args]
(let [t (make-track f args)
r (make-reaction #(-deref t)
:auto-run true)]
@r
r))
(defn track [f & args]
{:pre [(ifn? f)]}
(make-track f args))
(defn track! [f & args]
{:pre [(ifn? f)]}
(make-track! f args))
;;; cursor
(deftype RCursor [ratom path ^:mutable reaction
^:mutable state ^:mutable watches]
IAtom
IReactiveAtom
IEquiv
(-equiv [_ ^clj other]
(and (instance? RCursor other)
(= path (.-path other))
(= ratom (.-ratom other))))
Object
(_peek [this]
(binding [*ratom-context* nil]
(-deref this)))
(_set-state [this oldstate newstate]
(when-not (identical? oldstate newstate)
(set! state newstate)
(when (some? watches)
(notify-w this oldstate newstate))))
IDeref
(-deref [this]
(let [oldstate state
newstate (if-some [r reaction]
(-deref r)
(let [f (if (satisfies? IDeref ratom)
#(get-in @ratom path)
#(ratom path))]
(cached-reaction f ratom path this nil)))]
(._set-state this oldstate newstate)
newstate))
IReset
(-reset! [this new-value]
(let [oldstate state]
(._set-state this oldstate new-value)
(if (satisfies? IDeref ratom)
(if (= path [])
(reset! ratom new-value)
(swap! ratom assoc-in path new-value))
(ratom path new-value))
new-value))
ISwap
(-swap! [a f] (-reset! a (f (._peek a))))
(-swap! [a f x] (-reset! a (f (._peek a) x)))
(-swap! [a f x y] (-reset! a (f (._peek a) x y)))
(-swap! [a f x y more] (-reset! a (apply f (._peek a) x y more)))
IPrintWithWriter
(-pr-writer [a w opts] (pr-atom a w opts "RCursor" {:val (-deref a)
:path path}))
IWatchable
(-notify-watches [this old new] (notify-w this old new))
(-add-watch [this key f] (add-w this key f))
(-remove-watch [this key] (remove-w this key))
IHash
(-hash [_] (hash [ratom path])))
(defn cursor
[^clj src path]
(assert (or (satisfies? IReactiveAtom src)
(and (ifn? src)
(not (vector? src))))
(str "src must be a reactive atom or a function, not "
(pr-str src)
" while attempting to get path: "
(pr-str path)))
(->RCursor src path nil nil nil))
;;; with-let support
(defn with-let-destroy [v]
(when-some [f (.-destroy v)]
(f)))
(defn with-let-values [key]
(if-some [c *ratom-context*]
(cached-reaction array c key nil with-let-destroy)
(array)))
;;;; reaction
(defprotocol IDisposable
(dispose! [this])
(add-on-dispose! [this f]))
(defprotocol IRunnable
(run [this]))
(defn- handle-reaction-change [^clj this sender old new]
(._handle-change this sender old new))
;; Fields of a Reaction javascript object
;; - auto_run
;; - captured
;; - caught
;; - f
;; - ratomGeneration
;; - state
;; - watches
;; - watching
(deftype Reaction [f ^:mutable state ^:mutable ^boolean dirty? ^boolean nocache?
^:mutable watching ^:mutable watches ^:mutable auto-run
^:mutable caught]
IAtom
IReactiveAtom
IWatchable
(-notify-watches [this old new] (notify-w this old new))
(-add-watch [this key f] (add-w this key f))
(-remove-watch [this key]
(let [was-empty (empty? watches)]
(remove-w this key)
(when (and (not was-empty)
(empty? watches)
(nil? auto-run))
(dispose! this))))
IReset
(-reset! [a newval]
(assert (fn? (.-on-set a)) "Reaction is read only; on-set is not allowed")
(let [oldval state]
(set! state newval)
(.on-set a oldval newval)
(notify-w a oldval newval)
newval))
ISwap
(-swap! [a f] (-reset! a (f (._peek-at a))))
(-swap! [a f x] (-reset! a (f (._peek-at a) x)))
(-swap! [a f x y] (-reset! a (f (._peek-at a) x y)))
(-swap! [a f x y more] (-reset! a (apply f (._peek-at a) x y more)))
Object
(_peek-at [this]
(binding [*ratom-context* nil]
(-deref this)))
(_handle-change [this sender oldval newval]
(when-not (or (identical? oldval newval)
dirty?)
(if (nil? auto-run)
(do
(set! dirty? true)
(rea-enqueue this))
(if (true? auto-run)
(._run this false)
(auto-run this)))))
(_update-watching [this derefed]
(let [new (set derefed)
old (set watching)]
(set! watching derefed)
(doseq [w (s/difference new old)]
(-add-watch w this handle-reaction-change))
(doseq [w (s/difference old new)]
(-remove-watch w this))))
(_queued-run [this]
(when (and dirty? (some? watching))
(._run this true)))
(_try-capture [this f]
(try
(set! caught nil)
(deref-capture f this)
(catch :default e
(set! state e)
(set! caught e)
(set! dirty? false))))
(_run [this check]
(let [oldstate state
res (if check
(._try-capture this f)
(deref-capture f this))]
(when-not nocache?
(set! state res)
;; Use = to determine equality from reactions, since
;; they are likely to produce new data structures.
(when-not (or (nil? watches)
(= oldstate res))
(notify-w this oldstate res)))
res))
(_set-opts [this {:keys [auto-run on-set on-dispose no-cache]}]
(when (some? auto-run)
(set! (.-auto-run this) auto-run))
(when (some? on-set)
(set! (.-on-set this) on-set))
(when (some? on-dispose)
(set! (.-on-dispose this) on-dispose))
(when (some? no-cache)
(set! (.-nocache? this) no-cache)))
IRunnable
(run [this]
(flush!)
(._run this false))
IDeref
(-deref [this]
(when-some [e caught]
(throw e))
(let [non-reactive (nil? *ratom-context*)]
(when non-reactive
(flush!))
(if (and non-reactive (nil? auto-run))
(when dirty?
(let [oldstate state]
(set! state (f))
(when-not (or (nil? watches) (= oldstate state))
(notify-w this oldstate state))))
(do
(notify-deref-watcher! this)
(when dirty?
(._run this false)))))
state)
IDisposable
(dispose! [this]
(let [s state
wg watching]
(set! watching nil)
(set! state nil)
(set! auto-run nil)
(set! dirty? true)
(doseq [w (set wg)]
(-remove-watch w this))
(when (some? (.-on-dispose this))
(.on-dispose this s))
(when-some [a (.-on-dispose-arr this)]
(dotimes [i (alength a)]
((aget a i) this)))))
(add-on-dispose! [this f]
;; f is called with the reaction as argument when it is no longer active
(if-some [a (.-on-dispose-arr this)]
(.push a f)
(set! (.-on-dispose-arr this) (array f))))
IEquiv
(-equiv [o other] (identical? o other))
IPrintWithWriter
(-pr-writer [a w opts] (pr-atom a w opts "Reaction" {:val (-deref a)}))
IHash
(-hash [this] (goog/getUid this)))
(defn flush! []
(loop []
(let [q rea-queue]
(when-not (nil? q)
(set! rea-queue nil)
(dotimes [i (alength q)]
(let [^Reaction r (aget q i)]
(._queued-run r)))
(recur)))))
(set! batch/ratom-flush flush!)
(defn make-reaction [f & {:keys [auto-run on-set on-dispose]}]
(let [reaction (->Reaction f nil true false nil nil nil nil)]
(._set-opts reaction {:auto-run auto-run
:on-set on-set
:on-dispose on-dispose})
reaction))
(def ^:private temp-reaction (make-reaction nil))
(defn run-in-reaction
"Evaluates `f` and returns the result. If `f` calls `deref` on any ratoms,
creates a new Reaction that watches those atoms and calls `run` whenever
any of those watched ratoms change. Also, the new reaction is added to
list of 'watches' of each of the ratoms. The `run` parameter is a function
that should expect one argument. It is passed `obj` when run. The `opts`
are any options accepted by a Reaction and will be set on the newly created
Reaction. Sets the newly created Reaction to the `key` on `obj`."
[f obj key run opts]
(let [r temp-reaction
res (deref-capture f r)]
(when-not (nil? (.-watching r))
(set! temp-reaction (make-reaction nil))
(._set-opts r opts)
(set! (.-f r) f)
(set! (.-auto-run r) #(run obj))
(obj/set obj key r))
res))
(defn check-derefs [f]
(let [ctx (js-obj)
res (in-context ctx f)]
[res (some? (.-captured ctx))]))
;;; wrap
(deftype Wrapper [^:mutable state callback ^:mutable ^boolean changed
^:mutable watches]
IAtom
IDeref
(-deref [this]
(when (dev?)
(when (and changed (some? *ratom-context*))
(warn "derefing stale wrap: "
(pr-str this))))
state)
IReset
(-reset! [this newval]
(let [oldval state]
(set! changed true)
(set! state newval)
(when (some? watches)
(notify-w this oldval newval))
(callback newval)
newval))
ISwap
(-swap! [a f] (-reset! a (f state)))
(-swap! [a f x] (-reset! a (f state x)))
(-swap! [a f x y] (-reset! a (f state x y)))
(-swap! [a f x y more] (-reset! a (apply f state x y more)))
IEquiv
(-equiv [this ^clj other]
(and (instance? Wrapper other)
;; If either of the wrappers have changed, equality
;; cannot be relied on.
(not changed)
(not (.-changed other))
(= state (.-state other))
(= callback (.-callback other))))
IWatchable
(-notify-watches [this old new] (notify-w this old new))
(-add-watch [this key f] (add-w this key f))
(-remove-watch [this key] (remove-w this key))
IPrintWithWriter
(-pr-writer [a w opts] (pr-atom a w opts "Wrapper" {:val (-deref a)})))
(defn make-wrapper [value callback-fn args]
(->Wrapper value
(util/make-partial-fn callback-fn args)
false nil))
#_(do
(defn ratom-perf []
(set! debug false)
(dotimes [_ 10]
(let [nite 100000
a (atom 0)
f (fn []
(quot @a 10))
mid (make-reaction f)
res (track! (fn []
;; (with-let [x 1])
;; @(track f)
(inc @mid)
))]
@res
(time (dotimes [x nite]
(swap! a inc)
(flush!)))
(dispose! res))))
(ratom-perf))