aboutsummaryrefslogtreecommitdiff
path: root/src/lustre.gleam
blob: da0730bdca3d75bf85ea7b1460d7b80d0f152ea0 (plain)
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
////


// IMPORTS ---------------------------------------------------------------------

import lustre/element
import lustre/attribute


// TYPES -----------------------------------------------------------------------

///
pub opaque type Program(state, action) {
    Program(
        init: state,
        update: Update(state, action),
        render: Render(state, action)
    )
}


pub type Element(action)   = element.Element(action)
pub type Attribute(action) = attribute.Attribute(action)


// These types aren't exposed, but they're just here to try and shrink the type
// annotations for `Program` and `program` a little bit. When generating docs,
// Gleam automatically expands type aliases so this is purely for the benefit of
// those reading the source.
//
type Update(state, action) = fn (state, action) -> state
type Render(state, action) = fn (state) -> Element(action)


// CONSTRUCTORS ----------------------------------------------------------------

/// Create a basic lustre program that just renders some element on the page.
/// Note that this doesn't mean the content is static! With `element.stateful`
/// you can still create components with local state.
///
/// Basic lustre programs don't have any *global* application state and so the
/// plumbing is a lot simpler. If you find yourself passing state 
///
pub fn basic (element: Element(any)) -> Program(Nil, any) {
    let init   = Nil
    let update = fn (_, _) { Nil }
    let render = fn (_) { element }

    Program(init, update, render)
}

/// Create a more complex application mimicing TEA – the Elm architecture. We
/// start with some initial `state`, a function to update that state, and then
/// a render function to derive our program's view from that state.
///
/// 
pub fn application (init: state, update: Update(state, action), render: Render(state, action)) -> Program(state, action) {
    Program(init, update, render)
}


// EFFECTS ---------------------------------------------------------------------

///
pub external fn start (program: Program(state, action), selector: String) -> Nil
    = "./lustre/ffi.mjs" "mount"