ziex 0.1.0-dev.517 → 0.1.0-dev.522

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // src/index.ts
2
2
  var zx = {
3
3
  name: "zx",
4
- version: "0.1.0-dev.517",
4
+ version: "0.1.0-dev.522",
5
5
  description: "ZX is a framework for building web applications with Zig.",
6
6
  repository: "https://github.com/nurulhudaapon/zx",
7
7
  fingerprint: 14616285862371232000,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ziex",
3
- "version": "0.1.0-dev.517",
3
+ "version": "0.1.0-dev.522",
4
4
  "description": "ZX is a framework for building web applications with Zig.",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/wasm/index.d.ts CHANGED
@@ -1,40 +1,52 @@
1
1
  import { ZigJS } from "../../../../vendor/jsz/js/src";
2
+ /**
3
+ * ZX Client Bridge - Unified JS↔WASM communication layer
4
+ * Handles events, fetch, timers, and other async callbacks using jsz
5
+ */
6
+ export declare const CallbackType: {
7
+ readonly Event: 0;
8
+ readonly FetchSuccess: 1;
9
+ readonly FetchError: 2;
10
+ readonly Timeout: 3;
11
+ readonly Interval: 4;
12
+ };
13
+ export type CallbackTypeValue = typeof CallbackType[keyof typeof CallbackType];
2
14
  export declare const jsz: ZigJS;
3
- declare class ZXInstance {
15
+ /** Store a value using jsz.storeValue and get the 64-bit reference. */
16
+ export declare function storeValueGetRef(val: any): bigint;
17
+ /** ZX Bridge - provides JS APIs that callback into WASM */
18
+ export declare class ZxBridge {
4
19
  #private;
5
- exports: WebAssembly.Exports;
6
- events: Event[];
7
- constructor({ exports, events }: ZXInstanceOptions);
8
- addEvent(event: Event): number;
9
- /**
10
- * Initialize event delegation on a root element
11
- * This attaches a single event listener for each event type at the root,
12
- * and uses __zx_ref to look up the corresponding VElement in WASM
13
- */
14
- initEventDelegation(rootSelector?: string): void;
15
- getZxRef(element: HTMLElement): number | undefined;
20
+ constructor(exports: WebAssembly.Exports);
21
+ /** Fetch a URL and callback with the response */
22
+ fetch(urlPtr: number, urlLen: number, callbackId: bigint): void;
23
+ /** Set a timeout and callback when it fires */
24
+ setTimeout(callbackId: bigint, delayMs: number): void;
25
+ /** Set an interval and callback each time it fires */
26
+ setInterval(callbackId: bigint, intervalMs: number): void;
27
+ /** Clear an interval */
28
+ clearInterval(callbackId: bigint): void;
29
+ /** Handle a DOM event (called by event delegation) */
30
+ eventbridge(velementId: bigint, eventTypeId: number, event: Event): void;
31
+ /** Create the import object for WASM instantiation */
32
+ static createImportObject(bridgeRef: {
33
+ current: ZxBridge | null;
34
+ }): WebAssembly.Imports;
16
35
  }
17
- export declare function init(options?: InitOptions): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
36
+ /** Initialize event delegation */
37
+ export declare function initEventDelegation(bridge: ZxBridge, rootSelector?: string): void;
18
38
  export type InitOptions = {
19
- /** URL to the WASM file (default: /assets/main.wasm) */
20
39
  url?: string;
21
- /** CSS selector for the event delegation root element (default: 'body') */
22
40
  eventDelegationRoot?: string;
23
41
  importObject?: WebAssembly.Imports;
24
42
  };
25
- type ZXInstanceOptions = {
26
- exports: ZXInstance['exports'];
27
- events?: ZXInstance['events'];
28
- };
43
+ /** Initialize WASM with the ZX Bridge */
44
+ export declare function init(options?: InitOptions): Promise<{
45
+ source: WebAssembly.WebAssemblyInstantiatedSource;
46
+ bridge: ZxBridge;
47
+ }>;
29
48
  declare global {
30
- interface Window {
31
- _zx: ZXInstance;
32
- }
33
49
  interface HTMLElement {
34
- /**
35
- * The VElement ID of the element
36
- */
37
50
  __zx_ref?: number;
38
51
  }
39
52
  }
40
- export {};
package/wasm/index.js CHANGED
@@ -168,8 +168,102 @@ class ZigJS {
168
168
  }
169
169
  }
170
170
  // src/wasm/index.ts
171
- var DEFAULT_URL = "/assets/main.wasm";
172
- var MAX_EVENTS = 100;
171
+ var CallbackType = {
172
+ Event: 0,
173
+ FetchSuccess: 1,
174
+ FetchError: 2,
175
+ Timeout: 3,
176
+ Interval: 4
177
+ };
178
+ var jsz = new ZigJS;
179
+ var tempRefBuffer = new ArrayBuffer(8);
180
+ var tempRefView = new DataView(tempRefBuffer);
181
+ function storeValueGetRef(val) {
182
+ const originalMemory = jsz.memory;
183
+ jsz.memory = { buffer: tempRefBuffer };
184
+ jsz.storeValue(0, val);
185
+ jsz.memory = originalMemory;
186
+ return tempRefView.getBigUint64(0, true);
187
+ }
188
+ function readString(ptr, len) {
189
+ const memory = new Uint8Array(jsz.memory.buffer);
190
+ return new TextDecoder().decode(memory.slice(ptr, ptr + len));
191
+ }
192
+
193
+ class ZxBridge {
194
+ #exports;
195
+ #nextCallbackId = BigInt(1);
196
+ #intervals = new Map;
197
+ constructor(exports) {
198
+ this.#exports = exports;
199
+ }
200
+ get #handler() {
201
+ return this.#exports.__zx_cb;
202
+ }
203
+ #getNextId() {
204
+ return this.#nextCallbackId++;
205
+ }
206
+ #invoke(type, id, data) {
207
+ const handler = this.#handler;
208
+ if (!handler) {
209
+ console.warn("__zx_cb not exported from WASM");
210
+ return;
211
+ }
212
+ const dataRef = storeValueGetRef(data);
213
+ handler(type, id, dataRef);
214
+ }
215
+ fetch(urlPtr, urlLen, callbackId) {
216
+ const url = readString(urlPtr, urlLen);
217
+ fetch(url).then((response) => response.text()).then((text) => {
218
+ this.#invoke(CallbackType.FetchSuccess, callbackId, text);
219
+ }).catch((error) => {
220
+ this.#invoke(CallbackType.FetchError, callbackId, error.message ?? "Fetch failed");
221
+ });
222
+ }
223
+ setTimeout(callbackId, delayMs) {
224
+ setTimeout(() => {
225
+ this.#invoke(CallbackType.Timeout, callbackId, null);
226
+ }, delayMs);
227
+ }
228
+ setInterval(callbackId, intervalMs) {
229
+ const handle = setInterval(() => {
230
+ this.#invoke(CallbackType.Interval, callbackId, null);
231
+ }, intervalMs);
232
+ this.#intervals.set(callbackId, handle);
233
+ }
234
+ clearInterval(callbackId) {
235
+ const handle = this.#intervals.get(callbackId);
236
+ if (handle !== undefined) {
237
+ clearInterval(handle);
238
+ this.#intervals.delete(callbackId);
239
+ }
240
+ }
241
+ eventbridge(velementId, eventTypeId, event) {
242
+ const eventRef = storeValueGetRef(event);
243
+ const eventbridge = this.#exports.__zx_eventbridge;
244
+ if (eventbridge)
245
+ eventbridge(velementId, eventTypeId, eventRef);
246
+ }
247
+ static createImportObject(bridgeRef) {
248
+ return {
249
+ ...jsz.importObject(),
250
+ __zx: {
251
+ _fetch: (urlPtr, urlLen, callbackId) => {
252
+ bridgeRef.current?.fetch(urlPtr, urlLen, callbackId);
253
+ },
254
+ _setTimeout: (callbackId, delayMs) => {
255
+ bridgeRef.current?.setTimeout(callbackId, delayMs);
256
+ },
257
+ _setInterval: (callbackId, intervalMs) => {
258
+ bridgeRef.current?.setInterval(callbackId, intervalMs);
259
+ },
260
+ _clearInterval: (callbackId) => {
261
+ bridgeRef.current?.clearInterval(callbackId);
262
+ }
263
+ }
264
+ };
265
+ }
266
+ }
173
267
  var DELEGATED_EVENTS = [
174
268
  "click",
175
269
  "dblclick",
@@ -212,71 +306,45 @@ var EVENT_TYPE_MAP = {
212
306
  touchmove: 17,
213
307
  scroll: 18
214
308
  };
215
- var jsz = new ZigJS;
216
- var importObject = {
217
- ...jsz.importObject()
218
- };
219
-
220
- class ZXInstance {
221
- exports;
222
- events;
223
- #eventDelegationInitialized = false;
224
- constructor({ exports, events = [] }) {
225
- this.exports = exports;
226
- this.events = events;
227
- }
228
- addEvent(event) {
229
- if (this.events.length >= MAX_EVENTS)
230
- this.events.length = 0;
231
- const idx = this.events.push(event);
232
- return idx - 1;
233
- }
234
- initEventDelegation(rootSelector = "body") {
235
- if (this.#eventDelegationInitialized)
236
- return;
237
- const root = document.querySelector(rootSelector);
238
- if (!root)
239
- return;
240
- for (const eventType of DELEGATED_EVENTS) {
241
- root.addEventListener(eventType, (event) => {
242
- this.#handleDelegatedEvent(eventType, event);
243
- }, { passive: eventType.startsWith("touch") || eventType === "scroll" });
244
- }
245
- this.#eventDelegationInitialized = true;
246
- }
247
- #handleDelegatedEvent(eventType, event) {
248
- let target = event.target;
249
- while (target && target !== document.body) {
250
- const zxRef = target.__zx_ref;
251
- if (zxRef !== undefined) {
252
- const eventId = this.addEvent(event);
253
- const handleEvent = this.exports.handleEvent;
254
- if (typeof handleEvent === "function") {
255
- const eventTypeId = EVENT_TYPE_MAP[eventType] ?? 0;
256
- handleEvent(BigInt(zxRef), eventTypeId, BigInt(eventId));
309
+ function initEventDelegation(bridge, rootSelector = "body") {
310
+ const root = document.querySelector(rootSelector);
311
+ if (!root)
312
+ return;
313
+ for (const eventType of DELEGATED_EVENTS) {
314
+ root.addEventListener(eventType, (event) => {
315
+ let target = event.target;
316
+ while (target && target !== document.body) {
317
+ const zxRef = target.__zx_ref;
318
+ if (zxRef !== undefined) {
319
+ bridge.eventbridge(BigInt(zxRef), EVENT_TYPE_MAP[eventType] ?? 0, event);
320
+ break;
257
321
  }
258
- break;
322
+ target = target.parentElement;
259
323
  }
260
- target = target.parentElement;
261
- }
262
- }
263
- getZxRef(element) {
264
- return element.__zx_ref;
324
+ }, { passive: eventType.startsWith("touch") || eventType === "scroll" });
265
325
  }
266
326
  }
327
+ var DEFAULT_URL = "/assets/main.wasm";
267
328
  async function init(options = {}) {
268
- const url = options?.url ?? DEFAULT_URL;
269
- const wasmInstiatedSource = await WebAssembly.instantiateStreaming(fetch(url), Object.assign({}, importObject, options.importObject));
270
- const { instance } = wasmInstiatedSource;
329
+ const url = options.url ?? DEFAULT_URL;
330
+ const bridgeRef = { current: null };
331
+ const importObject = Object.assign({}, ZxBridge.createImportObject(bridgeRef), options.importObject);
332
+ const source = await WebAssembly.instantiateStreaming(fetch(url), importObject);
333
+ const { instance } = source;
271
334
  jsz.memory = instance.exports.memory;
272
- window._zx = new ZXInstance({ exports: instance.exports });
273
- window._zx.initEventDelegation(options.eventDelegationRoot ?? "body");
335
+ const bridge = new ZxBridge(instance.exports);
336
+ bridgeRef.current = bridge;
337
+ initEventDelegation(bridge, options.eventDelegationRoot ?? "body");
274
338
  const main = instance.exports.mainClient;
275
339
  if (typeof main === "function")
276
340
  main();
277
- return wasmInstiatedSource;
341
+ return { source, bridge };
278
342
  }
279
343
  export {
344
+ storeValueGetRef,
280
345
  jsz,
281
- init
346
+ initEventDelegation,
347
+ init,
348
+ ZxBridge,
349
+ CallbackType
282
350
  };