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
|
import {
AppAlreadyStarted,
AppNotYetStarted,
BadComponentName,
ComponentAlreadyRegistered,
ElementNotFound,
NotABrowser,
} from "./lustre.mjs";
import { from } from "./lustre/effect.mjs";
import { morph } from "./runtime.ffi.mjs";
import { Ok, Error, isEqual } from "./gleam.mjs";
// RUNTIME ---------------------------------------------------------------------
///
///
export class App {
#root = null;
#state = null;
#queue = [];
#effects = [];
#didUpdate = false;
#init = null;
#update = null;
#view = null;
constructor(init, update, render) {
this.#init = init;
this.#update = update;
this.#view = render;
}
start(selector, flags) {
if (!is_browser()) return new Error(new NotABrowser());
if (this.#root) return new Error(new AppAlreadyStarted());
this.#root =
selector instanceof HTMLElement
? selector
: document.querySelector(selector);
if (!this.#root) return new Error(new ElementNotFound());
const [next, effects] = this.#init(flags);
this.#state = next;
this.#effects = effects[0].toArray();
this.#didUpdate = true;
window.requestAnimationFrame(() => this.#tick());
return new Ok((msg) => this.dispatch(msg));
}
dispatch(msg) {
this.#queue.push(msg);
this.#tick();
}
emit(name, event = null) {
this.#root.dispatchEvent(
new CustomEvent(name, {
bubbles: true,
detail: event,
composed: true,
})
);
}
destroy() {
if (!this.#root) return new Error(new AppNotYetStarted());
this.#root.remove();
this.#root = null;
this.#state = null;
this.#queue = [];
this.#effects = [];
this.#didUpdate = false;
this.#update = () => {};
this.#view = () => {};
}
#tick() {
this.#flush();
if (this.#didUpdate) {
const vdom = this.#view(this.#state);
this.#root = morph(this.#root, vdom, (msg) => this.dispatch(msg));
this.#didUpdate = false;
}
}
#flush(times = 0) {
if (!this.#root) return;
if (this.#queue.length) {
while (this.#queue.length) {
const [next, effects] = this.#update(this.#state, this.#queue.shift());
// If the user returned their model unchanged and not reconstructed then
// we don't need to trigger a re-render.
this.#didUpdate ||= this.#state !== next;
this.#state = next;
this.#effects = this.#effects.concat(effects[0].toArray());
}
}
// Each update can produce effects which must now be executed.
while (this.#effects.length)
this.#effects.shift()(
(msg) => this.dispatch(msg),
(name, data) => this.emit(name, data)
);
// Synchronous effects will immediately queue a message to be processed. If
// it is reasonable, we can process those updates too before proceeding to
// the next render.
if (this.#queue.length) {
times >= 5 ? console.warn(tooManyUpdates) : this.#flush(++times);
}
}
}
export const setup = (init, update, render) => new App(init, update, render);
export const start = (app, selector, flags) => app.start(selector, flags);
export const destroy = (app) => app.destroy();
export const emit = (name, data) =>
// Normal `Effect`s constructed in Gleam from `effect.from` don't get told
// about the second argument, but it's there 👀.
from((_, emit) => {
emit(name, data);
});
// HTML EVENTS -----------------------------------------------------------------
export const prevent_default = (e) => e.preventDefault?.();
export const stop_propagation = (e) => e.stopPropagation?.();
// CUSTOM ELEMENTS -------------------------------------------------------------
export const setup_component = (
name,
init,
update,
render,
on_attribute_change
) => {
if (!name.includes("-")) return new Error(new BadComponentName());
if (!is_browser()) return new Error(new NotABrowser());
if (customElements.get(name)) {
return new Error(new ComponentAlreadyRegistered());
}
customElements.define(
name,
class extends HTMLElement {
static get observedAttributes() {
return on_attribute_change.entries().map(([name, _]) => name);
}
#container = document.createElement("div");
#app = null;
#dispatch = null;
constructor() {
super();
this.#app = new App(init, update, render);
// This is necessary for ✨ reasons ✨. Clearly there's a bug in the
// implementation of either the `App` or the runtime but I con't work it
// out.
//
// If we pass the container to the app directly then the component fails
// to render anything to the ODM.
this.#container.appendChild(document.createElement("div"));
const dispatch = this.#app.start(this.#container.firstChild);
this.#dispatch = dispatch[0];
on_attribute_change.forEach((decoder, name) => {
Object.defineProperty(this, name, {
get: () => {
return this[`_${name}`] || this.getAttribute(name);
},
set: (value) => {
const prev = this[name];
const decoded = decoder(value);
// We need this equality check to prevent constantly dispatching
// messages when the value is an object or array: it might not have
// changed but its reference might have and we don't want to trigger
// useless updates.
if (decoded.isOk() && !isEqual(prev, value)) {
this.#dispatch(decoded[0]);
}
if (typeof value === "string") {
this.setAttribute(name, value);
} else {
this[`_${name}`] = value;
}
},
});
});
}
connectedCallback() {
this.appendChild(this.#container.firstChild);
}
attributeChangedCallback(name, prev, next) {
if (prev !== next) {
this[name] = next;
}
}
disconnectedCallback() {
this.#app.destroy();
}
}
);
return new Ok(null);
};
// UTLS ------------------------------------------------------------------------
export const is_browser = () => window && window.document;
export const is_registered = (name) => !!customElements.get(name);
|