@cshah18/sdk 3.0.2 → 3.0.4

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.
@@ -2633,6 +2633,7 @@ class WidgetRoot {
2633
2633
  }
2634
2634
  /** Fetch latest group data and re-render containers */
2635
2635
  async refreshGroupDataFromRealtime() {
2636
+ console.log("request group data realtime callledddd");
2636
2637
  // Debounce rapid refreshes to prevent loops and reduce API load
2637
2638
  const now = Date.now();
2638
2639
  if (now - this.lastGroupDataRefreshTime < this.GROUP_REFRESH_DEBOUNCE) {
@@ -3047,6 +3048,7 @@ class WidgetRoot {
3047
3048
  let groupData = null;
3048
3049
  if (this.apiClient) {
3049
3050
  rewardData = await this.fetchRewardWithRetry(options.productId);
3051
+ console.log("from renderrrr");
3050
3052
  groupData = await this.fetchPrimaryGroup(options.productId);
3051
3053
  this.currentGroupData = groupData;
3052
3054
  this.currentGroupId = (groupData === null || groupData === void 0 ? void 0 : groupData.id) || null;
@@ -3303,6 +3305,7 @@ class WidgetRoot {
3303
3305
  */
3304
3306
  async fetchPrimaryGroup(productId) {
3305
3307
  var _a;
3308
+ console.log("fetch primary group called");
3306
3309
  if (!this.apiClient) {
3307
3310
  return null;
3308
3311
  }
@@ -3888,6 +3891,18 @@ class WidgetRoot {
3888
3891
  const rewardText = this.formatRewardText(reward);
3889
3892
  return rewardText ? `Save up to ${rewardText} with CoBuy` : "CoBuy reward available";
3890
3893
  }
3894
+ /**
3895
+ * Public hook to request a realtime refresh from external callers
3896
+ */
3897
+ async requestRefresh() {
3898
+ await this.refreshGroupDataFromRealtime();
3899
+ }
3900
+ /**
3901
+ * Expose current product id for optional filtering by host
3902
+ */
3903
+ getProductId() {
3904
+ return this.currentProductId;
3905
+ }
3891
3906
  }
3892
3907
 
3893
3908
  /**
@@ -8851,6 +8866,7 @@ class CoBuy {
8851
8866
  this.lobbyModal = null;
8852
8867
  this.modals = new Map();
8853
8868
  this.socketManager = null;
8869
+ this.widgets = new Set();
8854
8870
  this.configManager = new ConfigManager();
8855
8871
  this.logger = new Logger(false);
8856
8872
  }
@@ -8935,7 +8951,9 @@ class CoBuy {
8935
8951
  const ref = window.localStorage.getItem(key);
8936
8952
  if (ref) {
8937
8953
  this.logger.debug(`[SDK] Retrieved checkout reference via prefix: ${key}`);
8938
- const parsedGroupId = key.startsWith(`${basePrefix}_`) ? key.substring(basePrefix.length + 1) : null;
8954
+ const parsedGroupId = key.startsWith(`${basePrefix}_`)
8955
+ ? key.substring(basePrefix.length + 1)
8956
+ : null;
8939
8957
  return { key, checkoutRef: ref, groupId: parsedGroupId };
8940
8958
  }
8941
8959
  }
@@ -9095,6 +9113,8 @@ class CoBuy {
9095
9113
  const config = this.configManager.getConfig();
9096
9114
  const widget = new WidgetRoot(config, this.apiClient, this.analyticsClient);
9097
9115
  widget.render(options);
9116
+ // Track widget instance so we can refresh it on important lifecycle events
9117
+ this.widgets.add(widget);
9098
9118
  }
9099
9119
  catch (error) {
9100
9120
  this.logger.error("Failed to render widget", error);
@@ -9325,6 +9345,41 @@ class CoBuy {
9325
9345
  const response = await this.apiClient.confirmCheckout(groupId, checkoutRef);
9326
9346
  if (response.success) {
9327
9347
  this.logger.info(`Checkout confirmed successfully for group: ${groupId}`);
9348
+ // Directly refresh widgets. If we can infer productId, refresh only matching widgets.
9349
+ let productId = null;
9350
+ try {
9351
+ if (typeof window !== "undefined") {
9352
+ const basePrefix = `${this.CHECKOUT_REF_PREFIX}_${this.sessionId}_`;
9353
+ const ls = window.localStorage;
9354
+ for (let i = 0; i < ls.length; i++) {
9355
+ const key = ls.key(i);
9356
+ if (!key)
9357
+ continue;
9358
+ if (key.startsWith(basePrefix) && key.endsWith(`_${groupId}`)) {
9359
+ const remainder = key.substring(basePrefix.length); // <productId>_<groupId>
9360
+ const idx = remainder.lastIndexOf("_");
9361
+ if (idx > 0) {
9362
+ productId = remainder.substring(0, idx);
9363
+ }
9364
+ break;
9365
+ }
9366
+ }
9367
+ }
9368
+ }
9369
+ catch (e) {
9370
+ this.logger.debug("[SDK] Could not resolve productId for direct widget refresh", e);
9371
+ }
9372
+ const refreshPromises = [];
9373
+ this.widgets.forEach((w) => {
9374
+ const pid = typeof w.getProductId === "function" ? w.getProductId() : null;
9375
+ if (!productId || pid === productId) {
9376
+ if (typeof w.requestRefresh === "function") {
9377
+ console.log("calling refresshh now");
9378
+ refreshPromises.push(w.requestRefresh());
9379
+ }
9380
+ }
9381
+ });
9382
+ await Promise.all(refreshPromises.map((p) => p.then(() => undefined).catch(() => undefined)));
9328
9383
  }
9329
9384
  else {
9330
9385
  this.logger.error("Failed to confirm checkout", response.error);