/
core.cljc
1209 lines (926 loc) · 44.1 KB
/
core.cljc
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 re-frame.core
(:require
[re-frame.events :as events]
[re-frame.subs :as subs]
[re-frame.interop :as interop]
[re-frame.db :as db]
[re-frame.fx :as fx]
[re-frame.cofx :as cofx]
[re-frame.router :as router]
[re-frame.settings :as settings]
[re-frame.loggers :as loggers]
[re-frame.registrar :as registrar]
[re-frame.interceptor :as interceptor]
[re-frame.std-interceptors :as std-interceptors :refer [db-handler->interceptor
fx-handler->interceptor
ctx-handler->interceptor]]
[re-frame.utils :as utils]
[clojure.set :as set]))
;; -- dispatch ----------------------------------------------------------------
(defn dispatch
"Queue `event` for processing (handling).
`event` is a vector and the first element is typically a keyword
which identifies the kind of event.
The event will be added to a FIFO processing queue, so event
handling does not happen immediately. It will happen 'very soon'
but not now. And if the queue already contains events, they
will be processed first.
Usage:
#!clj
(dispatch [:order \"pizza\" {:supreme 2 :meatlovers 1 :veg 1}])
"
{:api-docs/heading "Dispatching Events"}
[event]
(router/dispatch event))
(defn dispatch-sync
"Synchronously (immediately) process `event`. It does **not** queue
the event for handling later as `dispatch` does.
`event` is a vector and the first element is typically a keyword
which identifies the kind of event.
It is an error to use `dispatch-sync` within an event handler because
you can't immediately process an new event when one is already
part way through being processed.
Generally, avoid using this function, and instead, use `dispatch`.
Only use it in the narrow set of cases where any delay in
processing is a problem:
1. the `:on-change` handler of a text field where we are expecting fast typing
2. when initialising your app - see 'main' in examples/todomvc/src/core.cljs
3. in a unit test where immediate, synchronous processing is useful
Usage:
#!clj
(dispatch-sync [:sing :falsetto \"piano accordion\"])
"
{:api-docs/heading "Dispatching Events"}
[event]
(router/dispatch-sync event))
;; -- Events ------------------------------------------------------------------
(defn reg-event-db
"Register the given event `handler` (function) for the given `id`. Optionally, provide
an `interceptors` chain:
- `id` is typically a namespaced keyword (but can be anything)
- `handler` is a function: (db event) -> db
- `interceptors` is a collection of interceptors. Will be flattened and nils removed.
Example Usage:
#!clj
(reg-event-db
:token
(fn [db event]
(assoc db :some-key (get event 2))) ;; return updated db
Or perhaps:
#!clj
(reg-event-db
:namespaced/id ;; <-- namespaced keywords are often used
[one two three] ;; <-- a seq of interceptors
(fn [db [_ arg1 arg2]] ;; <-- event vector is destructured
(-> db
(dissoc arg1)
(update :key + arg2)))) ;; return updated db
"
{:api-docs/heading "Event Handlers"}
([id handler]
(reg-event-db id nil handler))
([id interceptors handler]
(events/register id [cofx/inject-db fx/do-fx std-interceptors/inject-global-interceptors interceptors (db-handler->interceptor handler)])))
(defn reg-event-fx
"Register the given event `handler` (function) for the given `id`. Optionally, provide
an `interceptors` chain:
- `id` is typically a namespaced keyword (but can be anything)
- `handler` is a function: (coeffects-map event-vector) -> effects-map
- `interceptors` is a collection of interceptors. Will be flattened and nils removed.
Example Usage:
#!clj
(reg-event-fx
:event-id
(fn [cofx event]
{:db (assoc (:db cofx) :some-key (get event 2))})) ;; return a map of effects
Or perhaps:
#!clj
(reg-event-fx
:namespaced/id ;; <-- namespaced keywords are often used
[one two three] ;; <-- a seq of interceptors
(fn [{:keys [db] :as cofx} [_ arg1 arg2]] ;; destructure both arguments
{:db (assoc db :some-key arg1) ;; return a map of effects
:fx [[:dispatch [:some-event arg2]]]}))
"
{:api-docs/heading "Event Handlers"}
([id handler]
(reg-event-fx id nil handler))
([id interceptors handler]
(events/register id [cofx/inject-db fx/do-fx std-interceptors/inject-global-interceptors interceptors (fx-handler->interceptor handler)])))
(defn reg-event-ctx
"Register the given event `handler` (function) for the given `id`. Optionally, provide
an `interceptors` chain:
- `id` is typically a namespaced keyword (but can be anything)
- `handler` is a function: context-map -> context-map
You can explore what is provided in `context` [here](https://day8.github.io/re-frame/Interceptors/#what-is-context).
Example Usage:
#!clj
(reg-event-ctx
:event-id
(fn [{:keys [coeffects] :as context}]
(let [initial {:db (:db coeffects)
:event (:event coeffects)
:fx []}
result (-> initial
function1
function2
function3)
effects (select-keys result [:db :fx])]
(assoc context :effects effects))))
"
{:api-docs/heading "Event Handlers"}
([id handler]
(reg-event-ctx id nil handler))
([id interceptors handler]
(events/register id [cofx/inject-db fx/do-fx std-interceptors/inject-global-interceptors interceptors (ctx-handler->interceptor handler)])))
(defn clear-event
"Unregisters event handlers (presumably registered previously via the use of `reg-event-db` or `reg-event-fx`).
When called with no args, it will unregister all currently registered event handlers.
When given one arg, assumed to be the `id` of a previously registered
event handler, it will unregister the associated handler. Will produce a warning to
console if it finds no matching registration."
{:api-docs/heading "Event Handlers"}
([]
(registrar/clear-handlers events/kind))
([id]
(registrar/clear-handlers events/kind id)))
;; -- subscriptions -----------------------------------------------------------
(defn reg-sub
"A call to `reg-sub` associates a `query-id` WITH two functions.
The two functions provide 'a mechanism' for creating a node
in the Signal Graph. When a node of type `query-id` is needed,
the two functions can be used to create it.
The three arguments are:
- `query-id` - typically a namespaced keyword (later used in subscribe)
- optionally, an `input signals` function which returns the input data
flows required by this kind of node.
- a `computation function` which computes the value (output) of the
node (from the input data flows)
Later, during app execution, a call to `(subscribe [:sub-id 3 :blue])`,
will trigger the need for a new `:sub-id` Signal Graph node (matching the
query `[:sub-id 3 :blue]`). And, to create that node the two functions
associated with `:sub-id` will be looked up and used.
Just to be clear: calling `reg-sub` does not immediately create a node.
It only registers 'a mechanism' (the two functions) by which nodes
can be created later, when a node is bought into existence by the
use of `subscribe` in a `View Function`.
`reg-sub` arguments are:
- a `query-id` (typically a namespaced keyword)
- a function which returns the inputs required by this kind of node (can be supplied in one of three ways)
- a function which computes the value of this kind of node (can be supplied in one of three ways)
The `computation function` is always the last argument supplied and has three ways to be called.
Two of these methods are syntactic sugar to provide easier access to functional abstractions around your data.
1. A function that will accept two parameters, the `input-values` and `query-vector`. This is the
standard way to provide a `computation-function`
#!clj
(reg-sub
:query-id
(fn [input-values query-vector]
(:foo input-values)))
2. A single sugary tuple of `:->` and a 1-arity `computation-function`:
#!clj
(reg-sub
:query-id
:-> computation-fn)
This sugary variation allows you to pass a function that will expect only one parameter,
namely the `input-values` and entirely omit the `query-vector`. A typical `computation-function`
expects two parameters which can cause unfortunate results when attempting to use
clojure standard library functions, or other functions, in a functional manner.
For example, a significant number of subscriptions exist only to get a value
from the `input-values`. As shown below, this subscription will simply retrieve
the value associated with the `:foo` key in our db:
#!clj
(reg-sub
:query-id
(fn [db _] ;; :<---- trivial boilerplate we might want to skip over
(:foo db)))
This is slightly more boilerplate than we might like to do,
as we can use a keyword directly as a function, and we might like to do this:
#!clj
(reg-sub
:query-id
:foo) ;; :<---- This could be dangerous. If `:foo` is not in db, we get the `query-vector` instead of `nil`.
By using `:->` our function would not contain the `query-vector`, and any
missing keys would be represented as such:
#!clj
(reg-sub
:query-id
:-> :foo)
This form allows us to ignore the `query-vector` if our `computation-function`
has no need for it, and be safe from any accidents. Any 1-arity function can be provided,
and for more complicated use cases, `partial`, `comp`, and anonymous functions can still be used.
3. A single sugary tuple of `:=>` and a multi-arity `computation-function`
#!clj
(reg-sub
:query-id
:=> computation-fn)
The `query-vector` can be broken into two components `[query-id & optional-values]`, and
some subscriptions require the `optional-values` for extra work within the subscription.
To use them in variation #1, we need to destructure our `computation-function` parameters
in order to use them.
#!clj
(reg-sub
:query-id
(fn [db [_ foo]]
[db foo]))
Again we are writing boilerplate just to reach our values, and we might prefer to
have direction access through a parameter vector like `[input-values optional-values]`
instead, so we might be able to use a multi-arity function directly as our `computation-function`.
A rewrite of the above sub using this sugary syntax would look like this:
#!clj
(reg-sub
:query-id
:=> vector) ;; :<---- Could also be `(fn [db foo] [db foo])`
The `computation function` is expected to take two arguments:
- `input-values` - the values which flow into this node (how is it wired into the graph?)
- `query-vector` - the vector given to `subscribe`
and it returns a computed value (which then becomes the output of the node)
When `computation function` is called, the 2nd `query-vector` argument will be that
vector supplied to the `subscribe`. So, if the call was `(subscribe [:sub-id 3 :blue])`,
then the `query-vector` supplied to the computation function will be `[:sub-id 3 :blue]`.
The argument(s) supplied to `reg-sub` between `query-id` and the `computation-function`
can vary in 3 ways, but whatever is there defines the `input signals` part
of `the mechanism`, specifying what input values \"flow into\" the
`computation function` (as the 1st argument) when it is called.
So, `reg-sub` can be called in one of three ways, because there are three ways
to define the input signals part. But note, the 2nd method, in which a
`signals function` is explicitly supplied, is the most canonical and
instructive. The other two are really just sugary variations.
**First variation** - no input signal function given:
#!clj
(reg-sub
:query-id
a-computation-fn) ;; has signature: (fn [db query-vec] ... ret-value)
In the absence of an explicit `signals function`, the node's input signal defaults to `app-db`
and, as a result, the value within `app-db` (a map) is
given as the 1st argument when `a-computation-fn` is called.
**Second variation** - a signal function is explicitly supplied:
#!clj
(reg-sub
:query-id
signal-fn ;; <-- here
computation-fn)
This is the most canonical and instructive of the three variations.
When a node is created from the template, the `signal function` will be called and it
is expected to return the input signal(s) as either a singleton, if there is only
one, or a sequence if there are many, or a map with the signals as the values.
The current values of the returned signals will be supplied as the 1st argument to
the `a-computation-fn` when it is called - and subject to what this `signal-fn` returns,
this value will be either a singleton, sequence or map of them (paralleling
the structure returned by the `signal function`).
This example `signal function` returns a 2-vector of input signals.
#!clj
(fn [query-vec dynamic-vec]
[(subscribe [:a-sub])
(subscribe [:b-sub])])
The associated computation function must be written
to expect a 2-vector of values for its first argument:
#!clj
(fn [[a b] query-vec] ;; 1st argument is a seq of two values
....)
If, on the other hand, the signal function was simpler and returned a singleton, like this:
#!clj
(fn [query-vec dynamic-vec]
(subscribe [:a-sub])) ;; <-- returning a singleton
then the associated computation function must be written to expect a single value
as the 1st argument:
#!clj
(fn [a query-vec] ;; 1st argument is a single value
...)
Further Note: variation #1 above, in which an `signal-fn` was not supplied, like this:
#!clj
(reg-sub
:query-id
a-computation-fn) ;; has signature: (fn [db query-vec] ... ret-value)
is the equivalent of using this
2nd variation and explicitly supplying a `signal-fn` which returns `app-db`:
#!clj
(reg-sub
:query-id
(fn [_ _] re-frame/app-db) ;; <--- explicit signal-fn
a-computation-fn) ;; has signature: (fn [db query-vec] ... ret-value)
**Third variation** - syntax Sugar
#!clj
(reg-sub
:a-b-sub
:<- [:a-sub]
:<- [:b-sub]
(fn [[a b] query-vec] ;; 1st argument is a seq of two values
{:a a :b b}))
This 3rd variation is just syntactic sugar for the 2nd. Instead of providing an
`signals-fn` you provide one or more pairs of `:<-` and a subscription vector.
If you supply only one pair a singleton will be supplied to the computation function,
as if you had supplied a `signal-fn` returning only a single value:
#!clj
(reg-sub
:a-sub
:<- [:a-sub]
(fn [a query-vec] ;; only one pair, so 1st argument is a single value
...))
Syntactic sugar for both the `signal-fn` and `computation-fn` can be used together
and the direction of arrows shows the flow of data and functions. The example from
directly above is reproduced here:
#!clj
(reg-sub
:a-b-sub
:<- [:a-sub]
:<- [:b-sub]
:-> (partial zipmap [:a :b]))
For further understanding, read the tutorials, and look at the detailed comments in
/examples/todomvc/src/subs.cljs.
See also: `subscribe`
"
{:api-docs/heading "Subscriptions"}
[query-id & args]
(apply subs/reg-sub query-id args))
(defn subscribe
"Given a `query` vector, returns a Reagent `reaction` which will, over
time, reactively deliver a stream of values. So, in FRP-ish terms,
it returns a `Signal`.
To obtain the current value from the Signal, it must be dereferenced:
#!clj
(let [signal (subscribe [:items])
value (deref signal)] ;; could be written as @signal
...)
which is typically written tersely as simple:
#!clj
(let [items @(subscribe [:items])]
...)
`query` is a vector of at least one element. The first element is the
`query-id`, typically a namespaced keyword. The rest of the vector's
elements are optional, additional values which parameterise the query
performed.
`dynv` exists for historical reasons and is borderline deprecated these days.
It is a vector of signals. Re-frame will dereference each of them and pass a
vector of their values to your subscription handler as a third argument.
If there's logic determing __what__ query to subscribe __to__, consider
expressing it in a `signal function`, or use `reg-sub-raw`. Failing that, `dynv`
allows you to colocate this logic with the `subscribe` call.
**Example Usage**:
#!clj
(subscribe [:items])
(subscribe [:items \"blue\" :small])
(subscribe [:items {:colour \"blue\" :size :small}])
Note: for any given call to `subscribe` there must have been a previous call
to `reg-sub`, registering the query handler (functions) associated with
`query-id`.
**Hint**
When used in a view function BE SURE to `deref` the returned value.
In fact, to avoid any mistakes, some prefer to define:
#!clj
(def <sub (comp deref re-frame.core/subscribe))
And then, within their views, they call `(<sub [:items :small])` rather
than using `subscribe` directly.
**De-duplication**
Two, or more, concurrent subscriptions for the same query will
source reactive updates from the one executing handler.
See also: `reg-sub`
"
{:api-docs/heading "Subscriptions"}
([query]
(subs/subscribe query))
([query dynv]
(subs/subscribe query dynv)))
(defn clear-sub ;; think unreg-sub
"Unregisters subscription handlers (presumably registered previously via the use of `reg-sub`).
When called with no args, it will unregister all currently registered subscription handlers.
When given one arg, assumed to be the `id` of a previously registered
subscription handler, it will unregister the associated handler. Will produce a warning to
console if it finds no matching registration.
NOTE: Depending on the usecase, it may be necessary to call `clear-subscription-cache!` afterwards"
{:api-docs/heading "Subscriptions"}
([]
(registrar/clear-handlers subs/kind))
([query-id]
(registrar/clear-handlers subs/kind query-id)))
(defn reg-sub-raw
"This is a low level, advanced function. You should probably be
using `reg-sub` instead.
Some explanation is available in the docs at
<a href=\"http://day8.github.io/re-frame/flow-mechanics/\" target=\"_blank\">http://day8.github.io/re-frame/flow-mechanics/</a>"
{:api-docs/heading "Subscriptions"}
[query-id handler-fn]
(registrar/register-handler subs/kind query-id handler-fn))
;; XXX
(defn clear-subscription-cache!
"Removes all subscriptions from the cache.
This function can be used at development time or test time. Useful when hot reloading
namespaces containing subscription handlers. Also call it after a React/render exception,
because React components won't have been cleaned up properly. And this, in turn, means
the subscriptions within those components won't have been cleaned up correctly. So this
forces the issue.
"
{:api-docs/heading "Subscriptions"}
[]
(subs/clear-subscription-cache!))
;; -- effects -----------------------------------------------------------------
(defn reg-fx
"Register the given effect `handler` for the given `id`:
- `id` is keyword, often namespaced.
- `handler` is a side-effecting function which takes a single argument and whose return
value is ignored.
To use, first, associate `:effect2` with a handler:
#!clj
(reg-fx
:effect2
(fn [value]
... do something side-effect-y))
Then, later, if an event handler were to return this effects map:
#!clj
{:effect2 [1 2]}
then the `handler` `fn` we registered previously, using `reg-fx`, will be
called with an argument of `[1 2]`.
"
{:api-docs/heading "Effect Handlers"}
[id handler]
(fx/reg-fx id handler))
(defn clear-fx ;; think unreg-fx
"Unregisters effect handlers (presumably registered previously via the use of `reg-fx`).
When called with no args, it will unregister all currently registered effect handlers.
When given one arg, assumed to be the `id` of a previously registered
effect handler, it will unregister the associated handler. Will produce a warning to
console if it finds no matching registration.
"
{:api-docs/heading "Effect Handlers"}
([]
(registrar/clear-handlers fx/kind))
([id]
(registrar/clear-handlers fx/kind id)))
;; -- coeffects ---------------------------------------------------------------
(defn reg-cofx
"Register the given coeffect `handler` for the given `id`, for later use
within `inject-cofx`:
- `id` is keyword, often namespaced.
- `handler` is a function which takes either one or two arguments, the first of which is
always `coeffects` and which returns an updated `coeffects`.
See also: `inject-cofx`
"
{:api-docs/heading "Coeffects"}
[id handler]
(cofx/reg-cofx id handler))
(defn inject-cofx
"Given an `id`, and an optional, arbitrary `value`, returns an interceptor
whose `:before` adds to the `:coeffects` (map) by calling a pre-registered
'coeffect handler' identified by the `id`.
The previous association of a `coeffect handler` with an `id` will have
happened via a call to `re-frame.core/reg-cofx` - generally on program startup.
Within the created interceptor, this 'looked up' `coeffect handler` will
be called (within the `:before`) with two arguments:
- the current value of `:coeffects`
- optionally, the originally supplied arbitrary `value`
This `coeffect handler` is expected to modify and return its first, `coeffects` argument.
**Example of `inject-cofx` and `reg-cofx` working together**
First - Early in app startup, you register a `coeffect handler` for `:datetime`:
#!clj
(re-frame.core/reg-cofx
:datetime ;; usage (inject-cofx :datetime)
(fn coeffect-handler
[coeffect]
(assoc coeffect :now (js/Date.)))) ;; modify and return first arg
Second - Later, add an interceptor to an -fx event handler, using `inject-cofx`:
#!clj
(re-frame.core/reg-event-fx ;; when registering an event handler
:event-id
[ ... (inject-cofx :datetime) ... ] ;; <-- create an injecting interceptor
(fn event-handler
[coeffect event]
;;... in here can access (:now coeffect) to obtain current datetime ...
)))
**Background**
`coeffects` are the input resources required by an event handler
to perform its job. The two most obvious ones are `db` and `event`.
But sometimes an event handler might need other resources.
Perhaps an event handler needs a random number or a GUID or the current
datetime. Perhaps it needs access to a DataScript database connection.
If an event handler directly accesses these resources, it stops being
pure and, consequently, it becomes harder to test, etc. So we don't
want that.
Instead, the interceptor created by this function is a way to 'inject'
'necessary resources' into the `:coeffects` (map) subsequently given
to the event handler at call time.
See also `reg-cofx`
"
{:api-docs/heading "Coeffects"}
([id]
(cofx/inject-cofx id))
([id value]
(cofx/inject-cofx id value)))
(defn clear-cofx ;; think unreg-cofx
"Unregisters coeffect handlers (presumably registered previously via the use of `reg-cofx`).
When called with no args, it will unregister all currently registered coeffect handlers.
When given one arg, assumed to be the `id` of a previously registered
coeffect handler, it will unregister the associated handler. Will produce a warning to
console if it finds no matching registration."
{:api-docs/heading "Coeffects"}
([]
(registrar/clear-handlers cofx/kind))
([id]
(registrar/clear-handlers cofx/kind id)))
;; -- error handler ----------------------------------------------------------
(defn reg-event-error-handler
"Register the given event error `handler` (function) that will catch unhandled exceptions
thrown in the interceptors/handler chain.
Only one `handler` can be registered. Registering a new `handler` clears the existing `handler`.
This `handler` function has the signature:
`(handler [original-error re-frame-error])`
- `original-error`: A plaform-native Error object.
Represents the original error thrown by user code.
this is the error you see when no `handler` is registered.
- `re-frame-error`: A clojure ExceptionInfo object.
Includes the stacktrace of re-frame's internal functions,
and extra data about the interceptor process.
Call `(ex-data re-frame-error)` to get this info.
The data includes:
- `:interceptor`: the `:id` of the throwing interceptor.
- `:direction`: `:before` or `:after`.
- `:event-v`: the re-frame event which invoked this interceptor."
[handler]
(registrar/register-handler :error :event-handler handler))
(reg-event-error-handler interceptor/default-error-handler)
;; -- interceptors ------------------------------------------------------------
(def ^{:api-docs/heading "Interceptors"} debug
"An interceptor which logs/instruments an event handler's actions to
`re-frame/console` at the `:log` level.
Output includes:
1. the event vector
2. a `clojure.data/diff` of db, before vs after, which shows
the changes caused by the event handler. To understand the output,
you should understand:
<a href=\"https://clojuredocs.org/clojure.data/diff\" target=\"_blank\">https://clojuredocs.org/clojure.data/diff</a>.
You'd typically include this interceptor after (to the right of) any
`path` interceptor.
Warning: calling `clojure.data/diff` on large, complex data structures
can be slow. So, you won't want this interceptor present in production
code. So, you should condition it out like this:
#!clj
(re-frame.core/reg-event-db
:evt-id
[(when ^boolean goog.DEBUG re-frame.core/debug)] ;; <-- conditional
(fn [db v]
...))
To make this code fragment work, you'll also have to set `goog.DEBUG` to
`false` in your production builds. For an example, look in `project.clj` of /examples/todomvc.
"
std-interceptors/debug)
(defn path
"Returns an interceptor which acts somewhat like `clojure.core/update-in`, in the sense that
the event handler is given a specific part of `app-db` to change, not all of `app-db`.
The interceptor has both a `:before` and `:after` functions. The `:before` replaces
the `:db` key within coeffects with a sub-path within `app-db`. The `:after` reverses the process,
and it grafts the handler's return value back into db, at the right path.
Examples:
#!clj
(path :some :path)
(path [:some :path])
(path [:some :path] :to :here)
(path [:some :path] [:to] :here)
Example Use:
#!clj
(reg-event-db
:event-id
(path [:a :b]) ;; <-- used here, in interceptor chain
(fn [b v] ;; 1st arg is not db. Is the value from path [:a :b] within db
... new-b)) ;; returns a new value for that path (not the entire db)
Notes:
1. `path` may appear more than once in an interceptor chain. Progressive narrowing.
2. if `:effects` contains no `:db` effect, can't graft a value back in.
"
{:api-docs/heading "Interceptors"}
[& args]
(apply std-interceptors/path args))
(defn enrich
"Returns an interceptor which will run the given function `f` in the `:after`
position.
`f` is called with two arguments: `db` and `event`, and is expected to
return a modified `db`.
Unlike the `after` interceptor which is only about side effects, `enrich`
expects `f` to process and alter the given `db` coeffect in some useful way,
contributing to the derived data, flowing vibe.
If `f` returns `nil`, the `db` value passed to `f` will be returned instead.
#### Example Use:
Imagine that todomvc needed to do duplicate detection - if any two todos had
the same text, then highlight their background, and report them via a warning
at the bottom of the panel.
Almost any user action (edit text, add new todo, remove a todo) requires a
complete reassessment of duplication errors and warnings. E.g. that edit
just made might have introduced a new duplicate, or removed one. Same with
any todo removal. So we need to re-calculate warnings after any CRUD events
associated with the todos list.
Unless we are careful, we might end up coding subtly different checks
for each kind of CRUD operation. The duplicates check made after
'delete todo' event might be subtly different to that done after an
editing operation. Nice and efficient, but fiddly. A bug generator
approach.
So, instead, we create an `f` which recalculates ALL warnings from scratch
every time there is ANY change. It will inspect all the todos, and
reset ALL FLAGS every time (overwriting what was there previously)
and fully recalculate the list of duplicates (displayed at the bottom?).
<a href=\"https://twitter.com/nathanmarz/status/879722740776939520\" target=\"_blank\">https://twitter.com/nathanmarz/status/879722740776939520</a>
By applying `f` in an `:enrich` interceptor, after every CRUD event,
we keep the handlers simple and yet we ensure this important step
(of getting warnings right) is not missed on any change.
We can test `f` easily - it is a pure function - independently of
any CRUD operation.
This brings huge simplicity at the expense of some re-computation
each time. This may be a very satisfactory trade-off in many cases.
#### Returning nil
In some cases, it's useful to apply a change to specific situations that can
be determined at runtime instead of when defining the handler with an
`:enrich` interceptor. Instead of forcing you to return the `db` from every
non-applicable branch, you can return `nil` to use the given `db` value:
#!clj
(def set-last-update
(core/enrich
(fn [{db :db} [_ {user :user}]]
(when (active-user? user) ;; <- Only perform an update if user is active
...))))
"
{:api-docs/heading "Interceptors"}
[f]
(std-interceptors/enrich f))
(def ^{:api-docs/heading "Interceptors"} unwrap
"> New in v1.2.0
An interceptor which decreases the amount of destructuring necessary in an
event handler where the event is structured as a 2-vector of
[event-id payload-map].
It promotes the `payload-map` part to be the event ultimately given to the
event handler. Should you want the full original event, it can be found in
`coeffects` under the key `:original-event`.
If a dispatch looked like this:
#!clj
(dispatch [:event-id {:x 1 :y 2 :z 3}])
Your event handlers can look like this:
#!clj
(reg-event-fx
:event-id
[... unwrap ...] ;; <-- added to the interceptors
(fn [{:keys [db]} {:keys [x y z]}] ;; <-- instead of [_ {:keys [x y z]}]
...)
"
std-interceptors/unwrap)
(def ^{:api-docs/heading "Interceptors"} trim-v
"An interceptor which removes the first element of the event vector,
before it is supplied to the event handler, allowing you to write more
aesthetically pleasing event handlers. No leading underscore on the event-v!
Should you want the full original event, it can be found in `coeffects` under
the key `:original-event`.
Your event handlers will look like this:
#!clj
(reg-event-db
:event-id
[... trim-v ...] ;; <-- added to the interceptors
(fn [db [x y z]] ;; <-- instead of [_ x y z]
...)
"
std-interceptors/trim-v)
(defn after
"Returns an interceptor which runs the given function `f` in the `:after`
position, presumably for side effects.
`f` is called with two arguments: the `:effects` value for `:db`
(or the `:coeffect` value of `:db` if no `:db` effect is returned) and the event.
Its return value is ignored, so `f` can only side-effect.
An example of use can be seen in the re-frame github repo in `/examples/todomvc/events.cljs`:
- `f` runs schema validation (reporting any errors found).
- `f` writes to localstorage."
{:api-docs/heading "Interceptors"}
[f]
(std-interceptors/after f))
(defn on-changes
"Returns an interceptor which will observe N paths within `db`, and if any of them
test not `identical?` to their previous value (as a result of a event handler
being run), then it will run `f` to compute a new value, which is then assoc-ed
into the given `out-path` within `db`.
Example Usage:
#!clj
(defn my-f
[a-val b-val]
... some computation on a and b in here)
;; use it
(def my-interceptor (on-changes my-f [:c] [:a] [:b]))
(reg-event-db
:event-id
[... my-interceptor ...] ;; <-- ultimately used here
(fn [db v]
...))
If you put this interceptor on handlers which might change paths `:a` or `:b`,
it will:
- call `f` each time the value at path `[:a]` or `[:b]` changes
- call `f` with the values extracted from `[:a]` `[:b]`
- assoc the return value from `f` into the path `[:c]`
"
{:api-docs/heading "Interceptors"}
[f out-path & in-paths]
(apply std-interceptors/on-changes f out-path in-paths))
(defn reg-global-interceptor
"Registers the given `interceptor` as a global interceptor. Global interceptors are
included in the processing chain of every event.
When you register an event handler, you have the option of supplying an
interceptor chain. Any global interceptors you register are effectively
prepending to this chain.
Global interceptors are run in the order that they are registered.
Global interceptors are unique by :id. If a global interceptor with the same :id
key as `interceptor` is already registered, `interceptor` will take its place in the
global interceptor chain. This facilitates hot-reloading.
Note: members of re-frame.std-interceptors do not have unique ids. To register
more than one, consider:
(reg-global-interceptor (-> (re-frame.std-interceptors/on-changes + [:a] [:b])
(assoc :id :my-unique-id)))"
{:api-docs/heading "Global Interceptors"}
[interceptor]
(settings/reg-global-interceptor interceptor))
(defn clear-global-interceptor
"Unregisters global interceptors (presumably registered previously via the use of `reg-global-interceptor`).
When called with no args, it will unregister all currently registered global interceptors.
When given one arg, assumed to be the `id` of a previously registered
global interceptors, it will unregister the associated interceptor. Will produce a warning to
console if it finds no matching registration."
{:api-docs/heading "Global Interceptors"}
([]
(settings/clear-global-interceptors))
([id]
(settings/clear-global-interceptors id)))
(defn ->interceptor
"A utility function for creating interceptors.
Accepts three optional, named arguments:
- `:id` - an id for the interceptor (decorative only)
- `:before` - the interceptor's before function
- `:after` - the interceptor's after function
Example use:
#!clj
(def my-interceptor
(->interceptor
:id :my-interceptor
:before (fn [context]
... modifies and returns `context`)
:after (fn [context]
... modifies and returns `context`)))
Notes:
- `:before` functions modify and return their `context` argument. Sometimes they