aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-09 14:29:18 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-09 14:32:23 +0200
commit812f547d84b5ce1a2924ae16f6257e4fd19efcc0 (patch)
treea7d9a931de0e93a23b1613425aa2db3c67980700 /src
parent32a251aa9645de070727a6ec91490d3e9219ec7c (diff)
zeroscript: make component constructor and destructor optional
Diffstat (limited to 'src')
-rw-r--r--src/app/components/icons/root.zig6
-rw-r--r--src/app/components/navbutton/root.zig9
-rw-r--r--src/zeroscript/Component.zig44
-rw-r--r--src/zeroscript/component.template.js21
4 files changed, 56 insertions, 24 deletions
diff --git a/src/app/components/icons/root.zig b/src/app/components/icons/root.zig
index 298cfa6..6f7ab4a 100644
--- a/src/app/components/icons/root.zig
+++ b/src/app/components/icons/root.zig
@@ -6,16 +6,12 @@ pub const icons: []const zs.Component = &.{
icon("settings"),
};
-const Icon = struct {
- pub fn init(_: *@This(), _: *zs.Object) void {}
-};
-
pub fn icon(name: []const u8) zs.Component {
return .{
.namespace = "icon",
.name = name,
.html = .parse(@embedFile(name ++ ".svg")),
.css = @embedFile("index.css"),
- .link = Icon,
+ .link = struct {},
};
}
diff --git a/src/app/components/navbutton/root.zig b/src/app/components/navbutton/root.zig
index 7704b90..b76af43 100644
--- a/src/app/components/navbutton/root.zig
+++ b/src/app/components/navbutton/root.zig
@@ -27,6 +27,15 @@ pub fn init(self: *Self, element: *zs.Object) !void {
self.button.classList = try button.get(&env, "classList").await(&env, .object);
}
+pub fn deinit(self: *Self) !void {
+ var env: zs.Env = .default;
+ defer env.deinit();
+
+ _ = self.element.free(&env);
+ _ = self.shadow_root.free(&env);
+ _ = self.button.classList.free(&env);
+}
+
pub fn open(self: *Self) !void {
var env: zs.Env = .default;
defer env.deinit();
diff --git a/src/zeroscript/Component.zig b/src/zeroscript/Component.zig
index 2f92b70..35fafbe 100644
--- a/src/zeroscript/Component.zig
+++ b/src/zeroscript/Component.zig
@@ -76,7 +76,9 @@ pub fn exportLinks(self: *const Self) void {
const init = struct {
pub fn init(element: *Object) callconv(.c) ?*self.link {
const obj = std.heap.wasm_allocator.create(self.link) catch return null;
- wrapHandler(obj.init(element));
+ if (@hasDecl(self.link, "init")) {
+ wrapHandler(obj.init(element));
+ }
return obj;
}
}.init;
@@ -91,6 +93,25 @@ pub fn exportLinks(self: *const Self) void {
},
);
+ const deinit = struct {
+ pub fn deinit(obj: *self.link) callconv(.c) void {
+ if (@hasDecl(self.link, "deinit")) {
+ wrapHandler(obj.deinit());
+ }
+ std.heap.wasm_allocator.destroy(obj);
+ }
+ }.deinit;
+
+ @export(
+ &deinit,
+ .{
+ .name = std.fmt.comptimePrint(
+ "export::{s}::deinit",
+ .{self.name},
+ ),
+ },
+ );
+
for (self.html.elements) |element| {
comptime self.exportFromHtml(element);
}
@@ -166,24 +187,9 @@ pub fn toElement(self: *const Self) []const Element {
.{},
&.{.text(
std.fmt.comptimePrint(
- \\customElements.define("{s}-{s}", class extends HTMLElement {{
- \\ constructor() {{
- \\ super();
- \\ this.attachShadow({{ mode: 'open' }});
- \\ const template = document.getElementById("@{s}");
- \\ const node = document.importNode(template.content, true);
- \\ this.shadowRoot.appendChild(node);
- \\ this[ZsPtr] = null;
- \\ }}
- \\ connectedCallback() {{
- \\ wasm_ready.then(() => {{
- \\ const id = ObjectStore.generateFreeId();
- \\ ObjectStore.registerObject(id, this);
- \\ this[ZsPtr] = wasm.exports['export::{s}::init'](id)
- \\ }});
- \\ }}
- \\}})
- , .{ self.namespace, self.name, self.name, self.name }),
+ @embedFile("component.template.js"),
+ .{ self.namespace, self.name, self.name, self.name, self.name },
+ ),
)},
),
};
diff --git a/src/zeroscript/component.template.js b/src/zeroscript/component.template.js
new file mode 100644
index 0000000..e8cb914
--- /dev/null
+++ b/src/zeroscript/component.template.js
@@ -0,0 +1,21 @@
+customElements.define("{s}-{s}", class extends HTMLElement {{
+ constructor() {{
+ super();
+ this.attachShadow({{ mode: 'open' }});
+ const template = document.getElementById("@{s}");
+ const node = document.importNode(template.content, true);
+ this.shadowRoot.appendChild(node);
+ this[ZsPtr] = null;
+ }}
+ connectedCallback() {{
+ wasm_ready.then(() => {{
+ const id = ObjectStore.generateFreeId();
+ ObjectStore.registerObject(id, this);
+ this[ZsPtr] = wasm.exports['export::{s}::init'](id);
+ }});
+ }}
+ disconnectedCallback() {{
+ wasm.exports['export::{s}::deinit'](this[ZsId]);
+ ObjectStore.free(this);
+ }}
+}})