@cshah18/sdk 3.0.1 → 3.0.3

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.
@@ -3888,6 +3888,18 @@ class WidgetRoot {
3888
3888
  const rewardText = this.formatRewardText(reward);
3889
3889
  return rewardText ? `Save up to ${rewardText} with CoBuy` : "CoBuy reward available";
3890
3890
  }
3891
+ /**
3892
+ * Public hook to request a realtime refresh from external callers
3893
+ */
3894
+ async requestRefresh() {
3895
+ await this.refreshGroupDataFromRealtime();
3896
+ }
3897
+ /**
3898
+ * Expose current product id for optional filtering by host
3899
+ */
3900
+ getProductId() {
3901
+ return this.currentProductId;
3902
+ }
3891
3903
  }
3892
3904
 
3893
3905
  /**
@@ -8851,6 +8863,7 @@ class CoBuy {
8851
8863
  this.lobbyModal = null;
8852
8864
  this.modals = new Map();
8853
8865
  this.socketManager = null;
8866
+ this.widgets = new Set();
8854
8867
  this.configManager = new ConfigManager();
8855
8868
  this.logger = new Logger(false);
8856
8869
  }
@@ -9095,6 +9108,8 @@ class CoBuy {
9095
9108
  const config = this.configManager.getConfig();
9096
9109
  const widget = new WidgetRoot(config, this.apiClient, this.analyticsClient);
9097
9110
  widget.render(options);
9111
+ // Track widget instance so we can refresh it on important lifecycle events
9112
+ this.widgets.add(widget);
9098
9113
  }
9099
9114
  catch (error) {
9100
9115
  this.logger.error("Failed to render widget", error);
@@ -9325,6 +9340,40 @@ class CoBuy {
9325
9340
  const response = await this.apiClient.confirmCheckout(groupId, checkoutRef);
9326
9341
  if (response.success) {
9327
9342
  this.logger.info(`Checkout confirmed successfully for group: ${groupId}`);
9343
+ // Directly refresh widgets. If we can infer productId, refresh only matching widgets.
9344
+ let productId = null;
9345
+ try {
9346
+ if (typeof window !== "undefined") {
9347
+ const basePrefix = `${this.CHECKOUT_REF_PREFIX}_${this.sessionId}_`;
9348
+ const ls = window.localStorage;
9349
+ for (let i = 0; i < ls.length; i++) {
9350
+ const key = ls.key(i);
9351
+ if (!key)
9352
+ continue;
9353
+ if (key.startsWith(basePrefix) && key.endsWith(`_${groupId}`)) {
9354
+ const remainder = key.substring(basePrefix.length); // <productId>_<groupId>
9355
+ const idx = remainder.lastIndexOf("_");
9356
+ if (idx > 0) {
9357
+ productId = remainder.substring(0, idx);
9358
+ }
9359
+ break;
9360
+ }
9361
+ }
9362
+ }
9363
+ }
9364
+ catch (e) {
9365
+ this.logger.debug("[SDK] Could not resolve productId for direct widget refresh", e);
9366
+ }
9367
+ const refreshPromises = [];
9368
+ this.widgets.forEach((w) => {
9369
+ const pid = typeof w.getProductId === "function" ? w.getProductId() : null;
9370
+ if (!productId || pid === productId) {
9371
+ if (typeof w.requestRefresh === "function") {
9372
+ refreshPromises.push(w.requestRefresh());
9373
+ }
9374
+ }
9375
+ });
9376
+ await Promise.all(refreshPromises.map((p) => p.then(() => undefined).catch(() => undefined)));
9328
9377
  }
9329
9378
  else {
9330
9379
  this.logger.error("Failed to confirm checkout", response.error);