@diaryx/wasm 0.11.0 → 0.11.1-dev.193efc4

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/diaryx_wasm.js CHANGED
@@ -135,42 +135,43 @@ export class DiaryxBackend {
135
135
  /**
136
136
  * Create a new sync client for the given server and workspace.
137
137
  *
138
- * This creates a `WasmSyncClient` that wraps `SyncClient<CallbackTransport>`.
139
- * JavaScript manages WebSocket connections while Rust handles all sync logic.
138
+ * Creates a `WasmSyncClient` backed by the shared `SyncSession` protocol
139
+ * handler. JavaScript manages the WebSocket while Rust handles all sync
140
+ * protocol logic (handshake, message routing, framing).
140
141
  *
141
142
  * ## Example
142
143
  *
143
144
  * ```javascript
144
145
  * const client = backend.createSyncClient(
145
- * 'wss://sync.example.com/sync',
146
+ * 'https://sync.example.com',
146
147
  * 'my-workspace-id',
147
148
  * 'auth-token-optional'
148
149
  * );
149
150
  *
150
- * // Get URLs and create WebSocket connections
151
- * const metaUrl = client.getMetadataUrl();
152
- * const bodyUrl = client.getBodyUrl();
151
+ * // Get the WebSocket URL
152
+ * const wsUrl = client.getWsUrl();
153
+ * const ws = new WebSocket(wsUrl);
154
+ * ws.binaryType = 'arraybuffer';
153
155
  *
154
- * // Create WebSockets and connect them to the client
155
- * const metaWs = new WebSocket(metaUrl);
156
- * metaWs.binaryType = 'arraybuffer';
157
- * metaWs.onopen = () => client.markMetadataConnected();
158
- * metaWs.onclose = () => client.markMetadataDisconnected();
159
- * metaWs.onmessage = async (e) => {
160
- * const response = await client.injectMetadataMessage(new Uint8Array(e.data));
161
- * if (response) metaWs.send(response);
156
+ * ws.onopen = async () => {
157
+ * await client.onConnected();
158
+ * // Drain outgoing messages
159
+ * let msg;
160
+ * while ((msg = client.pollOutgoingBinary())) ws.send(msg);
161
+ * while ((msg = client.pollOutgoingText())) ws.send(msg);
162
162
  * };
163
- * // Similar for body WebSocket...
164
163
  *
165
- * // Poll for outgoing messages
166
- * setInterval(() => {
164
+ * ws.onmessage = async (e) => {
165
+ * if (typeof e.data === 'string') {
166
+ * await client.onTextMessage(e.data);
167
+ * } else {
168
+ * await client.onBinaryMessage(new Uint8Array(e.data));
169
+ * }
170
+ * // Drain outgoing messages and events
167
171
  * let msg;
168
- * while ((msg = client.pollMetadataOutgoing())) metaWs.send(msg);
169
- * while ((msg = client.pollBodyOutgoing())) bodyWs.send(msg);
170
- * }, 50);
171
- *
172
- * // Start sync
173
- * await client.start();
172
+ * while ((msg = client.pollOutgoingBinary())) ws.send(msg);
173
+ * while ((msg = client.pollOutgoingText())) ws.send(msg);
174
+ * };
174
175
  * ```
175
176
  * @param {string} server_url
176
177
  * @param {string} workspace_id
@@ -253,26 +254,6 @@ export class DiaryxBackend {
253
254
  const ret = wasm.diaryxbackend_executeJs(this.__wbg_ptr, command);
254
255
  return ret;
255
256
  }
256
- /**
257
- * Get initial body sync step1 message for a document.
258
- *
259
- * Returns a Uint8Array containing the Y-sync step1 message for
260
- * the specified document's body content.
261
- *
262
- * ## Example
263
- * ```javascript
264
- * const step1 = backend.getBodySyncStep1("notes/my-note.md");
265
- * // Frame it for multiplexed connection and send
266
- * ```
267
- * @param {string} doc_name
268
- * @returns {Uint8Array}
269
- */
270
- getBodySyncStep1(doc_name) {
271
- const ptr0 = passStringToWasm0(doc_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
272
- const len0 = WASM_VECTOR_LEN;
273
- const ret = wasm.diaryxbackend_getBodySyncStep1(this.__wbg_ptr, ptr0, len0);
274
- return ret;
275
- }
276
257
  /**
277
258
  * Get the current configuration from root index frontmatter.
278
259
  * Config keys are stored as `diaryx_*` properties.
@@ -282,23 +263,6 @@ export class DiaryxBackend {
282
263
  const ret = wasm.diaryxbackend_getConfig(this.__wbg_ptr);
283
264
  return ret;
284
265
  }
285
- /**
286
- * Get initial workspace sync step1 message.
287
- *
288
- * Returns a Uint8Array containing the Y-sync step1 message to send
289
- * over the WebSocket to initiate workspace metadata sync.
290
- *
291
- * ## Example
292
- * ```javascript
293
- * const step1 = backend.getWorkspaceSyncStep1();
294
- * ws.send(step1);
295
- * ```
296
- * @returns {Uint8Array}
297
- */
298
- getWorkspaceSyncStep1() {
299
- const ret = wasm.diaryxbackend_getWorkspaceSyncStep1(this.__wbg_ptr);
300
- return ret;
301
- }
302
266
  /**
303
267
  * Check if this backend has native sync support.
304
268
  *
@@ -310,65 +274,6 @@ export class DiaryxBackend {
310
274
  const ret = wasm.diaryxbackend_hasNativeSync(this.__wbg_ptr);
311
275
  return ret !== 0;
312
276
  }
313
- /**
314
- * Check if there are pending outgoing sync messages.
315
- * @returns {boolean}
316
- */
317
- hasOutgoingSyncMessages() {
318
- const ret = wasm.diaryxbackend_hasOutgoingSyncMessages(this.__wbg_ptr);
319
- return ret !== 0;
320
- }
321
- /**
322
- * Inject an incoming body sync message.
323
- *
324
- * Call this when the WebSocket receives a message for a body document.
325
- * The message should already be unframed (doc_name extracted separately).
326
- * Returns a response message to send back, or null if no response is needed.
327
- *
328
- * ## Example
329
- * ```javascript
330
- * // After unframing the multiplexed message:
331
- * const response = await backend.injectBodySyncMessage(docName, data, true);
332
- * if (response) ws.send(frameBodyMessage(docName, response));
333
- * ```
334
- * @param {string} doc_name
335
- * @param {Uint8Array} message
336
- * @param {boolean} write_to_disk
337
- * @returns {Promise<any>}
338
- */
339
- injectBodySyncMessage(doc_name, message, write_to_disk) {
340
- const ptr0 = passStringToWasm0(doc_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
341
- const len0 = WASM_VECTOR_LEN;
342
- const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc);
343
- const len1 = WASM_VECTOR_LEN;
344
- const ret = wasm.diaryxbackend_injectBodySyncMessage(this.__wbg_ptr, ptr0, len0, ptr1, len1, write_to_disk);
345
- return ret;
346
- }
347
- /**
348
- * Inject an incoming workspace sync message.
349
- *
350
- * Call this when the WebSocket receives a message for the workspace
351
- * (metadata) connection. Returns a response message to send back,
352
- * or null if no response is needed.
353
- *
354
- * ## Example
355
- * ```javascript
356
- * ws.onmessage = (event) => {
357
- * const data = new Uint8Array(event.data);
358
- * const response = backend.injectWorkspaceSyncMessage(data, true);
359
- * if (response) ws.send(response);
360
- * };
361
- * ```
362
- * @param {Uint8Array} message
363
- * @param {boolean} write_to_disk
364
- * @returns {Promise<any>}
365
- */
366
- injectWorkspaceSyncMessage(message, write_to_disk) {
367
- const ptr0 = passArray8ToWasm0(message, wasm.__wbindgen_malloc);
368
- const len0 = WASM_VECTOR_LEN;
369
- const ret = wasm.diaryxbackend_injectWorkspaceSyncMessage(this.__wbg_ptr, ptr0, len0, write_to_disk);
370
- return ret;
371
- }
372
277
  /**
373
278
  * Unsubscribe from filesystem events.
374
279
  *
@@ -415,43 +320,6 @@ export class DiaryxBackend {
415
320
  const ret = wasm.diaryxbackend_onFileSystemEvent(this.__wbg_ptr, callback);
416
321
  return BigInt.asUintN(64, ret);
417
322
  }
418
- /**
419
- * Get count of pending outgoing sync messages.
420
- * @returns {number}
421
- */
422
- outgoingSyncMessageCount() {
423
- const ret = wasm.diaryxbackend_outgoingSyncMessageCount(this.__wbg_ptr);
424
- return ret >>> 0;
425
- }
426
- /**
427
- * Poll for an outgoing sync message.
428
- *
429
- * Returns the next outgoing message as a JavaScript object with:
430
- * - `docName`: Document name ("workspace" or file path)
431
- * - `message`: Uint8Array message data
432
- * - `isBody`: Boolean indicating body (true) or workspace (false)
433
- *
434
- * Returns null if no messages are queued.
435
- *
436
- * ## Example
437
- * ```javascript
438
- * setInterval(() => {
439
- * let msg;
440
- * while ((msg = backend.pollOutgoingSyncMessage()) !== null) {
441
- * if (msg.isBody) {
442
- * bodyWs.send(frameBodyMessage(msg.docName, msg.message));
443
- * } else {
444
- * workspaceWs.send(msg.message);
445
- * }
446
- * }
447
- * }, 50);
448
- * ```
449
- * @returns {any}
450
- */
451
- pollOutgoingSyncMessage() {
452
- const ret = wasm.diaryxbackend_pollOutgoingSyncMessage(this.__wbg_ptr);
453
- return ret;
454
- }
455
323
  /**
456
324
  * Read binary file.
457
325
  *
@@ -475,30 +343,6 @@ export class DiaryxBackend {
475
343
  const ret = wasm.diaryxbackend_saveConfig(this.__wbg_ptr, config_js);
476
344
  return ret;
477
345
  }
478
- /**
479
- * Start sync session and set up event bridge.
480
- *
481
- * This wires up the sync manager's event callback to queue outgoing
482
- * messages. Call this before connecting WebSocket.
483
- *
484
- * ## Example
485
- * ```javascript
486
- * backend.startSync();
487
- * const ws = new WebSocket(url);
488
- * // ... rest of setup
489
- * ```
490
- */
491
- startSync() {
492
- wasm.diaryxbackend_startSync(this.__wbg_ptr);
493
- }
494
- /**
495
- * Stop sync session.
496
- *
497
- * Clears the outgoing message queue. Call after disconnecting WebSocket.
498
- */
499
- stopSync() {
500
- wasm.diaryxbackend_stopSync(this.__wbg_ptr);
501
- }
502
346
  /**
503
347
  * Write binary file.
504
348
  *
@@ -696,17 +540,11 @@ export class OpfsFileSystem {
696
540
  if (Symbol.dispose) OpfsFileSystem.prototype[Symbol.dispose] = OpfsFileSystem.prototype.free;
697
541
 
698
542
  /**
699
- * WASM sync client wrapper for JavaScript integration.
543
+ * WASM sync client backed by the shared SyncSession protocol handler.
700
544
  *
701
- * This struct wraps a `SyncClient<CallbackTransport>` and exposes its
702
- * functionality to JavaScript via wasm-bindgen. It manages two transports
703
- * (metadata and body) and provides methods for:
704
- *
705
- * - Getting WebSocket URLs for connection
706
- * - Notifying connection status changes
707
- * - Injecting incoming messages
708
- * - Polling for outgoing messages
709
- * - Starting and stopping sync
545
+ * JavaScript feeds WebSocket events in via `onConnected()`, `onBinaryMessage()`,
546
+ * etc. The client processes them through `SyncSession` and queues outgoing
547
+ * messages/events for JavaScript to poll.
710
548
  */
711
549
  export class WasmSyncClient {
712
550
  static __wrap(ptr) {
@@ -727,20 +565,9 @@ export class WasmSyncClient {
727
565
  wasm.__wbg_wasmsyncclient_free(ptr, 0);
728
566
  }
729
567
  /**
730
- * Focus on specific files for sync.
731
- *
732
- * Sends a focus message to the server indicating which files the client
733
- * is currently interested in syncing. Other clients will receive a
734
- * `focus_list_changed` notification and can subscribe to sync updates
735
- * for these files.
568
+ * Send a focus message for specific files.
736
569
  *
737
- * Call this when a file is opened in the editor.
738
- *
739
- * ## Example
740
- * ```javascript
741
- * // User opens a file
742
- * client.focusFiles(["workspace/notes.md"]);
743
- * ```
570
+ * Other clients will be notified which files this client is interested in.
744
571
  * @param {string[]} files
745
572
  */
746
573
  focusFiles(files) {
@@ -748,49 +575,6 @@ export class WasmSyncClient {
748
575
  const len0 = WASM_VECTOR_LEN;
749
576
  wasm.wasmsyncclient_focusFiles(this.__wbg_ptr, ptr0, len0);
750
577
  }
751
- /**
752
- * Get the initial SyncStep1 message for a body document.
753
- *
754
- * Returns a Uint8Array containing the framed message to send via WebSocket.
755
- * @param {string} doc_name
756
- * @returns {Uint8Array}
757
- */
758
- getBodySyncStep1(doc_name) {
759
- const ptr0 = passStringToWasm0(doc_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
760
- const len0 = WASM_VECTOR_LEN;
761
- const ret = wasm.wasmsyncclient_getBodySyncStep1(this.__wbg_ptr, ptr0, len0);
762
- return ret;
763
- }
764
- /**
765
- * Get the WebSocket URL for the body connection.
766
- *
767
- * Returns null if sync hasn't been configured.
768
- * @returns {string | undefined}
769
- */
770
- getBodyUrl() {
771
- const ret = wasm.wasmsyncclient_getBodyUrl(this.__wbg_ptr);
772
- let v1;
773
- if (ret[0] !== 0) {
774
- v1 = getStringFromWasm0(ret[0], ret[1]).slice();
775
- wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
776
- }
777
- return v1;
778
- }
779
- /**
780
- * Get the WebSocket URL for the metadata connection.
781
- *
782
- * Returns null if sync hasn't been configured.
783
- * @returns {string | undefined}
784
- */
785
- getMetadataUrl() {
786
- const ret = wasm.wasmsyncclient_getMetadataUrl(this.__wbg_ptr);
787
- let v1;
788
- if (ret[0] !== 0) {
789
- v1 = getStringFromWasm0(ret[0], ret[1]).slice();
790
- wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
791
- }
792
- return v1;
793
- }
794
578
  /**
795
579
  * Get the server URL.
796
580
  * @returns {string}
@@ -824,158 +608,103 @@ export class WasmSyncClient {
824
608
  }
825
609
  }
826
610
  /**
827
- * Get the initial SyncStep1 message for workspace sync.
611
+ * Get the WebSocket URL for the v2 sync connection.
828
612
  *
829
- * Returns a Uint8Array containing the message to send via WebSocket.
830
- * @returns {Uint8Array}
831
- */
832
- getWorkspaceSyncStep1() {
833
- const ret = wasm.wasmsyncclient_getWorkspaceSyncStep1(this.__wbg_ptr);
834
- return ret;
835
- }
836
- /**
837
- * Check if there are pending body outgoing messages.
838
- * @returns {boolean}
613
+ * Returns a URL like `wss://sync.example.com/sync2?token=...&session=...`
614
+ * @returns {string}
839
615
  */
840
- hasBodyOutgoing() {
841
- const ret = wasm.wasmsyncclient_hasBodyOutgoing(this.__wbg_ptr);
842
- return ret !== 0;
616
+ getWsUrl() {
617
+ let deferred1_0;
618
+ let deferred1_1;
619
+ try {
620
+ const ret = wasm.wasmsyncclient_getWsUrl(this.__wbg_ptr);
621
+ deferred1_0 = ret[0];
622
+ deferred1_1 = ret[1];
623
+ return getStringFromWasm0(ret[0], ret[1]);
624
+ } finally {
625
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
626
+ }
843
627
  }
844
628
  /**
845
- * Check if there are pending body outgoing text messages.
629
+ * Check if there are pending events.
846
630
  * @returns {boolean}
847
631
  */
848
- hasBodyOutgoingText() {
849
- const ret = wasm.wasmsyncclient_hasBodyOutgoingText(this.__wbg_ptr);
632
+ hasEvents() {
633
+ const ret = wasm.wasmsyncclient_hasEvents(this.__wbg_ptr);
850
634
  return ret !== 0;
851
635
  }
852
636
  /**
853
- * Check if there are pending metadata outgoing messages.
637
+ * Check if there are pending outgoing messages or events.
854
638
  * @returns {boolean}
855
639
  */
856
- hasMetadataOutgoing() {
857
- const ret = wasm.wasmsyncclient_hasMetadataOutgoing(this.__wbg_ptr);
640
+ hasOutgoing() {
641
+ const ret = wasm.wasmsyncclient_hasOutgoing(this.__wbg_ptr);
858
642
  return ret !== 0;
859
643
  }
860
644
  /**
861
- * Inject an incoming body message.
645
+ * Inject an incoming binary WebSocket message.
862
646
  *
863
- * Call this when the body WebSocket receives a message.
864
- * The message should already be unframed (doc_name extracted separately).
865
- * Returns a Promise that resolves to a Uint8Array response (or null).
866
- * @param {string} doc_name
867
- * @param {Uint8Array} message
647
+ * Returns a Promise that resolves when processing is complete.
648
+ * After this, poll outgoing queues.
649
+ * @param {Uint8Array} data
868
650
  * @returns {Promise<any>}
869
651
  */
870
- injectBodyMessage(doc_name, message) {
871
- const ptr0 = passStringToWasm0(doc_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
652
+ onBinaryMessage(data) {
653
+ const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
872
654
  const len0 = WASM_VECTOR_LEN;
873
- const ptr1 = passArray8ToWasm0(message, wasm.__wbindgen_malloc);
874
- const len1 = WASM_VECTOR_LEN;
875
- const ret = wasm.wasmsyncclient_injectBodyMessage(this.__wbg_ptr, ptr0, len0, ptr1, len1);
655
+ const ret = wasm.wasmsyncclient_onBinaryMessage(this.__wbg_ptr, ptr0, len0);
876
656
  return ret;
877
657
  }
878
658
  /**
879
- * Inject an incoming metadata message.
880
- *
881
- * Call this when the metadata WebSocket receives a message.
882
- * Returns a Promise that resolves to a Uint8Array response (or null).
659
+ * Notify that the WebSocket connected.
883
660
  *
884
- * The response should be sent back via the WebSocket if not null.
885
- * @param {Uint8Array} message
661
+ * Triggers workspace SyncStep1 and handshake. After calling this,
662
+ * poll `pollOutgoingBinary()` / `pollOutgoingText()` to get messages to send.
886
663
  * @returns {Promise<any>}
887
664
  */
888
- injectMetadataMessage(message) {
889
- const ptr0 = passArray8ToWasm0(message, wasm.__wbindgen_malloc);
890
- const len0 = WASM_VECTOR_LEN;
891
- const ret = wasm.wasmsyncclient_injectMetadataMessage(this.__wbg_ptr, ptr0, len0);
665
+ onConnected() {
666
+ const ret = wasm.wasmsyncclient_onConnected(this.__wbg_ptr);
892
667
  return ret;
893
668
  }
894
669
  /**
895
- * Check if the body connection is established.
896
- * @returns {boolean}
897
- */
898
- isBodyConnected() {
899
- const ret = wasm.wasmsyncclient_isBodyConnected(this.__wbg_ptr);
900
- return ret !== 0;
901
- }
902
- /**
903
- * Check if both connections are established.
904
- * @returns {boolean}
905
- */
906
- isConnected() {
907
- const ret = wasm.wasmsyncclient_isConnected(this.__wbg_ptr);
908
- return ret !== 0;
909
- }
910
- /**
911
- * Check if the metadata connection is established.
912
- * @returns {boolean}
913
- */
914
- isMetadataConnected() {
915
- const ret = wasm.wasmsyncclient_isMetadataConnected(this.__wbg_ptr);
916
- return ret !== 0;
917
- }
918
- /**
919
- * Check if the sync client is running.
920
- * @returns {boolean}
921
- */
922
- isRunning() {
923
- const ret = wasm.wasmsyncclient_isRunning(this.__wbg_ptr);
924
- return ret !== 0;
925
- }
926
- /**
927
- * Mark the body connection as connected.
928
- *
929
- * Call this when the body WebSocket opens.
930
- */
931
- markBodyConnected() {
932
- wasm.wasmsyncclient_markBodyConnected(this.__wbg_ptr);
933
- }
934
- /**
935
- * Mark the body connection as disconnected.
936
- *
937
- * Call this when the body WebSocket closes.
938
- */
939
- markBodyDisconnected() {
940
- wasm.wasmsyncclient_markBodyDisconnected(this.__wbg_ptr);
941
- }
942
- /**
943
- * Mark the metadata connection as connected.
944
- *
945
- * Call this when the metadata WebSocket opens.
670
+ * Notify that the WebSocket disconnected.
671
+ * @returns {Promise<any>}
946
672
  */
947
- markMetadataConnected() {
948
- wasm.wasmsyncclient_markMetadataConnected(this.__wbg_ptr);
673
+ onDisconnected() {
674
+ const ret = wasm.wasmsyncclient_onDisconnected(this.__wbg_ptr);
675
+ return ret;
949
676
  }
950
677
  /**
951
- * Mark the metadata connection as disconnected.
678
+ * Notify that a snapshot was downloaded and imported.
952
679
  *
953
- * Call this when the metadata WebSocket closes.
680
+ * Call this after handling a `downloadSnapshot` event.
681
+ * @returns {Promise<any>}
954
682
  */
955
- markMetadataDisconnected() {
956
- wasm.wasmsyncclient_markMetadataDisconnected(this.__wbg_ptr);
683
+ onSnapshotImported() {
684
+ const ret = wasm.wasmsyncclient_onSnapshotImported(this.__wbg_ptr);
685
+ return ret;
957
686
  }
958
687
  /**
959
- * Poll for an outgoing body message.
688
+ * Inject an incoming text WebSocket message (JSON control message).
960
689
  *
961
- * Returns a Uint8Array if there's a message to send, null otherwise.
962
- * The message is already framed with the document name.
963
- * @returns {Uint8Array | undefined}
690
+ * Returns a Promise that resolves when processing is complete.
691
+ * @param {string} text
692
+ * @returns {Promise<any>}
964
693
  */
965
- pollBodyOutgoing() {
966
- const ret = wasm.wasmsyncclient_pollBodyOutgoing(this.__wbg_ptr);
694
+ onTextMessage(text) {
695
+ const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
696
+ const len0 = WASM_VECTOR_LEN;
697
+ const ret = wasm.wasmsyncclient_onTextMessage(this.__wbg_ptr, ptr0, len0);
967
698
  return ret;
968
699
  }
969
700
  /**
970
- * Poll for an outgoing body text message (for focus/unfocus).
701
+ * Poll for a JSON-serialized event.
971
702
  *
972
- * Returns a string if there's a text message to send, null otherwise.
973
- * JavaScript should call this in a polling loop and send any messages
974
- * via the body WebSocket as text frames.
703
+ * Returns a JSON string representing a SyncEvent, or null.
975
704
  * @returns {string | undefined}
976
705
  */
977
- pollBodyOutgoingText() {
978
- const ret = wasm.wasmsyncclient_pollBodyOutgoingText(this.__wbg_ptr);
706
+ pollEvent() {
707
+ const ret = wasm.wasmsyncclient_pollEvent(this.__wbg_ptr);
979
708
  let v1;
980
709
  if (ret[0] !== 0) {
981
710
  v1 = getStringFromWasm0(ret[0], ret[1]).slice();
@@ -984,112 +713,59 @@ export class WasmSyncClient {
984
713
  return v1;
985
714
  }
986
715
  /**
987
- * Poll for an outgoing metadata message.
716
+ * Poll for an outgoing binary message.
988
717
  *
989
718
  * Returns a Uint8Array if there's a message to send, null otherwise.
990
- * JavaScript should call this in a polling loop and send any messages
991
- * via the metadata WebSocket.
992
719
  * @returns {Uint8Array | undefined}
993
720
  */
994
- pollMetadataOutgoing() {
995
- const ret = wasm.wasmsyncclient_pollMetadataOutgoing(this.__wbg_ptr);
721
+ pollOutgoingBinary() {
722
+ const ret = wasm.wasmsyncclient_pollOutgoingBinary(this.__wbg_ptr);
996
723
  return ret;
997
724
  }
998
725
  /**
999
- * Queue a body update message for sending.
726
+ * Poll for an outgoing text message.
1000
727
  *
1001
- * This creates a Y-sync Update message for the given document
1002
- * and queues it for sending via the body WebSocket.
1003
- * @param {string} doc_name
1004
- * @param {string} content
1005
- */
1006
- queueBodyUpdate(doc_name, content) {
1007
- const ptr0 = passStringToWasm0(doc_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1008
- const len0 = WASM_VECTOR_LEN;
1009
- const ptr1 = passStringToWasm0(content, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1010
- const len1 = WASM_VECTOR_LEN;
1011
- const ret = wasm.wasmsyncclient_queueBodyUpdate(this.__wbg_ptr, ptr0, len0, ptr1, len1);
1012
- if (ret[1]) {
1013
- throw takeFromExternrefTable0(ret[0]);
1014
- }
1015
- }
1016
- /**
1017
- * Queue a workspace update message for sending.
1018
- *
1019
- * This creates a Y-sync Update message from the current workspace state
1020
- * and queues it for sending via the metadata WebSocket.
728
+ * Returns a string if there's a message to send, null otherwise.
729
+ * @returns {string | undefined}
1021
730
  */
1022
- queueWorkspaceUpdate() {
1023
- const ret = wasm.wasmsyncclient_queueWorkspaceUpdate(this.__wbg_ptr);
1024
- if (ret[1]) {
1025
- throw takeFromExternrefTable0(ret[0]);
731
+ pollOutgoingText() {
732
+ const ret = wasm.wasmsyncclient_pollOutgoingText(this.__wbg_ptr);
733
+ let v1;
734
+ if (ret[0] !== 0) {
735
+ v1 = getStringFromWasm0(ret[0], ret[1]).slice();
736
+ wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
1026
737
  }
738
+ return v1;
1027
739
  }
1028
740
  /**
1029
- * Start the sync session.
741
+ * Queue a local CRDT update for sending to the server.
1030
742
  *
1031
- * This should be called after both WebSocket connections are established.
1032
- * It sends the initial SyncStep1 messages and subscribes to all body docs.
1033
- *
1034
- * Returns a Promise that resolves when initial sync messages are sent.
743
+ * Call this when local CRDT changes need to be synced.
744
+ * @param {string} doc_id
745
+ * @param {Uint8Array} data
1035
746
  * @returns {Promise<any>}
1036
747
  */
1037
- start() {
1038
- const ret = wasm.wasmsyncclient_start(this.__wbg_ptr);
1039
- return ret;
1040
- }
1041
- /**
1042
- * Stop the sync session.
1043
- *
1044
- * Clears all pending messages and resets state.
1045
- */
1046
- stop() {
1047
- wasm.wasmsyncclient_stop(this.__wbg_ptr);
1048
- }
1049
- /**
1050
- * Subscribe to body sync for the currently focused files.
1051
- *
1052
- * This sends SyncStep1 messages for all files in the provided list.
1053
- * Call this after receiving a `focus_list_changed` message from the server.
1054
- *
1055
- * ## Example
1056
- * ```javascript
1057
- * // Received focus_list_changed event
1058
- * const files = event.files;
1059
- * client.subscribeBodies(files);
1060
- * ```
1061
- * @param {string[]} files
1062
- */
1063
- subscribeBodies(files) {
1064
- const ptr0 = passArrayJsValueToWasm0(files, wasm.__wbindgen_malloc);
748
+ queueLocalUpdate(doc_id, data) {
749
+ const ptr0 = passStringToWasm0(doc_id, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1065
750
  const len0 = WASM_VECTOR_LEN;
1066
- wasm.wasmsyncclient_subscribeBodies(this.__wbg_ptr, ptr0, len0);
751
+ const ptr1 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
752
+ const len1 = WASM_VECTOR_LEN;
753
+ const ret = wasm.wasmsyncclient_queueLocalUpdate(this.__wbg_ptr, ptr0, len0, ptr1, len1);
754
+ return ret;
1067
755
  }
1068
756
  /**
1069
- * Subscribe to body sync for a specific document.
757
+ * Set a share session code.
1070
758
  *
1071
- * This queues a SyncStep1 message for the given document.
1072
- * Call this when a new file is created or when opening a file for editing.
1073
- * @param {string} doc_name
759
+ * Call this before connecting to join a share session.
760
+ * @param {string} code
1074
761
  */
1075
- subscribeBody(doc_name) {
1076
- const ptr0 = passStringToWasm0(doc_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
762
+ setSessionCode(code) {
763
+ const ptr0 = passStringToWasm0(code, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
1077
764
  const len0 = WASM_VECTOR_LEN;
1078
- wasm.wasmsyncclient_subscribeBody(this.__wbg_ptr, ptr0, len0);
765
+ wasm.wasmsyncclient_setSessionCode(this.__wbg_ptr, ptr0, len0);
1079
766
  }
1080
767
  /**
1081
- * Unfocus specific files.
1082
- *
1083
- * Sends an unfocus message to the server indicating the client is no
1084
- * longer interested in syncing these files.
1085
- *
1086
- * Call this when a file is closed in the editor.
1087
- *
1088
- * ## Example
1089
- * ```javascript
1090
- * // User closes a file
1091
- * client.unfocusFiles(["workspace/notes.md"]);
1092
- * ```
768
+ * Send an unfocus message for specific files.
1093
769
  * @param {string[]} files
1094
770
  */
1095
771
  unfocusFiles(files) {
@@ -1590,7 +1266,7 @@ function __wbg_get_imports() {
1590
1266
  const a = state0.a;
1591
1267
  state0.a = 0;
1592
1268
  try {
1593
- return wasm_bindgen__convert__closures_____invoke__h4ffe3e306d53c3fd(a, state0.b, arg0, arg1);
1269
+ return wasm_bindgen__convert__closures_____invoke__h4e796b59e8c15a06(a, state0.b, arg0, arg1);
1594
1270
  } finally {
1595
1271
  state0.a = a;
1596
1272
  }
@@ -1724,10 +1400,6 @@ function __wbg_get_imports() {
1724
1400
  __wbg_set_3f1d0b984ed272ed: function(arg0, arg1, arg2) {
1725
1401
  arg0[arg1] = arg2;
1726
1402
  },
1727
- __wbg_set_6cb8631f80447a67: function() { return handleError(function (arg0, arg1, arg2) {
1728
- const ret = Reflect.set(arg0, arg1, arg2);
1729
- return ret;
1730
- }, arguments); },
1731
1403
  __wbg_set_cc56eefd2dd91957: function(arg0, arg1, arg2) {
1732
1404
  arg0.set(getArrayU8FromWasm0(arg1, arg2));
1733
1405
  },
@@ -1823,23 +1495,23 @@ function __wbg_get_imports() {
1823
1495
  return ret;
1824
1496
  }, arguments); },
1825
1497
  __wbindgen_cast_0000000000000001: function(arg0, arg1) {
1826
- // Cast intrinsic for `Closure(Closure { dtor_idx: 605, function: Function { arguments: [NamedExternref("Event")], shim_idx: 608, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1827
- const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h28df2ee661749669, wasm_bindgen__convert__closures_____invoke__ha897f5a6ec9291db);
1498
+ // Cast intrinsic for `Closure(Closure { dtor_idx: 649, function: Function { arguments: [NamedExternref("Event")], shim_idx: 652, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
1499
+ const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h07a67dc9810a4be6, wasm_bindgen__convert__closures_____invoke__h599d65e67049bef4);
1828
1500
  return ret;
1829
1501
  },
1830
1502
  __wbindgen_cast_0000000000000002: function(arg0, arg1) {
1831
- // Cast intrinsic for `Closure(Closure { dtor_idx: 605, function: Function { arguments: [NamedExternref("IDBVersionChangeEvent")], shim_idx: 606, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1832
- const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h28df2ee661749669, wasm_bindgen__convert__closures_____invoke__h841a77a6e340370c);
1503
+ // Cast intrinsic for `Closure(Closure { dtor_idx: 649, function: Function { arguments: [NamedExternref("IDBVersionChangeEvent")], shim_idx: 650, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1504
+ const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h07a67dc9810a4be6, wasm_bindgen__convert__closures_____invoke__h41872df2b19d108c);
1833
1505
  return ret;
1834
1506
  },
1835
1507
  __wbindgen_cast_0000000000000003: function(arg0, arg1) {
1836
- // Cast intrinsic for `Closure(Closure { dtor_idx: 710, function: Function { arguments: [NamedExternref("Event")], shim_idx: 711, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1837
- const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h5cf44c247c58c7e4, wasm_bindgen__convert__closures_____invoke__h5baa53a7fa33b538);
1508
+ // Cast intrinsic for `Closure(Closure { dtor_idx: 749, function: Function { arguments: [NamedExternref("Event")], shim_idx: 750, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1509
+ const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h54a6b627d1123dca, wasm_bindgen__convert__closures_____invoke__h7d21c95eeb3011e3);
1838
1510
  return ret;
1839
1511
  },
1840
1512
  __wbindgen_cast_0000000000000004: function(arg0, arg1) {
1841
- // Cast intrinsic for `Closure(Closure { dtor_idx: 736, function: Function { arguments: [Externref], shim_idx: 737, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1842
- const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h9e8d4146731933b4, wasm_bindgen__convert__closures_____invoke__h5ab46734fc677051);
1513
+ // Cast intrinsic for `Closure(Closure { dtor_idx: 775, function: Function { arguments: [Externref], shim_idx: 776, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
1514
+ const ret = makeMutClosure(arg0, arg1, wasm.wasm_bindgen__closure__destroy__h440f08373ff30a05, wasm_bindgen__convert__closures_____invoke__h359356b1e0b37746);
1843
1515
  return ret;
1844
1516
  },
1845
1517
  __wbindgen_cast_0000000000000005: function(arg0) {
@@ -1883,27 +1555,27 @@ function __wbg_get_imports() {
1883
1555
  };
1884
1556
  }
1885
1557
 
1886
- function wasm_bindgen__convert__closures_____invoke__h841a77a6e340370c(arg0, arg1, arg2) {
1887
- wasm.wasm_bindgen__convert__closures_____invoke__h841a77a6e340370c(arg0, arg1, arg2);
1558
+ function wasm_bindgen__convert__closures_____invoke__h41872df2b19d108c(arg0, arg1, arg2) {
1559
+ wasm.wasm_bindgen__convert__closures_____invoke__h41872df2b19d108c(arg0, arg1, arg2);
1888
1560
  }
1889
1561
 
1890
- function wasm_bindgen__convert__closures_____invoke__h5baa53a7fa33b538(arg0, arg1, arg2) {
1891
- wasm.wasm_bindgen__convert__closures_____invoke__h5baa53a7fa33b538(arg0, arg1, arg2);
1562
+ function wasm_bindgen__convert__closures_____invoke__h7d21c95eeb3011e3(arg0, arg1, arg2) {
1563
+ wasm.wasm_bindgen__convert__closures_____invoke__h7d21c95eeb3011e3(arg0, arg1, arg2);
1892
1564
  }
1893
1565
 
1894
- function wasm_bindgen__convert__closures_____invoke__h5ab46734fc677051(arg0, arg1, arg2) {
1895
- wasm.wasm_bindgen__convert__closures_____invoke__h5ab46734fc677051(arg0, arg1, arg2);
1566
+ function wasm_bindgen__convert__closures_____invoke__h359356b1e0b37746(arg0, arg1, arg2) {
1567
+ wasm.wasm_bindgen__convert__closures_____invoke__h359356b1e0b37746(arg0, arg1, arg2);
1896
1568
  }
1897
1569
 
1898
- function wasm_bindgen__convert__closures_____invoke__ha897f5a6ec9291db(arg0, arg1, arg2) {
1899
- const ret = wasm.wasm_bindgen__convert__closures_____invoke__ha897f5a6ec9291db(arg0, arg1, arg2);
1570
+ function wasm_bindgen__convert__closures_____invoke__h599d65e67049bef4(arg0, arg1, arg2) {
1571
+ const ret = wasm.wasm_bindgen__convert__closures_____invoke__h599d65e67049bef4(arg0, arg1, arg2);
1900
1572
  if (ret[1]) {
1901
1573
  throw takeFromExternrefTable0(ret[0]);
1902
1574
  }
1903
1575
  }
1904
1576
 
1905
- function wasm_bindgen__convert__closures_____invoke__h4ffe3e306d53c3fd(arg0, arg1, arg2, arg3) {
1906
- wasm.wasm_bindgen__convert__closures_____invoke__h4ffe3e306d53c3fd(arg0, arg1, arg2, arg3);
1577
+ function wasm_bindgen__convert__closures_____invoke__h4e796b59e8c15a06(arg0, arg1, arg2, arg3) {
1578
+ wasm.wasm_bindgen__convert__closures_____invoke__h4e796b59e8c15a06(arg0, arg1, arg2, arg3);
1907
1579
  }
1908
1580
 
1909
1581