@moltendb-web/core 0.1.0-alpha.14 → 0.1.0-alpha.21

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.
@@ -1,30 +1,39 @@
1
- // MoltenDB Web Worker — auto-generated by moltendb-wasm build script
2
- // Do not edit directly; edit moltendb-wasm/scripts/build.mjs instead.
3
1
  import init, { WorkerDb } from './moltendb.js';
4
2
 
5
- let db = null;
3
+ let db;
6
4
 
7
- self.onmessage = async (event) => {
8
- const { id, action, ...params } = event.data;
5
+ self.onmessage = async (e) => {
6
+ const { id, action, ...payload } = e.data;
9
7
 
10
- try {
11
- if (!db && action === 'init') {
12
- await init();
13
- db = await new WorkerDb(params.dbName || 'moltendb');
14
- self.postMessage({ id, result: { status: 'initialized' } });
15
- return;
16
- }
8
+ // --- Initialization Phase ---
9
+ if (action === 'init') {
10
+ try {
11
+ await init(payload.workerUrl);
12
+ db = await new WorkerDb(payload.dbName);
13
+
14
+ // THE NATIVE FEED: Listen to Rust and broadcast to the main thread
15
+ db.subscribe((eventStr) => {
16
+ try {
17
+ const eventData = JSON.parse(eventStr);
18
+ // Use type: 'event' so the transport knows it's an unsolicited broadcast
19
+ self.postMessage({ type: 'event', ...eventData });
20
+ } catch (err) {
21
+ console.error("[MoltenDB Worker] Failed to parse event", err);
22
+ }
23
+ });
17
24
 
18
- if (!db) {
19
- throw new Error('Database not initialized. Send an "init" message first.');
25
+ self.postMessage({ id, result: { status: 'ok' } });
26
+ } catch (error) {
27
+ self.postMessage({ id, error: String(error) });
20
28
  }
29
+ return;
30
+ }
21
31
 
22
- // Pass action + params but NOT id so the Rust validator does not see
23
- // 'id' as an unknown property.
24
- const result = db.handle_message({ action, ...params });
32
+ // --- Standard Request/Response Phase ---
33
+ try {
34
+ const result = db.handle_message({ action, ...payload });
25
35
  self.postMessage({ id, result });
26
-
27
36
  } catch (error) {
28
- self.postMessage({ id, error: error.toString() });
37
+ self.postMessage({ id, error: String(error) });
29
38
  }
30
- };
39
+ };
@@ -76,6 +76,12 @@ export class WorkerDb {
76
76
  * Each unique name is a separate database file in the browser's OPFS storage.
77
77
  */
78
78
  constructor(db_name: string);
79
+ /**
80
+ * Subscribe to real-time database changes.
81
+ * The provided JavaScript function will be called with a JSON string
82
+ * representing the mutation event.
83
+ */
84
+ subscribe(callback: Function): void;
79
85
  }
80
86
 
81
87
  export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
@@ -86,9 +92,10 @@ export interface InitOutput {
86
92
  readonly workerdb_analytics: (a: number, b: number, c: number, d: number) => void;
87
93
  readonly workerdb_handle_message: (a: number, b: number, c: number) => void;
88
94
  readonly workerdb_new: (a: number, b: number) => number;
89
- readonly __wasm_bindgen_func_elem_3598: (a: number, b: number) => void;
90
- readonly __wasm_bindgen_func_elem_3677: (a: number, b: number, c: number, d: number) => void;
91
- readonly __wasm_bindgen_func_elem_3690: (a: number, b: number, c: number, d: number) => void;
95
+ readonly workerdb_subscribe: (a: number, b: number) => void;
96
+ readonly __wasm_bindgen_func_elem_3624: (a: number, b: number) => void;
97
+ readonly __wasm_bindgen_func_elem_3703: (a: number, b: number, c: number, d: number) => void;
98
+ readonly __wasm_bindgen_func_elem_3716: (a: number, b: number, c: number, d: number) => void;
92
99
  readonly __wbindgen_export: (a: number, b: number) => number;
93
100
  readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
94
101
  readonly __wbindgen_export3: (a: number) => void;
package/dist/moltendb.js CHANGED
@@ -131,6 +131,15 @@ export class WorkerDb {
131
131
  const ret = wasm.workerdb_new(ptr0, len0);
132
132
  return takeObject(ret);
133
133
  }
134
+ /**
135
+ * Subscribe to real-time database changes.
136
+ * The provided JavaScript function will be called with a JSON string
137
+ * representing the mutation event.
138
+ * @param {Function} callback
139
+ */
140
+ subscribe(callback) {
141
+ wasm.workerdb_subscribe(this.__wbg_ptr, addHeapObject(callback));
142
+ }
134
143
  }
135
144
  if (Symbol.dispose) WorkerDb.prototype[Symbol.dispose] = WorkerDb.prototype.free;
136
145
 
@@ -377,7 +386,7 @@ function __wbg_get_imports() {
377
386
  const a = state0.a;
378
387
  state0.a = 0;
379
388
  try {
380
- return __wasm_bindgen_func_elem_3690(a, state0.b, arg0, arg1);
389
+ return __wasm_bindgen_func_elem_3716(a, state0.b, arg0, arg1);
381
390
  } finally {
382
391
  state0.a = a;
383
392
  }
@@ -497,8 +506,8 @@ function __wbg_get_imports() {
497
506
  return ret;
498
507
  }, arguments); },
499
508
  __wbindgen_cast_0000000000000001: function(arg0, arg1) {
500
- // Cast intrinsic for `Closure(Closure { dtor_idx: 661, function: Function { arguments: [Externref], shim_idx: 672, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
501
- const ret = makeMutClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_3598, __wasm_bindgen_func_elem_3677);
509
+ // Cast intrinsic for `Closure(Closure { dtor_idx: 663, function: Function { arguments: [Externref], shim_idx: 674, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
510
+ const ret = makeMutClosure(arg0, arg1, wasm.__wasm_bindgen_func_elem_3624, __wasm_bindgen_func_elem_3703);
502
511
  return addHeapObject(ret);
503
512
  },
