re-frame/docs/WIP/Flow.md

103 lines
3.8 KiB
Markdown
Raw Normal View History

2016-12-03 10:44:06 +00:00
## Flow
This tutorial focuses mainly on how data flows between dominoes 3-4-5-6.
2016-12-04 03:20:25 +00:00
We'll look at the underlying reactive mechanism.
2016-12-03 10:44:06 +00:00
BUT we'll start by looking at the overall picture ...
2016-11-30 10:28:01 +00:00
## Interconnections
Ask a systems theorist, and they'll tell you that a system has **parts** and **interconnections**.
Human brains tend to focus first on the **parts**, and then, later, the
2016-12-03 10:44:06 +00:00
**interconnections**.
2016-11-30 10:28:01 +00:00
2016-12-03 10:44:06 +00:00
In the case of re-frame, dominoes are the parts, so, tick, yes, we have
looked at them first. But, if the parts are functions, what then of
the **interconnections**?
2016-11-30 10:28:01 +00:00
2016-12-04 03:20:25 +00:00
What does it even mean to talk about **interconnections between functions?**
To answer that question, I'll rephrase it as:
2016-11-30 10:28:01 +00:00
how are the domino functions **composed**. How does `f ∘ g` happen?
At the language level, we know how function composition works.
A function such as `count` composes like this:
```clj
(str (count (filter odd? [1 2 3 4 5])))
```
Clojure's semantics tell us when `count` is called, and with what
argument, and how the value it computes becomes the arg for a further function.
We know how data "flows" into and out of the functions.
2016-12-03 10:44:06 +00:00
Sometimes, we'd rewrite the code above as:
2016-11-30 10:28:01 +00:00
```clj
(->> [1 2 3 4 5]
(filter odd?)
count
str)
2016-12-03 10:44:06 +00:00
```
2016-11-30 10:28:01 +00:00
When we arrange our code like this, we talk of "threading" data
2016-12-03 10:44:06 +00:00
through functions. It somehow helps comprehension to frame function composition as a data flow.
2016-11-30 10:28:01 +00:00
2016-12-04 03:20:25 +00:00
In a similar spirit, you can almost see re-frame's 6 domino cascade like this:
2016-11-30 10:28:01 +00:00
```clj
(->> event ;; domino 1
event-handler ;; 2
effect-handler ;; 3
2016-12-03 10:44:06 +00:00
-- app state --- ;;
2016-11-30 10:28:01 +00:00
queries ;; 4
view-fns ;; 5
React) ;; 6
2016-12-03 10:44:06 +00:00
```
2016-11-30 10:28:01 +00:00
Fine. So how, then, does data get threaded through the domino functions of re-frame?
How does data flow out of one function (domino)
into the next? What theory of computation is driving this bus?
My first, weak answer is: how wonderful is it that you don't need to worry about
this too much, re-frame looks
after it for you. It will thread (convey) data from one domino function to the next.
It will call your functions at the right time, with the right (data) arguments.
2016-12-04 03:20:25 +00:00
My second answer is: the method varies from domino to domino. Read on. But our
main focus is on the reactive flow of 3-4-5-6.
2016-11-30 10:28:01 +00:00
## 1 -> 2
`dispatch` queues events and they are not immediately processed. So event handling is done async.
A router reads events from this queue, looks up the right handler and calls it.
2016-12-03 10:44:06 +00:00
xxx
2016-11-30 10:28:01 +00:00
## 2 -> 3
2016-12-03 10:44:06 +00:00
Except I lied in the previous section. The router doesn't really look up a single "handler". Instead it looks up an interceptor chain (described later).
2016-11-30 10:28:01 +00:00
That interceptor chain is a pipeline of functions. The last of them
2016-12-03 10:44:06 +00:00
xxx
2016-11-30 10:28:01 +00:00
2016-12-03 10:44:06 +00:00
## 3->4->5->6
## On Flow
Arguments from authority ...
> Everything flows, nothing stands still. (Panta rhei)
> No man ever steps in the same river twice for it's not the same river and he's not the same man.
[Heraclitus 500 BC](http://en.wikiquote.org/wiki/Heraclitus). Who, being Greek, had never seen a frozen river. [alt version](http://farm6.static.flickr.com/5213/5477602206_ecb78559ed.jpg).
> Think of an experience from your childhood. Something you remember clearly, something you can see,
feel, maybe even smell, as if you were really there. After all you really were there at the time,
werent you? How else could you remember it? But here is the bombshell: you werent there. Not a
single atom that is in your body today was there when that event took place .... Matter flows
from place to place and momentarily comes together to be you. Whatever you are, therefore, you
are not the stuff of which you are made. If that does not make the hair stand up on the back of
your neck, read it again until it does, because it is important.
Steve Grand
2016-12-04 03:20:25 +00:00