diff options
Diffstat (limited to 'src/zeroscript')
| -rw-r--r-- | src/zeroscript/Component.zig | 44 | ||||
| -rw-r--r-- | src/zeroscript/component.template.js | 21 |
2 files changed, 46 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 }, + ), )}, ), }; 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); + }} +}}) |