504
513
  __wbindgen_cast_0000000000000002: function(arg0) {
@@ -535,10 +544,10 @@ function __wbg_get_imports() {
535
544
  };
536
545
  }
537
546
 
538
- function __wasm_bindgen_func_elem_3677(arg0, arg1, arg2) {
547
+ function __wasm_bindgen_func_elem_3703(arg0, arg1, arg2) {
539
548
  try {
540
549
  const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
541
- wasm.__wasm_bindgen_func_elem_3677(retptr, arg0, arg1, addHeapObject(arg2));
550
+ wasm.__wasm_bindgen_func_elem_3703(retptr, arg0, arg1, addHeapObject(arg2));
542
551
  var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
543
552
  var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
544
553
  if (r1) {
@@ -549,8 +558,8 @@ function __wasm_bindgen_func_elem_3677(arg0, arg1, arg2) {
549
558
  }
550
559
  }
551
560
 
552
- function __wasm_bindgen_func_elem_3690(arg0, arg1, arg2, arg3) {
553
- wasm.__wasm_bindgen_func_elem_3690(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
561
+ function __wasm_bindgen_func_elem_3716(arg0, arg1, arg2, arg3) {
562
+ wasm.__wasm_bindgen_func_elem_3716(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3));
554
563
  }
555
564
 
556
565
  const WorkerDbFinalization = (typeof FinalizationRegistry === 'undefined')
Binary file
@@ -5,9 +5,10 @@ export const __wbg_workerdb_free: (a: number, b: number) => void;
5
5
  export const workerdb_analytics: (a: number, b: number, c: number, d: number) => void;
6
6
  export const workerdb_handle_message: (a: number, b: number, c: number) => void;
7
7
  export const workerdb_new: (a: number, b: number) => number;
8
- export const __wasm_bindgen_func_elem_3598: (a: number, b: number) => void;
9
- export const __wasm_bindgen_func_elem_3677: (a: number, b: number, c: number, d: number) => void;
10
- export const __wasm_bindgen_func_elem_3690: (a: number, b: number, c: number, d: number) => void;
8
+ export const workerdb_subscribe: (a: number, b: number) => void;
9
+ export const __wasm_bindgen_func_elem_3624: (a: number, b: number) => void;
10
+ export const __wasm_bindgen_func_elem_3703: (a: number, b: number, c: number, d: number) => void;
11
+ export const __wasm_bindgen_func_elem_3716: (a: number, b: number, c: number, d: number) => void;
11
12
  export const __wbindgen_export: (a: number, b: number) => number;
12
13
  export const __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
13
14
  export const __wbindgen_export3: (a: number) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moltendb-web/core",
3
- "version": "0.1.0-alpha.14",
3
+ "version": "0.1.0-alpha.21",
4
4
  "description": "MoltenDB WASM runtime — the database engine, Web Worker, and main-thread client in one package.",
5
5
  "type": "module",
6
6
  "author": "Maximilian Both <maximilian.both27@outlook.com>",