@chainstream-io/sdk 0.1.21 → 0.1.22

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.
@@ -301,10 +301,24 @@ var StreamApi = class {
301
301
  constructor(context) {
302
302
  __publicField(this, "realtimeClient");
303
303
  __publicField(this, "listenersMap");
304
- const realtimeEndpoint = this.buildWsUrl(context.streamUrl, context.accessToken);
304
+ __publicField(this, "context");
305
+ __publicField(this, "initPromise", null);
306
+ this.context = context;
307
+ this.listenersMap = /* @__PURE__ */ new Map();
308
+ }
309
+ /**
310
+ * Initialize Centrifuge client with current token
311
+ * This is called lazily when connect() is called
312
+ */
313
+ async initClient() {
314
+ if (this.realtimeClient) {
315
+ return;
316
+ }
317
+ const token = await this.getTokenValue();
318
+ const realtimeEndpoint = this.buildWsUrl(this.context.streamUrl, token);
305
319
  this.realtimeClient = new import_centrifuge.Centrifuge(realtimeEndpoint, {
306
320
  getToken: async (_ctx) => {
307
- return typeof context.accessToken === "string" ? context.accessToken : await context.accessToken.getToken();
321
+ return this.getTokenValue();
308
322
  }
309
323
  });
310
324
  this.realtimeClient.on("connected", () => {
@@ -314,34 +328,73 @@ var StreamApi = class {
314
328
  }).on("error", (err) => {
315
329
  console.error("[streaming] error: ", err);
316
330
  });
317
- this.listenersMap = /* @__PURE__ */ new Map();
331
+ }
332
+ /**
333
+ * Wait for initialization to complete
334
+ * If not initialized, auto-connect first
335
+ */
336
+ async ensureInitialized() {
337
+ if (!this.initPromise) {
338
+ this.connect();
339
+ }
340
+ await this.initPromise;
341
+ }
342
+ /**
343
+ * Get token value from string or TokenProvider
344
+ */
345
+ async getTokenValue() {
346
+ return typeof this.context.accessToken === "string" ? this.context.accessToken : await this.context.accessToken.getToken();
318
347
  }
319
348
  /**
320
349
  * Build WebSocket URL with token as query parameter
321
350
  */
322
- buildWsUrl(endpoint, accessToken) {
351
+ buildWsUrl(endpoint, token) {
323
352
  const url = new URL(endpoint);
324
- if (typeof accessToken === "string") {
325
- url.searchParams.set("token", accessToken);
326
- }
353
+ url.searchParams.set("token", token);
327
354
  return url.toString();
328
355
  }
356
+ /**
357
+ * Connect to the WebSocket server.
358
+ * This is automatically called when you use subscribe methods if not already connected.
359
+ * You can also call this method manually for explicit control.
360
+ */
329
361
  connect() {
330
- this.realtimeClient.connect();
362
+ this.initPromise = this.initClient().then(() => {
363
+ this.realtimeClient.connect();
364
+ });
365
+ }
366
+ /**
367
+ * Disconnect from the WebSocket server.
368
+ * All subscriptions will be automatically removed.
369
+ */
370
+ disconnect() {
371
+ if (this.realtimeClient) {
372
+ this.realtimeClient.disconnect();
373
+ }
374
+ }
375
+ /**
376
+ * Check if the WebSocket is currently connected.
377
+ */
378
+ isConnected() {
379
+ return this.realtimeClient?.state === "connected";
331
380
  }
332
381
  /**
333
382
  * Start batching commands for efficient bulk operations
334
383
  * All subscription commands after this call will be batched until stopBatching is called
335
384
  */
336
385
  startBatching() {
337
- this.realtimeClient.startBatching();
386
+ if (this.realtimeClient) {
387
+ this.realtimeClient.startBatching();
388
+ }
338
389
  }
339
390
  /**
340
391
  * Stop batching and flush all collected commands to the server
341
392
  * This will send all batched subscription commands in a single network request
342
393
  */
343
394
  stopBatching() {
344
- this.realtimeClient.stopBatching();
395
+ if (this.realtimeClient) {
396
+ this.realtimeClient.stopBatching();
397
+ }
345
398
  }
346
399
  /**
347
400
  * Batch subscribe method that accepts a function containing subscription calls
@@ -371,26 +424,31 @@ var StreamApi = class {
371
424
  });
372
425
  }
373
426
  subscribe(channel, fn, filter, methodName) {
374
- let sub = this.realtimeClient.getSubscription(channel);
375
427
  let listeners = this.listenersMap.get(channel);
376
- if (!sub) {
428
+ if (!listeners) {
377
429
  listeners = /* @__PURE__ */ new Set();
378
430
  this.listenersMap.set(channel, listeners);
379
- console.log("[xrealtime] create new sub: ", channel);
380
- const processedFilter = filter && methodName ? replaceFilterFields(filter, methodName) : filter;
381
- sub = this.realtimeClient.newSubscription(channel, {
382
- delta: "fossil",
383
- ...processedFilter && { filter: processedFilter }
384
- });
385
- sub.on("subscribed", () => {
386
- console.log("[xrealtime] subscribed", channel);
387
- }).on("unsubscribed", () => {
388
- console.log("[xrealtime] unsubscribed", channel);
389
- }).on("publication", (ctx) => {
390
- listeners?.forEach((it) => it(ctx.data));
391
- }).subscribe();
392
431
  }
393
- listeners?.add(fn);
432
+ listeners.add(fn);
433
+ this.ensureInitialized().then(() => {
434
+ let sub = this.realtimeClient.getSubscription(channel);
435
+ if (!sub) {
436
+ console.log("[xrealtime] create new sub: ", channel);
437
+ const processedFilter = filter && methodName ? replaceFilterFields(filter, methodName) : filter;
438
+ sub = this.realtimeClient.newSubscription(channel, {
439
+ delta: "fossil",
440
+ ...processedFilter && { filter: processedFilter }
441
+ });
442
+ sub.on("subscribed", () => {
443
+ console.log("[xrealtime] subscribed", channel);
444
+ }).on("unsubscribed", () => {
445
+ console.log("[xrealtime] unsubscribed", channel);
446
+ }).on("publication", (ctx) => {
447
+ const currentListeners = this.listenersMap.get(channel);
448
+ currentListeners?.forEach((it) => it(ctx.data));
449
+ }).subscribe();
450
+ }
451
+ });
394
452
  return new StreamUnsubscrible(this, channel, fn);
395
453
  }
396
454
  unsubscribe(channel, fn) {
@@ -402,12 +460,14 @@ var StreamApi = class {
402
460
  console.log("unsubscribe, remain listeners: ", listeners.size);
403
461
  if (listeners.size === 0) {
404
462
  console.log("unsubscribe channel: ", channel);
405
- const sub = this.realtimeClient.getSubscription(channel);
406
- if (sub) {
407
- sub.unsubscribe();
408
- this.realtimeClient.removeSubscription(sub);
409
- }
410
463
  this.listenersMap.delete(channel);
464
+ this.ensureInitialized().then(() => {
465
+ const sub = this.realtimeClient.getSubscription(channel);
466
+ if (sub) {
467
+ sub.unsubscribe();
468
+ this.realtimeClient.removeSubscription(sub);
469
+ }
470
+ });
411
471
  }
412
472
  }
413
473
  formatScientificNotation(value) {