You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Simplify example folders; add extra example; add clarification to documentation
* Custom events instead of signals
* Finish draft and fix tests
* Updated examples
* Split window and document events so names dont clash
* add window focus
* More window events
* First half of documentation
* Finish documentation
* Add examples
* Finish example
* Rebased
Copy file name to clipboardExpand all lines: docs/concepts.md
+24-10Lines changed: 24 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -7,35 +7,42 @@ permalink: /concepts
7
7
## Main concepts
8
8
9
9
A Flame application consists of the following record
10
+
10
11
```haskell
11
12
typeApplicationmodelmessage= {
12
13
init::model,
13
14
view::model->Htmlmessage,
14
-
update::model->message->model
15
+
update::model->message->model,
16
+
subscribe::Array (Subscriptionmessage)
15
17
}
16
18
```
17
19
The type variable `model` refers to the state of the application. `message`, on the other hand, describes the kinds of events the application can handle.
18
20
19
21
### Application state
20
22
21
23
In the counter example we set our model as a simple type alias
24
+
22
25
```haskell
23
26
typeModel=Int
24
27
```
28
+
25
29
that is, the state of our application is a single integer.In a real world application, the model will probably be something more interesting -- Flame makes no assumption about how it is structured.
26
30
27
31
With our model type declared, we can define the initial state of the application
32
+
28
33
```haskell
29
34
init::Model
30
35
init=0
31
36
```
37
+
32
38
The first time the application is rendered, Flame calls the view function with `init`.
33
39
34
40
### Application markup
35
41
36
-
The `view` function maps the current state to markup. Whenever the model is updated, flame patches the DOM by calling `view` with the new state.
42
+
The `view` function maps the current state to markup. Whenever the model is updated, Flame patches the DOM by calling `view` with the new state.
37
43
38
44
In the counter example, the view is defined as
45
+
39
46
```haskell
40
47
view::Model->HtmlMessage
41
48
view model =HE.main "main" [
@@ -44,17 +51,21 @@ view model = HE.main "main" [
44
51
HE.button [HA.onClick Increment] "+"
45
52
]
46
53
```
54
+
47
55
The `message`s raised on events are used to signal how the application state should be updated.
48
56
49
57
See [Defining views](views) for an in depth look at views.
50
58
51
59
### State updating
52
60
53
61
The `update` function handles events, returning an updated model. In a Flame application, we reify native events as a custom data type. In the counter example, we are interested in the following events:
62
+
54
63
```haskell
55
64
dataMessage=Increment | Decrement
56
65
```
66
+
57
67
and thus our update function looks like
68
+
58
69
```haskell
59
70
update::Model->Message->Model
60
71
update model =case _ of
@@ -64,31 +75,34 @@ update model = case _ of
64
75
65
76
See [Handling events](events) for an in depth look at update strategies.
66
77
67
-
### External event handling
78
+
### Subscriptions
79
+
80
+
Finally, we can specify events that come from outside of the `view` as an array to `subscribe`. Such events include `window`, `document` -- and even messages arbitrarily raised by user code. These messages will then be handled in the usual way by the `update` function.
68
81
69
-
Finally, mounting a Flame application record yields a [`Channel`](https://pursuit.purescript.org/packages/purescript-signal/10.1.0/docs/Signal.Channel), which can be fed messages from events outside of our view. This includes `window` or `document` events, such as resize or load, and custom events, all of which are then handled as usual by the application `update` function.
82
+
In the counter example no external events are handled, so the subscription list is empty
70
83
71
-
In the counter example, no external events are handled, so we use the version of `mount` that discards the channel
72
84
```haskell
73
-
main::EffectUnit
74
-
main =FAN.mount_ "main" {
75
-
...
85
+
subscribe::Array (SubscriptionMessage)
86
+
subscribe =[]
76
87
}
77
88
```
78
89
79
-
See [Handling external events](events#handling-external-events) for an in depth look at external events.
90
+
See [Handling external events](events#handling-external-events) for an in depth look at subscriptions.
80
91
81
92
### Rendering
82
93
83
94
Having all pieces put together, we can either render the application to the DOM, as in the case of the counter example
95
+
84
96
```haskell
85
97
main::EffectUnit
86
98
main =FAN.mount_ (QuerySelector"body") {
87
99
init,
100
+
view,
88
101
update,
89
-
view
102
+
subscribe
90
103
}
91
104
```
105
+
92
106
or as a `String` with `Flame.Renderer.String.render`, which can be used server-side.
93
107
94
108
See [Rendering the app](rendering) for an in depth look at rendering.
0 commit comments