mirror of
https://github.com/status-im/re-frame.git
synced 2025-02-23 15:28:09 +00:00
Merge branch 'develop'
This commit is contained in:
commit
4471fe58c8
@ -108,7 +108,7 @@ And here's more carnage:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Again, this approach will work. But that dirty great big side-effect doesn't come
|
Again, this approach will work. But that dirty great big side-effect doesn't come
|
||||||
for free. Its like a muddy monster truck has shown up in our field of white tulips.
|
for free. It's like a muddy monster truck has shown up in our field of white tulips.
|
||||||
|
|
||||||
### Bad, Why?
|
### Bad, Why?
|
||||||
|
|
||||||
@ -353,7 +353,7 @@ data from arguments.
|
|||||||
|
|
||||||
`-db` handlers and `-fx` handlers are conceptually the same. They only differ numerically.
|
`-db` handlers and `-fx` handlers are conceptually the same. They only differ numerically.
|
||||||
|
|
||||||
`-db` handlers take ONE coeeffect called `db`, and they return only ONE effect (db again).
|
`-db` handlers take ONE coeffect called `db`, and they return only ONE effect (db again).
|
||||||
|
|
||||||
Whereas `-fx` handlers take potentially MANY coeffects (a map of them) and they return
|
Whereas `-fx` handlers take potentially MANY coeffects (a map of them) and they return
|
||||||
potentially MANY effects (a map of them). So, One vs Many.
|
potentially MANY effects (a map of them). So, One vs Many.
|
||||||
@ -381,7 +381,7 @@ can. The `-fx` version is more flexible, so it will sometimes have its place.
|
|||||||
|
|
||||||
90% of the time, simple `-db` handlers are the right tool to use.
|
90% of the time, simple `-db` handlers are the right tool to use.
|
||||||
|
|
||||||
But about 10% of the time, our handlers need additional inputs (coeffecs) or they need to
|
But about 10% of the time, our handlers need additional inputs (coeffects) or they need to
|
||||||
cause additional side-effects (effects). That's when you reach for `-fx` handlers.
|
cause additional side-effects (effects). That's when you reach for `-fx` handlers.
|
||||||
|
|
||||||
`-fx` handlers allow us to return effects, declaratively in data.
|
`-fx` handlers allow us to return effects, declaratively in data.
|
||||||
@ -389,4 +389,3 @@ cause additional side-effects (effects). That's when you reach for `-fx` handle
|
|||||||
In the next tutorial, we'll shine a light on `interceptors` which are
|
In the next tutorial, we'll shine a light on `interceptors` which are
|
||||||
the mechanism by which event handlers are executed. That knowledge will give us a springboard
|
the mechanism by which event handlers are executed. That knowledge will give us a springboard
|
||||||
to then, as a next step, better understand coeffects and effects. We'll soon be writing our own.
|
to then, as a next step, better understand coeffects and effects. We'll soon be writing our own.
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ Flowing" story promoted by re-frame.
|
|||||||
So, you'll want to use Interceptors because they solve problems, and help you to write nice code.
|
So, you'll want to use Interceptors because they solve problems, and help you to write nice code.
|
||||||
|
|
||||||
__Second__, under the covers, Interceptors provide the mechanism by which
|
__Second__, under the covers, Interceptors provide the mechanism by which
|
||||||
event handlers are executed (when you `dispatch`). You they are central concept.
|
event handlers are executed (when you `dispatch`). They are a central concept.
|
||||||
|
|
||||||
### What Do Interceptors Do?
|
### What Do Interceptors Do?
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ They wrap.
|
|||||||
Specifically, they wrap event handlers.
|
Specifically, they wrap event handlers.
|
||||||
|
|
||||||
Imagine your event handler is like a piece of ham. An interceptor would be
|
Imagine your event handler is like a piece of ham. An interceptor would be
|
||||||
like bread either side of your ham, which makes a sandwich.
|
like bread on either side of your ham, which makes a sandwich.
|
||||||
|
|
||||||
And two Interceptors, in a chain, would be like you put another
|
And two Interceptors, in a chain, would be like you put another
|
||||||
pair of bread slices around the outside of the existing sandwich to make
|
pair of bread slices around the outside of the existing sandwich to make
|
||||||
@ -154,7 +154,7 @@ That's it. That's how an event gets handled.
|
|||||||
Some data called a `context` is threaded through all the calls.
|
Some data called a `context` is threaded through all the calls.
|
||||||
|
|
||||||
This value is passed as the argument to every `:before` and `:after`
|
This value is passed as the argument to every `:before` and `:after`
|
||||||
function and they returned it, possibly modified.
|
function and it is returned by each function, possibly modified.
|
||||||
|
|
||||||
A `context` is a map with this structure:
|
A `context` is a map with this structure:
|
||||||
```clj
|
```clj
|
||||||
@ -189,7 +189,7 @@ DataScript connection. Interceptors can build up `:coeffects`, via their
|
|||||||
|
|
||||||
Equally, some interceptors in the chain will have `:after` functions
|
Equally, some interceptors in the chain will have `:after` functions
|
||||||
which process the side effects accumulated into `:effects`
|
which process the side effects accumulated into `:effects`
|
||||||
including but, not limited to, updates to app-db.
|
including, but not limited to, updates to app-db.
|
||||||
|
|
||||||
### Self Modifying
|
### Self Modifying
|
||||||
|
|
||||||
@ -223,7 +223,7 @@ If our components did this:
|
|||||||
|
|
||||||
We'd have to write this handler:
|
We'd have to write this handler:
|
||||||
```clj
|
```clj
|
||||||
(def-event-db
|
(reg-event-db
|
||||||
:delete-item
|
:delete-item
|
||||||
(fn
|
(fn
|
||||||
[db [_ key-to-delete]] ;; <---- Arrgggghhh underscore
|
[db [_ key-to-delete]] ;; <---- Arrgggghhh underscore
|
||||||
@ -239,7 +239,7 @@ What a relief it would be to get rid of it, but how? We'll write an interceptor:
|
|||||||
|
|
||||||
Once we have written `trim-event`, our registration will change to look like this:
|
Once we have written `trim-event`, our registration will change to look like this:
|
||||||
```clj
|
```clj
|
||||||
(def-event-db
|
(reg-event-db
|
||||||
:delete-item
|
:delete-item
|
||||||
[trim-event] ;; <--- interceptor added
|
[trim-event] ;; <--- interceptor added
|
||||||
(fn
|
(fn
|
||||||
|
@ -121,7 +121,7 @@ to our event handler (`cofx`).
|
|||||||
|
|
||||||
### `inject-cofx`
|
### `inject-cofx`
|
||||||
|
|
||||||
`inject-cofx` is part of re-frame API.
|
`inject-cofx` is part of the re-frame API.
|
||||||
|
|
||||||
It is a function which returns an Interceptor whose `:before` function loads
|
It is a function which returns an Interceptor whose `:before` function loads
|
||||||
a key/value pair into a `context's` `:coeffect` map.
|
a key/value pair into a `context's` `:coeffect` map.
|
||||||
@ -159,7 +159,7 @@ Each `cofx-id` requires a different action.
|
|||||||
|
|
||||||
This function is also part of the re-frame API.
|
This function is also part of the re-frame API.
|
||||||
|
|
||||||
It allows you associate a`cofx-id` (like `:now` or `:local-store`) with a
|
It allows you to associate a`cofx-id` (like `:now` or `:local-store`) with a
|
||||||
handler function that injects the right key/value pair.
|
handler function that injects the right key/value pair.
|
||||||
|
|
||||||
The function you register will be passed two arguments:
|
The function you register will be passed two arguments:
|
||||||
@ -227,7 +227,7 @@ registration functions and have them auto insert the DataScript connection.
|
|||||||
|
|
||||||
### Testing
|
### Testing
|
||||||
|
|
||||||
During testing, you may want to stub out certain coeffets.
|
During testing, you may want to stub out certain coeffects.
|
||||||
|
|
||||||
You may, for example, want to test that an event handler works
|
You may, for example, want to test that an event handler works
|
||||||
using a specific `now`, not a true random number.
|
using a specific `now`, not a true random number.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user