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
|
//// To read the full documentation for this module, please visit
//// [https://lustre.build/api/lustre](https://lustre.build/api/lustre)
// IMPORTS ---------------------------------------------------------------------
import gleam/dynamic.{Decoder}
import gleam/map.{Map}
import lustre/effect.{Effect}
import lustre/element.{Element}
// TYPES -----------------------------------------------------------------------
@target(javascript)
///
pub type App(flags, model, msg)
@target(erlang)
///
pub opaque type App(flags, model, msg) {
App
}
pub type Error {
AppAlreadyStarted
AppNotYetStarted
BadComponentName
ComponentAlreadyRegistered
ElementNotFound
NotABrowser
}
// CONSTRUCTORS ----------------------------------------------------------------
///
pub fn element(element: Element(msg)) -> App(Nil, Nil, msg) {
let init = fn(_) { #(Nil, effect.none()) }
let update = fn(_, _) { #(Nil, effect.none()) }
let view = fn(_) { element }
application(init, update, view)
}
///
pub fn simple(
init: fn(flags) -> model,
update: fn(model, msg) -> model,
view: fn(model) -> Element(msg),
) -> App(flags, model, msg) {
let init = fn(flags) { #(init(flags), effect.none()) }
let update = fn(model, msg) { #(update(model, msg), effect.none()) }
application(init, update, view)
}
///
@external(javascript, "./lustre.ffi.mjs", "setup")
pub fn application(
_init: fn(flags) -> #(model, Effect(msg)),
_update: fn(model, msg) -> #(model, Effect(msg)),
_view: fn(model) -> Element(msg),
) -> App(flags, model, msg) {
// Applications are not usable on the erlang target. For those users, `App`
// is an opaque type (aka they can't see its structure) and functions like
// `start` and `destroy` are no-ops.
//
// Because the constructor is marked as `@target(erlang)` for some reason we
// can't simply refer to it here even though the compiler should know that the
// body of this function can only be entered from erlang (because we have an
// external def for javascript) but alas, it does not.
//
// So instead, we must do this awful hack and cast a `Nil` to the `App` type
// to make everything happy. Theoeretically this is not going to be a problem
// unless someone starts poking around with their own ffi and at that point
// they deserve it.
dynamic.unsafe_coerce(dynamic.from(Nil))
}
@external(javascript, "./lustre.ffi.mjs", "setup_component")
pub fn component(
_name: String,
_init: fn() -> #(model, Effect(msg)),
_update: fn(model, msg) -> #(model, Effect(msg)),
_view: fn(model) -> Element(msg),
_on_attribute_change: Map(String, Decoder(msg)),
) -> Result(Nil, Error) {
Ok(Nil)
}
// EFFECTS ---------------------------------------------------------------------
///
@external(javascript, "./lustre.ffi.mjs", "start")
pub fn start(
_app: App(flags, model, msg),
_selector: String,
_flags: flags,
) -> Result(fn(msg) -> Nil, Error) {
Error(NotABrowser)
}
///
@external(javascript, "./lustre.ffi.mjs", "destroy")
pub fn destroy(_app: App(flags, model, msg)) -> Result(Nil, Error) {
Ok(Nil)
}
// UTILS -----------------------------------------------------------------------
///
@external(javascript, "./lustre.ffi.mjs", "is_browser")
pub fn is_browser() -> Bool {
False
}
///
@external(javascript, "./lustre.ffi.mjs", "is_registered")
pub fn is_registered(_name: String) -> Bool {
False
}
|