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
|
// IMPORTS ---------------------------------------------------------------------
import gleam/dict.{type Dict}
import gleam/dynamic.{type Decoder, type Dynamic}
import gleam/erlang/process.{type Selector, type Subject}
import gleam/function.{identity}
import gleam/list
import gleam/json.{type Json}
import gleam/option.{Some}
import gleam/otp/actor.{type Next, type StartError, Spec}
import gleam/result
import lustre/effect.{type Effect}
import lustre/element.{type Element, type Patch}
import lustre/internals/patch.{Diff, Init}
import lustre/internals/vdom
// TYPES -----------------------------------------------------------------------
///
///
type State(runtime, model, msg) {
State(
self: Subject(Action(runtime, msg)),
model: model,
update: fn(model, msg) -> #(model, Effect(msg)),
view: fn(model) -> Element(msg),
html: Element(msg),
renderers: Dict(Dynamic, fn(Patch(msg)) -> Nil),
handlers: Dict(String, fn(Dynamic) -> Result(msg, Nil)),
on_attribute_change: Dict(String, Decoder(msg)),
)
}
///
///
pub type Action(runtime, msg) {
AddRenderer(Dynamic, fn(Patch(msg)) -> Nil)
Attrs(List(#(String, Dynamic)))
Batch(List(msg), Effect(msg))
Debug(DebugAction)
Dispatch(msg)
Emit(String, Json)
Event(String, Dynamic)
RemoveRenderer(Dynamic)
SetSelector(Selector(Action(runtime, msg)))
Shutdown
}
pub type DebugAction {
Model(reply: fn(Dynamic) -> Nil)
View(reply: fn(Element(Dynamic)) -> Nil)
}
// ACTOR -----------------------------------------------------------------------
@target(erlang)
///
///
pub fn start(
init: #(model, Effect(msg)),
update: fn(model, msg) -> #(model, Effect(msg)),
view: fn(model) -> Element(msg),
on_attribute_change: Dict(String, Decoder(msg)),
) -> Result(Subject(Action(runtime, msg)), StartError) {
let timeout = 1000
let init = fn() {
let self = process.new_subject()
let html = view(init.0)
let handlers = vdom.handlers(html)
let state =
State(
self,
init.0,
update,
view,
html,
dict.new(),
handlers,
on_attribute_change,
)
let selector = process.selecting(process.new_selector(), self, identity)
run_effects(init.1, self)
actor.Ready(state, selector)
}
actor.start_spec(Spec(init, timeout, loop))
}
@target(erlang)
fn loop(
message: Action(runtime, msg),
state: State(runtime, model, msg),
) -> Next(Action(runtime, msg), State(runtime, model, msg)) {
case message {
Attrs(attrs) -> {
list.filter_map(attrs, fn(attr) {
case dict.get(state.on_attribute_change, attr.0) {
Error(_) -> Error(Nil)
Ok(decoder) ->
decoder(attr.1)
|> result.replace_error(Nil)
}
})
|> Batch(effect.none())
|> loop(state)
}
AddRenderer(id, renderer) -> {
let renderers = dict.insert(state.renderers, id, renderer)
let next = State(..state, renderers: renderers)
renderer(Init(dict.keys(state.on_attribute_change), state.html))
actor.continue(next)
}
Batch([], _) -> actor.continue(state)
Batch([msg], other_effects) -> {
let #(model, effects) = state.update(state.model, msg)
let html = state.view(model)
let diff = patch.elements(state.html, html)
let next =
State(..state, model: model, html: html, handlers: diff.handlers)
run_effects(effect.batch([effects, other_effects]), state.self)
case patch.is_empty_element_diff(diff) {
True -> Nil
False -> run_renderers(state.renderers, Diff(diff))
}
actor.continue(next)
}
Batch([msg, ..rest], other_effects) -> {
let #(model, effects) = state.update(state.model, msg)
let html = state.view(model)
let diff = patch.elements(state.html, html)
let next =
State(..state, model: model, html: html, handlers: diff.handlers)
loop(Batch(rest, effect.batch([effects, other_effects])), next)
}
Debug(Model(reply)) -> {
reply(dynamic.from(state.model))
actor.continue(state)
}
Debug(View(reply)) -> {
reply(element.map(state.html, dynamic.from))
actor.continue(state)
}
Dispatch(msg) -> {
let #(model, effects) = state.update(state.model, msg)
let html = state.view(model)
let diff = patch.elements(state.html, html)
let next =
State(..state, model: model, html: html, handlers: diff.handlers)
run_effects(effects, state.self)
case patch.is_empty_element_diff(diff) {
True -> Nil
False -> run_renderers(state.renderers, Diff(diff))
}
actor.continue(next)
}
Emit(name, event) -> {
let patch = patch.Emit(name, event)
run_renderers(state.renderers, patch)
actor.continue(state)
}
Event(name, event) -> {
case dict.get(state.handlers, name) {
Error(_) -> actor.continue(state)
Ok(handler) -> {
handler(event)
|> result.map(Dispatch)
|> result.map(actor.send(state.self, _))
|> result.unwrap(Nil)
actor.continue(state)
}
}
}
RemoveRenderer(id) -> {
let renderers = dict.delete(state.renderers, id)
let next = State(..state, renderers: renderers)
actor.continue(next)
}
SetSelector(selector) -> actor.Continue(state, Some(selector))
Shutdown -> actor.Stop(process.Killed)
}
}
// UTILS -----------------------------------------------------------------------
@target(erlang)
fn run_renderers(
renderers: Dict(any, fn(Patch(msg)) -> Nil),
patch: Patch(msg),
) -> Nil {
use _, _, renderer <- dict.fold(renderers, Nil)
renderer(patch)
}
@target(erlang)
fn run_effects(effects: Effect(msg), self: Subject(Action(runtime, msg))) -> Nil {
let dispatch = fn(msg) { actor.send(self, Dispatch(msg)) }
let emit = fn(name, event) { actor.send(self, Emit(name, event)) }
effect.perform(effects, dispatch, emit)
}
// Empty implementations of every function in this module are required because we
// need to be able to build the codebase *locally* with the JavaScript target to
// bundle the server component runtime.
//
// For *consumers* of Lustre this is not a problem, Gleam will see this module is
// never included in any path reachable from JavaScript but when we're *inside the
// package* Gleam has no idea that is the case.
@target(javascript)
pub fn start(
init: #(model, Effect(msg)),
update: fn(model, msg) -> #(model, Effect(msg)),
view: fn(model) -> Element(msg),
on_attribute_change: Dict(String, Decoder(msg)),
) -> Result(Subject(Action(runtime, msg)), StartError) {
panic
}
@target(javascript)
fn loop(
message: Action(runtime, msg),
state: State(runtime, model, msg),
) -> Next(Action(runtime, msg), State(runtime, model, msg)) {
panic
}
@target(javascript)
fn run_renderers(
renderers: Dict(any, fn(Patch(msg)) -> Nil),
patch: Patch(msg),
) -> Nil {
panic
}
@target(javascript)
fn run_effects(effects: Effect(msg), self: Subject(Action(runtime, msg))) -> Nil {
panic
}
|