aboutsummaryrefslogtreecommitdiff
path: root/src/client-component.ffi.mjs
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2024-01-30 20:18:47 +0000
committerHayleigh Thompson <me@hayleigh.dev>2024-01-30 20:18:47 +0000
commit97a393fa1c9a342183ceaede9cd07564816bacce (patch)
tree09be60bac826ab1973f6577daf22befb694ea9fa /src/client-component.ffi.mjs
parentdcad7a49d0fa9d17f5e8c8e7677548be2967f364 (diff)
downloadlustre-97a393fa1c9a342183ceaede9cd07564816bacce.tar.gz
lustre-97a393fa1c9a342183ceaede9cd07564816bacce.zip
:bug: Fixed bug where ffi code incorrectly imported runtime.
Diffstat (limited to 'src/client-component.ffi.mjs')
-rw-r--r--src/client-component.ffi.mjs98
1 files changed, 51 insertions, 47 deletions
diff --git a/src/client-component.ffi.mjs b/src/client-component.ffi.mjs
index 0970e7f..c64527f 100644
--- a/src/client-component.ffi.mjs
+++ b/src/client-component.ffi.mjs
@@ -1,5 +1,5 @@
import { Ok, Error, isEqual } from "./gleam.mjs";
-import { Dispatch, Shutdown } from "./lustre/runtime.mjs";
+import { Dispatch, Shutdown } from "./lustre/internals/runtime.mjs";
import {
ComponentAlreadyRegistered,
BadComponentName,
@@ -16,59 +16,63 @@ export function register({ init, update, view, on_attribute_change }, name) {
window.customElements.define(
name,
- class LustreClientComponent extends HTMLElement {
- #root = document.createElement("div");
- #application = null;
+ makeComponent(init, update, view, on_attribute_change),
+ );
- static get observedAttributes() {
- return on_attribute_change.entries().map(([name, _]) => name);
- }
+ return new Ok(undefined);
+}
- constructor() {
- super();
- on_attribute_change.forEach((decoder, name) => {
- Object.defineProperty(this, name, {
- get() {
- return this[`_${name}`] || this.getAttribute(name);
- },
+function makeComponent(init, update, view, on_attribute_change) {
+ return class LustreClientComponent extends HTMLElement {
+ #root = document.createElement("div");
+ #application = null;
- set(value) {
- const prev = this[name];
- const decoded = decoder(value);
+ static get observedAttributes() {
+ return on_attribute_change.entries().map(([name, _]) => name);
+ }
- if (decoded.isOk() && !isEqual(prev, value)) {
- this.#application
- ? this.#application.send(new Dispatch(decoded[0]))
- : window.requestAnimationFrame(() =>
- this.#application.send(new Dispatch(decoded[0]))
- );
- }
+ constructor() {
+ super();
+ on_attribute_change.forEach((decoder, name) => {
+ Object.defineProperty(this, name, {
+ get() {
+ return this[`_${name}`] || this.getAttribute(name);
+ },
- if (typeof value === "string") {
- this.setAttribute(name, value);
- } else {
- this[`_${name}`] = value;
- }
- },
- });
- });
- }
+ set(value) {
+ const prev = this[name];
+ const decoded = decoder(value);
- connectedCallback() {
- this.#application = new LustreClientApplication(
- init(),
- update,
- view,
- this.#root
- );
- this.appendChild(this.#root);
- }
+ if (decoded.isOk() && !isEqual(prev, value)) {
+ this.#application
+ ? this.#application.send(new Dispatch(decoded[0]))
+ : window.requestAnimationFrame(() =>
+ this.#application.send(new Dispatch(decoded[0])),
+ );
+ }
- disconnectedCallback() {
- this.#application.send(new Shutdown());
- }
+ if (typeof value === "string") {
+ this.setAttribute(name, value);
+ } else {
+ this[`_${name}`] = value;
+ }
+ },
+ });
+ });
+ }
+
+ connectedCallback() {
+ this.#application = new LustreClientApplication(
+ init(),
+ update,
+ view,
+ this.#root,
+ );
+ this.appendChild(this.#root);
}
- );
- return new Ok(null);
+ disconnectedCallback() {
+ this.#application.send(new Shutdown());
+ }
+ };
}