Skip to content

Commit da81e04

Browse files
committed
Update mode docs
1 parent 6bf9d85 commit da81e04

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

docs/events.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Application model message = {
1717
}
1818
```
1919

20-
is the `update` function. This is where we define our business logic by matching event `message`s and returning an updated model. So far we have been using the `Update` type alias. Let's expand it:
20+
is the `update` function. So far we have been using the `Update` type alias. Let's expand it:
2121

2222
```haskell
2323
type Application model message = {
@@ -28,7 +28,7 @@ type Application model message = {
2828
}
2929
```
3030

31-
Besides the updated model, `update` also returns an array of side effects to perform. Each entry in the array may optionally raise another `message`, which in turn is handled by `update` as well.
31+
That means that `update` returns an updated model but also an array of side effects to perform. Each entry in the array may optionally raise another `message`, which is in turn handled by `update` as well.
3232

3333
Consider an application to roll dices
3434

@@ -54,7 +54,7 @@ view model = HE.main "main" [
5454

5555
`Roll` returns the model as it is. However, generating random numbers is a side effect so we return it on our array. Flame will run this effect and raise `Update`, which then updates the model with the die number.
5656

57-
Likewise, we could define a loading screen to appear before AJAX requests
57+
Likewise, we could perform some network requests with a loading screen
5858

5959
```haskell
6060
type Model = {
@@ -63,13 +63,13 @@ type Model = {
6363
}
6464

6565
data Message =
66-
Loading |
66+
Perform |
6767
Response String |
6868
DifferentResponse String |
6969
Finish String
7070

71-
performAJAX :: String -> Aff String
72-
performAJAX url = ...
71+
fetch :: String -> Aff String
72+
fetch url = ...
7373

7474
useResponse :: Model -> String -> Aff Model
7575
useResponse = ...
@@ -79,28 +79,28 @@ useDifferentResponse = ...
7979

8080
update :: Model -> Message -> Tuple Model (Array (Aff (Maybe Message)))
8181
update model = case _ of
82-
Loading -> model { isLoading = true } /\ requests
82+
Perform -> model { isLoading = true } /\ requests
8383
Response contents -> F.noMessages $ useResponse model contents -- noMessages is the same as _ /\ []
8484
Finish contents -> F.noMessages model { isLoading = false, response = model.response <> contents }
8585
where requests = [
86-
Just <<< Response <$> performAJAX "url",
87-
Just <<< DifferentResponse <$> performAJAX "url2",
88-
Just <<< Response <$> performAJAX "url3",
89-
Just <<< DifferentResponse <$> performAJAX "url4",
86+
Just <<< Response <$> fetch "url",
87+
Just <<< DifferentResponse <$> fetch "url2",
88+
Just <<< Response <$> fetch "url3",
89+
Just <<< DifferentResponse <$> fetch "url4",
9090
pure <<< Just $ Finish "Performed all"
9191
]
9292

9393
view :: Model -> Html Message
9494
view model = HE.main "main" [
95-
HE.button [HA.disabled model.isLoading, HA.onClick Loading] "Perform requests",
95+
HE.button [HA.disabled model.isLoading, HA.onClick Perform] "Perform requests",
9696
if model.isLoading then
9797
HE.div [HA.class' "overlay"] "Loading..."
9898
else
9999
...
100100
]
101101
```
102102

103-
Here for `Loading`, we return an array of network calls and a final `Finish` message. The effects are run in order, and once we have a response, their events are raised for `update` as well.
103+
Here for `Perform`, we return an array of network calls and a final `Finish` message. The effects are run in order, and once we have a response their events are raised for `update` as well.
104104

105105
You may be wondering: why separate model updating and side effects? The reason is that in this way we are "forced" to keep most of our business logic in pure functions, which are easier to reason and test. Effects become interchangeable, decoupled from what we do with their results.
106106

@@ -135,7 +135,7 @@ Once a subscription has been defined, the raised `message` will be handled by th
135135

136136
* Arbitrary message passing
137137

138-
Sometimes, we need to talk to an application from external events handlers or other points in the code away from the mount point. Consider an app that uses web sockets, or a singe page application that uses multiple mount points for lazy loading, or just simple initialization events. For these and other use cases, Flame provides a `mount` (no trailing underscore) function that takes an application id, as well a `send` function to raise messages for application ids
138+
Sometimes, we need to talk to an application from external events handlers or other points in the code away from the mount point. Consider an app that uses web sockets, or a singe page application that uses multiple mount points for lazy loading, or just some initialization events. For these and other use cases, Flame provides a `mount` (no trailing underscore) function that takes an application id, as well a `send` function to raise messages for application ids
139139

140140
```haskell
141141
mount :: forall id model message. Show id => QuerySelector -> AppId id message -> Application model message -> Effect Unit
@@ -199,7 +199,7 @@ Flame also provides a way to "broadcast" `CustomEvent`s for all listeners. `Flam
199199
broadcast :: forall arg. SerializeState arg => EventType -> arg -> Effect Unit
200200
```
201201

202-
whose events can be handled with `onCustomEven` on your `subscribe` list. Broadcasting events is considered unsafe, as it is user code responsibility to make sure all listeners expect the same `message` payload.
202+
whose events can be handled with `onCustomEvent` on your `subscribe` list. Broadcasting events is considered unsafe as it is user code responsibility to make sure all listeners expect the same `message` payload.
203203

204204
See the [API reference](https://pursuit.purescript.org/packages/purescript-flame) for a complete list of built-in external events. See this [test application](https://github.com/easafe/purescript-flame/tree/master/examples/Subscriptions) for a full example of subscriptions.
205205

0 commit comments

Comments
 (0)