Packages
lustre
3.0.0-rc.4
5.7.1
5.7.0
5.6.0
5.5.2
5.5.1
5.5.0
5.4.0
5.3.5
5.3.4
5.3.3
5.3.2
5.3.1
5.3.0
5.2.1
5.2.0
5.1.1
5.1.0
5.0.3
5.0.2
5.0.1
5.0.0
4.6.4
4.6.3
4.6.2
4.6.1
4.6.0
4.5.1
4.5.0
4.4.4
4.4.3
4.4.1
4.4.0
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.8
4.1.7
4.1.6
4.1.5
4.1.4
4.1.3
4.1.2
4.1.1
4.1.0
4.0.0
4.0.0-rc1
4.0.0-rc.2
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.12
3.0.11
3.0.10
3.0.9
3.0.8
3.0.7
3.0.6
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
3.0.0-rc.8
3.0.0-rc.7
3.0.0-rc.6
3.0.0-rc.5
3.0.0-rc.4
3.0.0-rc.3
3.0.0-rc.2
3.0.0-rc.1
2.0.1
2.0.0
1.3.0
1.2.0
1.1.0
1.0.0
Create HTML templates, single page applications, Web Components, and real-time server components in Gleam!
Current section
Files
Jump to
Current section
Files
src/lustre.ffi.mjs
import { innerHTML, createTree } from "./runtime.ffi.mjs";
import { Ok, Error, List } from "./gleam.mjs";
import {
Some,
None,
map as option_map,
} from "../gleam_stdlib/gleam/option.mjs";
// RUNTIME ---------------------------------------------------------------------
///
///
export class App {
#root = null;
#state = null;
#queue = [];
#commands = [];
#willUpdate = false;
#didUpdate = false;
// These are the three functions that the user provides to the runtime.
#__init;
#__update;
#__render;
constructor(init, update, render) {
this.#__init = init;
this.#__update = update;
this.#__render = render;
}
start(selector = "body") {
if (this.#root) return this;
try {
this.#root = document.querySelector(selector);
} catch (_) {
return new Error(undefined);
}
const [next, cmds] = this.#__init();
this.#state = next;
this.#commands = cmds[0].toArray();
this.#didUpdate = true;
window.requestAnimationFrame(() => this.#tick());
return new Ok((msg) => this.dispatch(msg));
}
dispatch(msg) {
if (!this.#willUpdate) window.requestAnimationFrame(() => this.#tick());
this.#queue.push(msg);
this.#willUpdate = true;
}
#render() {
const node = this.#__render(this.#state);
const tree = createTree(map(node, (msg) => this.dispatch(msg)));
innerHTML(this.#root, tree);
}
#tick() {
this.#flush();
this.#didUpdate && this.#render();
this.#willUpdate = false;
}
#flush(times = 0) {
if (this.#queue.length) {
while (this.#queue.length) {
const [next, cmds] = this.#__update(this.#state, this.#queue.shift());
this.#state = next;
this.#commands.concat(cmds[0].toArray());
}
this.#didUpdate = true;
}
// Each update can produce commands which must now be executed.
while (this.#commands.length) this.#commands.shift()(this.dispatch);
// Synchronous commands 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 = "body") => app.start(selector);
// VDOM ------------------------------------------------------------------------
export const node = (tag, attrs, children) =>
createTree(tag, Object.fromEntries(attrs.toArray()), children.toArray());
export const text = (content) => content;
export const attr = (key, value) => {
if (value instanceof List) return [key, value.toArray()];
if (value instanceof Some) return [key, value[0]];
if (value instanceof None) return [key, undefined];
return [key, value];
};
export const on = (event, handler) => [`on${event}`, handler];
export const map = (node, f) => ({
...node,
attributes: Object.entries(node.attributes).reduce((attrs, [key, value]) => {
// It's safe to mutate the `attrs` object here because we created it at
// the start of the reduce: it's not shared with any other code.
// If the attribute is an event handler, wrap it in a function that
// transforms
if (key.startsWith("on") && typeof value === "function") {
attrs[key] = (e) => option_map(value(e), f);
} else {
attrs[key] = value;
}
return attrs;
}, {}),
childNodes: node.childNodes.map((child) => map(child, f)),
});
export const styles = (list) => Object.fromEntries(list.toArray());