@lark-sh/client 0.1.22 → 0.1.23

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/README.md CHANGED
@@ -210,6 +210,46 @@ db.onAuthStateChanged((auth) => {
210
210
  });
211
211
  ```
212
212
 
213
+ ### Lazy Connect
214
+
215
+ You don't need to wait for `connect()` to finish before using the database. Operations called during connection are automatically queued and execute once authenticated:
216
+
217
+ ```typescript
218
+ const db = new LarkDatabase();
219
+
220
+ // Start connecting (don't await)
221
+ const connectPromise = db.connect('project/db', { anonymous: true });
222
+
223
+ // These queue and run after auth completes
224
+ db.ref('users').on('value', callback);
225
+ const writePromise = db.ref('data').set({ hello: 'world' });
226
+
227
+ await connectPromise;
228
+ await writePromise;
229
+ ```
230
+
231
+ ### Volatile Paths
232
+
233
+ Lark supports volatile paths for high-frequency data like player positions or cursors. Volatile paths are defined in your server rules with `.volatile: true` and use fire-and-forget writes for lower latency:
234
+
235
+ ```typescript
236
+ // Check which paths are volatile (set by server rules)
237
+ db.volatilePaths; // e.g. ['players/*/position', 'cursors']
238
+
239
+ // Writes to volatile paths resolve immediately (no server ack)
240
+ await db.ref('players/abc/position').set({ x: 10, y: 20 });
241
+
242
+ // Volatile events include a server timestamp for interpolation
243
+ db.ref('players').on('value', (snapshot) => {
244
+ if (snapshot.isVolatile()) {
245
+ const ts = snapshot.getServerTimestamp(); // Unix epoch ms
246
+ // Use deltas between timestamps for smooth interpolation
247
+ }
248
+ });
249
+ ```
250
+
251
+ When using WebTransport, volatile writes are sent via UDP datagrams for even lower latency. The server batches volatile events at 20Hz over WebTransport vs 7Hz over WebSocket.
252
+
213
253
  ## Firebase v8 Compatibility
214
254
 
215
255
  For users migrating from Firebase who need exact v8 API behavior, use the `fb-v8` sub-package:
@@ -368,9 +368,6 @@ function isEventMessage(msg) {
368
368
  function isOnceResponseMessage(msg) {
369
369
  return "oc" in msg;
370
370
  }
371
- function isPingMessage(msg) {
372
- return "o" in msg && msg.o === "pi";
373
- }
374
371
 
375
372
  // src/connection/MessageQueue.ts
376
373
  var MessageQueue = class {
@@ -4210,7 +4207,7 @@ var ServerValue = {
4210
4207
  */
4211
4208
  increment: (delta) => ({ ".sv": { increment: delta } })
4212
4209
  };
4213
- var LarkDatabase = class {
4210
+ var _LarkDatabase = class _LarkDatabase {
4214
4211
  /**
4215
4212
  * Create a new LarkDatabase instance.
4216
4213
  *
@@ -4253,6 +4250,7 @@ var LarkDatabase = class {
4253
4250
  // Connection promise - shared by connect() and lazy operations
4254
4251
  this._connectionPromise = null;
4255
4252
  this._serverTimeOffset = 0;
4253
+ this._pingInterval = null;
4256
4254
  this.messageQueue = new MessageQueue();
4257
4255
  this.subscriptionManager = new SubscriptionManager();
4258
4256
  this.pendingWrites = new PendingWriteManager();
@@ -4450,6 +4448,7 @@ var LarkDatabase = class {
4450
4448
  };
4451
4449
  this._state = "authenticated";
4452
4450
  this._reconnectAttempt = 0;
4451
+ this.startPingInterval();
4453
4452
  this.fireConnectionStateChange();
4454
4453
  if (isReconnect) {
4455
4454
  await this.restoreAfterReconnect();
@@ -4539,6 +4538,7 @@ var LarkDatabase = class {
4539
4538
  */
4540
4539
  cleanupFull() {
4541
4540
  const wasAuthenticated = this._state === "authenticated";
4541
+ this.stopPingInterval();
4542
4542
  this.transport?.close();
4543
4543
  this.transport = null;
4544
4544
  this._state = "disconnected";
@@ -4561,6 +4561,7 @@ var LarkDatabase = class {
4561
4561
  * Used for unexpected disconnect.
4562
4562
  */
4563
4563
  cleanupForReconnect() {
4564
+ this.stopPingInterval();
4564
4565
  this.transport?.close();
4565
4566
  this.transport = null;
4566
4567
  this._auth = null;
@@ -4568,6 +4569,21 @@ var LarkDatabase = class {
4568
4569
  this.messageQueue.rejectAll(new Error("Connection closed"));
4569
4570
  this.fireConnectionStateChange();
4570
4571
  }
4572
+ startPingInterval() {
4573
+ this.stopPingInterval();
4574
+ this._pingInterval = _LarkDatabase._realSetInterval(() => {
4575
+ this.transport?.send(JSON.stringify({ o: "pi" }));
4576
+ }, 1e4);
4577
+ if (typeof this._pingInterval === "object" && "unref" in this._pingInterval) {
4578
+ this._pingInterval.unref();
4579
+ }
4580
+ }
4581
+ stopPingInterval() {
4582
+ if (this._pingInterval) {
4583
+ _LarkDatabase._realClearInterval(this._pingInterval);
4584
+ this._pingInterval = null;
4585
+ }
4586
+ }
4571
4587
  // ============================================
4572
4588
  // .info Path Handling
4573
4589
  // ============================================
@@ -4950,10 +4966,6 @@ var LarkDatabase = class {
4950
4966
  if (process.env.LARK_DEBUG) {
4951
4967
  console.log("[LARK] <<< SERVER:", JSON.stringify(message, null, 2));
4952
4968
  }
4953
- if (isPingMessage(message)) {
4954
- this.transport?.send(JSON.stringify({ o: "po" }));
4955
- return;
4956
- }
4957
4969
  if (isAckMessage(message)) {
4958
4970
  this.pendingWrites.onAck(message.a);
4959
4971
  this.subscriptionManager.clearPendingWrite(message.a);
@@ -5307,7 +5319,11 @@ var LarkDatabase = class {
5307
5319
  * Server values that are resolved by the server when a write is committed.
5308
5320
  * Alias for the exported ServerValue object.
5309
5321
  */
5310
- LarkDatabase.ServerValue = ServerValue;
5322
+ _LarkDatabase.ServerValue = ServerValue;
5323
+ // Use real timers for ping so fake timers in tests don't cause infinite loops
5324
+ _LarkDatabase._realSetInterval = globalThis.setInterval;
5325
+ _LarkDatabase._realClearInterval = globalThis.clearInterval;
5326
+ var LarkDatabase = _LarkDatabase;
5311
5327
 
5312
5328
  export {
5313
5329
  LarkError,
@@ -5321,4 +5337,4 @@ export {
5321
5337
  ServerValue,
5322
5338
  LarkDatabase
5323
5339
  };
5324
- //# sourceMappingURL=chunk-T5RXZAER.mjs.map
5340
+ //# sourceMappingURL=chunk-3MBKCYWV.mjs.map