@lark-sh/client 0.1.19 → 0.1.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.
@@ -1536,17 +1536,14 @@ var SubscriptionManager = class {
1536
1536
  queryParamsChanged = view.updateQueryParams(queryParams);
1537
1537
  tag = this.viewKeyToTag.get(viewKey);
1538
1538
  }
1539
- const existingEventTypes = view.getEventTypes();
1540
- const isNewEventType = !existingEventTypes.includes(eventType);
1541
1539
  const unsubscribe = view.addCallback(eventType, callback);
1542
1540
  const wrappedUnsubscribe = () => {
1543
1541
  this.unsubscribeCallback(normalizedPath, eventType, callback, queryId);
1544
1542
  };
1545
- if (isNewView || isNewEventType || queryParamsChanged) {
1543
+ if (isNewView || queryParamsChanged) {
1546
1544
  const hasAncestorComplete = this.hasAncestorCompleteView(normalizedPath);
1547
1545
  if (!hasAncestorComplete) {
1548
- const allEventTypes = view.getEventTypes();
1549
- this.sendSubscribe?.(normalizedPath, allEventTypes, view.queryParams ?? void 0, tag).catch((err) => {
1546
+ this.sendSubscribe?.(normalizedPath, view.queryParams ?? void 0, tag).catch((err) => {
1550
1547
  console.error("Failed to subscribe:", err);
1551
1548
  });
1552
1549
  }
@@ -1638,11 +1635,6 @@ var SubscriptionManager = class {
1638
1635
  this.sendUnsubscribe?.(normalizedPath, queryParams).catch((err) => {
1639
1636
  console.error("Failed to unsubscribe:", err);
1640
1637
  });
1641
- } else {
1642
- const remainingEventTypes = view.getEventTypes();
1643
- this.sendSubscribe?.(normalizedPath, remainingEventTypes, queryParams).catch((err) => {
1644
- console.error("Failed to update subscription:", err);
1645
- });
1646
1638
  }
1647
1639
  }
1648
1640
  if (queryIds.size === 0) {
@@ -2070,10 +2062,9 @@ var SubscriptionManager = class {
2070
2062
  */
2071
2063
  async resubscribeAll() {
2072
2064
  for (const view of this.views.values()) {
2073
- const eventTypes = view.getEventTypes();
2074
- if (eventTypes.length > 0) {
2065
+ if (view.hasCallbacks()) {
2075
2066
  try {
2076
- await this.sendSubscribe?.(view.path, eventTypes, view.queryParams ?? void 0);
2067
+ await this.sendSubscribe?.(view.path, view.queryParams ?? void 0);
2077
2068
  } catch (err) {
2078
2069
  console.error(`Failed to resubscribe to ${view.path}:`, err);
2079
2070
  }
@@ -4243,21 +4234,33 @@ var ServerValue = {
4243
4234
  increment: (delta) => ({ ".sv": { increment: delta } })
4244
4235
  };
4245
4236
  var LarkDatabase = class {
4246
- constructor() {
4237
+ /**
4238
+ * Create a new LarkDatabase instance.
4239
+ *
4240
+ * @param databaseId - Database ID in format "project/database"
4241
+ * @param options - Connection options (token, anonymous, domain, transport).
4242
+ * Defaults to anonymous auth if not specified.
4243
+ *
4244
+ * @example
4245
+ * ```typescript
4246
+ * // Lazy connection - connects on first operation
4247
+ * const db = new LarkDatabase('project/database', { anonymous: true });
4248
+ * db.ref('/users').on('value', cb); // Auto-connects here
4249
+ *
4250
+ * // Explicit connection - for UI feedback or error handling
4251
+ * const db = new LarkDatabase('project/database', { anonymous: true });
4252
+ * await db.connect(); // Explicitly await connection
4253
+ * db.ref('/users').on('value', cb);
4254
+ * ```
4255
+ */
4256
+ constructor(databaseId, options = {}) {
4247
4257
  this._state = "disconnected";
4248
4258
  this._auth = null;
4249
- this._databaseId = null;
4250
- this._domain = null;
4251
4259
  this._volatilePaths = [];
4252
4260
  this._transportType = null;
4253
- // Auth state
4254
- this._currentToken = null;
4255
- // Token for auth (empty string = anonymous)
4256
- this._isAnonymous = false;
4257
4261
  // True if connected anonymously
4258
4262
  // Reconnection state
4259
4263
  this._connectionId = null;
4260
- this._connectOptions = null;
4261
4264
  this._intentionalDisconnect = false;
4262
4265
  this._reconnectAttempt = 0;
4263
4266
  this._reconnectTimer = null;
@@ -4270,13 +4273,26 @@ var LarkDatabase = class {
4270
4273
  this.authStateChangedCallbacks = /* @__PURE__ */ new Set();
4271
4274
  // .info path subscriptions (handled locally, not sent to server)
4272
4275
  this.infoSubscriptions = [];
4273
- // Authentication promise - resolves when auth completes, allows operations to queue
4274
- this.authenticationPromise = null;
4275
- this.authenticationResolve = null;
4276
+ // Connection promise - shared by connect() and lazy operations
4277
+ this._connectionPromise = null;
4276
4278
  this._serverTimeOffset = 0;
4277
4279
  this.messageQueue = new MessageQueue();
4278
4280
  this.subscriptionManager = new SubscriptionManager();
4279
4281
  this.pendingWrites = new PendingWriteManager();
4282
+ const projectId = databaseId.split("/")[0];
4283
+ if (!projectId) {
4284
+ throw new Error('Invalid database ID: must be in format "projectId/databaseName"');
4285
+ }
4286
+ this._databaseId = databaseId;
4287
+ this._connectOptions = options;
4288
+ this._domain = options.domain || DEFAULT_LARK_DOMAIN;
4289
+ this._currentToken = options.token || "";
4290
+ this._isAnonymous = !options.token && options.anonymous !== false;
4291
+ this.subscriptionManager.initialize({
4292
+ sendSubscribe: this.sendSubscribeMessage.bind(this),
4293
+ sendUnsubscribe: this.sendUnsubscribeMessage.bind(this),
4294
+ createSnapshot: this.createSnapshot.bind(this)
4295
+ });
4280
4296
  }
4281
4297
  // ============================================
4282
4298
  // Connection State
@@ -4361,21 +4377,38 @@ var LarkDatabase = class {
4361
4377
  // Connection Management
4362
4378
  // ============================================
4363
4379
  /**
4364
- * Connect to a database.
4380
+ * Explicitly connect to the database.
4365
4381
  *
4366
- * @param databaseId - Database ID in format "project/database"
4367
- * @param options - Connection options (token, anonymous, domain)
4382
+ * This is optional - operations will auto-connect if needed.
4383
+ * Use this when you want to:
4384
+ * - Await connection completion for UI feedback
4385
+ * - Handle connection errors separately from operation errors
4386
+ * - Pre-warm the connection before first operation
4387
+ *
4388
+ * @returns Promise that resolves when fully authenticated
4368
4389
  */
4369
- async connect(databaseId, options = {}) {
4370
- if (this._state !== "disconnected") {
4371
- throw new Error("Already connected or connecting");
4390
+ async connect() {
4391
+ return this.ensureConnected();
4392
+ }
4393
+ /**
4394
+ * Ensure connection is established, triggering it if needed.
4395
+ * Multiple concurrent calls share the same connection promise.
4396
+ */
4397
+ async ensureConnected() {
4398
+ if (this._state === "authenticated") {
4399
+ return;
4400
+ }
4401
+ if (this._connectionPromise) {
4402
+ return this._connectionPromise;
4372
4403
  }
4373
- this._connectOptions = options;
4374
4404
  this._intentionalDisconnect = false;
4375
- this.authenticationPromise = new Promise((resolve) => {
4376
- this.authenticationResolve = resolve;
4377
- });
4378
- await this.performConnect(databaseId, options);
4405
+ this._connectionPromise = this.performConnect(this._databaseId, this._connectOptions);
4406
+ try {
4407
+ await this._connectionPromise;
4408
+ } catch (error) {
4409
+ this._connectionPromise = null;
4410
+ throw error;
4411
+ }
4379
4412
  }
4380
4413
  /**
4381
4414
  * Internal connect implementation used by both initial connect and reconnect.
@@ -4385,19 +4418,9 @@ var LarkDatabase = class {
4385
4418
  * 3. Send auth (authenticates user - required even for anonymous)
4386
4419
  */
4387
4420
  async performConnect(databaseId, options, isReconnect = false) {
4388
- const previousState = this._state;
4389
4421
  this._state = isReconnect ? "reconnecting" : "connecting";
4390
- this._databaseId = databaseId;
4391
- this._domain = options.domain || DEFAULT_LARK_DOMAIN;
4392
- if (!isReconnect) {
4393
- this._currentToken = options.token || "";
4394
- this._isAnonymous = !options.token && options.anonymous !== false;
4395
- }
4396
4422
  try {
4397
4423
  const projectId = databaseId.split("/")[0];
4398
- if (!projectId) {
4399
- throw new Error('Invalid database ID: must be in format "projectId/databaseName"');
4400
- }
4401
4424
  const wsUrl = `wss://${projectId}.${this._domain}/ws`;
4402
4425
  const transportResult = await createTransport(
4403
4426
  wsUrl,
@@ -4450,18 +4473,7 @@ var LarkDatabase = class {
4450
4473
  };
4451
4474
  this._state = "authenticated";
4452
4475
  this._reconnectAttempt = 0;
4453
- if (this.authenticationResolve) {
4454
- this.authenticationResolve();
4455
- this.authenticationResolve = null;
4456
- }
4457
4476
  this.fireConnectionStateChange();
4458
- if (!isReconnect) {
4459
- this.subscriptionManager.initialize({
4460
- sendSubscribe: this.sendSubscribeMessage.bind(this),
4461
- sendUnsubscribe: this.sendUnsubscribeMessage.bind(this),
4462
- createSnapshot: this.createSnapshot.bind(this)
4463
- });
4464
- }
4465
4477
  if (isReconnect) {
4466
4478
  await this.restoreAfterReconnect();
4467
4479
  }
@@ -4475,12 +4487,8 @@ var LarkDatabase = class {
4475
4487
  }
4476
4488
  this._state = "disconnected";
4477
4489
  this._auth = null;
4478
- this._databaseId = null;
4479
- this._connectOptions = null;
4480
4490
  this._connectionId = null;
4481
4491
  this._transportType = null;
4482
- this._currentToken = null;
4483
- this._isAnonymous = false;
4484
4492
  this.transport?.close();
4485
4493
  this.transport = null;
4486
4494
  throw error;
@@ -4548,8 +4556,9 @@ var LarkDatabase = class {
4548
4556
  }
4549
4557
  }
4550
4558
  /**
4551
- * Full cleanup - clears all state including subscriptions.
4559
+ * Full cleanup - clears connection state including subscriptions.
4552
4560
  * Used for intentional disconnect.
4561
+ * Preserves config (databaseId, domain, token) so connect() can be called again.
4553
4562
  */
4554
4563
  cleanupFull() {
4555
4564
  const wasAuthenticated = this._state === "authenticated";
@@ -4557,17 +4566,11 @@ var LarkDatabase = class {
4557
4566
  this.transport = null;
4558
4567
  this._state = "disconnected";
4559
4568
  this._auth = null;
4560
- this._databaseId = null;
4561
4569
  this._volatilePaths = [];
4562
- this._domain = null;
4563
4570
  this._connectionId = null;
4564
- this._connectOptions = null;
4565
4571
  this._transportType = null;
4566
- this._currentToken = null;
4567
- this._isAnonymous = false;
4568
4572
  this._reconnectAttempt = 0;
4569
- this.authenticationPromise = null;
4570
- this.authenticationResolve = null;
4573
+ this._connectionPromise = null;
4571
4574
  this.subscriptionManager.clear();
4572
4575
  this.messageQueue.rejectAll(new Error("Connection closed"));
4573
4576
  this.pendingWrites.clear();
@@ -4668,15 +4671,14 @@ var LarkDatabase = class {
4668
4671
  * Attempt to reconnect to the database.
4669
4672
  */
4670
4673
  async attemptReconnect() {
4671
- if (this._intentionalDisconnect || !this._databaseId || !this._connectOptions) {
4674
+ if (this._intentionalDisconnect) {
4672
4675
  return;
4673
4676
  }
4674
- this.authenticationPromise = new Promise((resolve) => {
4675
- this.authenticationResolve = resolve;
4676
- });
4677
+ this._connectionPromise = this.performConnect(this._databaseId, this._connectOptions, true);
4677
4678
  try {
4678
- await this.performConnect(this._databaseId, this._connectOptions, true);
4679
+ await this._connectionPromise;
4679
4680
  } catch {
4681
+ this._connectionPromise = null;
4680
4682
  }
4681
4683
  }
4682
4684
  /**
@@ -4840,9 +4842,7 @@ var LarkDatabase = class {
4840
4842
  * @internal Send a transaction to the server.
4841
4843
  */
4842
4844
  async _sendTransaction(ops) {
4843
- if (!this.isAuthenticatedOrThrow()) {
4844
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
4845
- }
4845
+ if (this._state !== "authenticated") await this.ensureConnected();
4846
4846
  const requestId = this.messageQueue.nextRequestId();
4847
4847
  this.pendingWrites.trackWrite(requestId, "transaction", "/", ops);
4848
4848
  const message = {
@@ -5036,41 +5036,6 @@ var LarkDatabase = class {
5036
5036
  // ============================================
5037
5037
  // Internal: Sending Messages
5038
5038
  // ============================================
5039
- /**
5040
- * Check if authenticated synchronously.
5041
- * Returns true if authenticated, false if connecting (should wait), throws if disconnected.
5042
- */
5043
- isAuthenticatedOrThrow() {
5044
- if (this._state === "authenticated") {
5045
- return true;
5046
- }
5047
- if (this._state === "connecting" || this._state === "connected" || this._state === "joined" || this._state === "reconnecting") {
5048
- return false;
5049
- }
5050
- throw new LarkError("not_connected", "Not connected - call connect() first");
5051
- }
5052
- /**
5053
- * Wait for authentication to complete before performing an operation.
5054
- * If already authenticated, returns immediately (synchronously).
5055
- * If connecting/reconnecting, waits for auth to complete.
5056
- * If disconnected and no connect in progress, throws.
5057
- *
5058
- * IMPORTANT: This returns a Promise only if waiting is needed.
5059
- * Callers should use: `if (!this.isAuthenticatedOrThrow()) if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();`
5060
- * to preserve synchronous execution when already authenticated.
5061
- */
5062
- async waitForAuthenticated() {
5063
- if (this._state === "authenticated") {
5064
- return;
5065
- }
5066
- if (this._state === "connecting" || this._state === "connected" || this._state === "joined" || this._state === "reconnecting") {
5067
- if (this.authenticationPromise) {
5068
- await this.authenticationPromise;
5069
- return;
5070
- }
5071
- }
5072
- throw new LarkError("not_connected", "Not connected - call connect() first");
5073
- }
5074
5039
  send(message) {
5075
5040
  if (!this.transport || !this.transport.connected) {
5076
5041
  throw new LarkError("not_connected", "Not connected to database");
@@ -5085,7 +5050,7 @@ var LarkDatabase = class {
5085
5050
  * Note: Priority is now part of the value (as .priority), not a separate field.
5086
5051
  */
5087
5052
  async _sendSet(path, value) {
5088
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5053
+ if (this._state !== "authenticated") await this.ensureConnected();
5089
5054
  const normalizedPath = normalizePath(path) || "/";
5090
5055
  validateWriteData(value, normalizedPath);
5091
5056
  validateWriteSize(value, normalizedPath);
@@ -5111,7 +5076,7 @@ var LarkDatabase = class {
5111
5076
  * @internal Send an update operation.
5112
5077
  */
5113
5078
  async _sendUpdate(path, values) {
5114
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5079
+ if (this._state !== "authenticated") await this.ensureConnected();
5115
5080
  const normalizedPath = normalizePath(path) || "/";
5116
5081
  for (const [key, value] of Object.entries(values)) {
5117
5082
  const fullPath = key.startsWith("/") ? key : `${normalizedPath}/${key}`;
@@ -5144,7 +5109,7 @@ var LarkDatabase = class {
5144
5109
  * @internal Send a delete operation.
5145
5110
  */
5146
5111
  async _sendDelete(path) {
5147
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5112
+ if (this._state !== "authenticated") await this.ensureConnected();
5148
5113
  const normalizedPath = normalizePath(path) || "/";
5149
5114
  const requestId = this.messageQueue.nextRequestId();
5150
5115
  const pendingWriteIds = this.subscriptionManager.getPendingWriteIdsForPath(normalizedPath);
@@ -5253,7 +5218,7 @@ var LarkDatabase = class {
5253
5218
  return new DataSnapshot(cached.value, path, this);
5254
5219
  }
5255
5220
  }
5256
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5221
+ if (this._state !== "authenticated") await this.ensureConnected();
5257
5222
  const requestId = this.messageQueue.nextRequestId();
5258
5223
  const message = {
5259
5224
  o: "o",
@@ -5270,7 +5235,7 @@ var LarkDatabase = class {
5270
5235
  * @internal Send an onDisconnect operation.
5271
5236
  */
5272
5237
  async _sendOnDisconnect(path, action, value) {
5273
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5238
+ if (this._state !== "authenticated") await this.ensureConnected();
5274
5239
  const requestId = this.messageQueue.nextRequestId();
5275
5240
  const message = {
5276
5241
  o: "od",
@@ -5296,13 +5261,12 @@ var LarkDatabase = class {
5296
5261
  * @internal Send a subscribe message to server.
5297
5262
  * Includes tag for non-default queries to enable proper event routing.
5298
5263
  */
5299
- async sendSubscribeMessage(path, eventTypes, queryParams, tag) {
5300
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5264
+ async sendSubscribeMessage(path, queryParams, tag) {
5265
+ if (this._state !== "authenticated") await this.ensureConnected();
5301
5266
  const requestId = this.messageQueue.nextRequestId();
5302
5267
  const message = {
5303
5268
  o: "sb",
5304
5269
  p: normalizePath(path) || "/",
5305
- e: eventTypes,
5306
5270
  r: requestId,
5307
5271
  ...queryParams,
5308
5272
  ...tag !== void 0 ? { tag } : {}
@@ -5315,7 +5279,7 @@ var LarkDatabase = class {
5315
5279
  * Includes query params and tag so server can identify which specific subscription to remove.
5316
5280
  */
5317
5281
  async sendUnsubscribeMessage(path, queryParams, tag) {
5318
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5282
+ if (this._state !== "authenticated") await this.ensureConnected();
5319
5283
  const requestId = this.messageQueue.nextRequestId();
5320
5284
  const message = {
5321
5285
  o: "us",
@@ -5380,4 +5344,4 @@ export {
5380
5344
  ServerValue,
5381
5345
  LarkDatabase
5382
5346
  };
5383
- //# sourceMappingURL=chunk-UF3SZ62Y.mjs.map
5347
+ //# sourceMappingURL=chunk-HHBHX2EM.mjs.map