diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-09 14:29:18 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-09 14:32:23 +0200 |
| commit | 812f547d84b5ce1a2924ae16f6257e4fd19efcc0 (patch) | |
| tree | a7d9a931de0e93a23b1613425aa2db3c67980700 /src/zeroscript/Component.zig | |
| parent | 32a251aa9645de070727a6ec91490d3e9219ec7c (diff) | |
zeroscript: make component constructor and destructor optional
Diffstat (limited to 'src/zeroscript/Component.zig')
| -rw-r--r-- | src/zeroscript/Component.zig | 44 |
1 files changed, 25 insertions, 19 deletions
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 }, + ), )}, ), }; |