@unhingged/vizu-core 0.1.7 → 0.1.9

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/dist/auto.cjs CHANGED
@@ -406,7 +406,9 @@ var CloudStorageAdapter = class {
406
406
  const payload = params.get("vizu_auth");
407
407
  if (!payload) return null;
408
408
  try {
409
- const json = JSON.parse(atob(payload));
409
+ const bytes = Uint8Array.from(atob(payload), (c) => c.charCodeAt(0));
410
+ const decoded = new TextDecoder("utf-8").decode(bytes);
411
+ const json = JSON.parse(decoded);
410
412
  if (!json.t || !json.e || !json.u || !json.w) return null;
411
413
  if (json.w !== this.workspace) return null;
412
414
  const stored = {
@@ -460,6 +462,11 @@ var CloudStorageAdapter = class {
460
462
  }
461
463
  /* ─── StorageAdapter v2 ───────────────────────────────────────────── */
462
464
  async load(_namespace) {
465
+ if (!this.cachedToken || this.isExpired(this.cachedToken)) {
466
+ const fresh = this.readStoredToken();
467
+ if (!fresh || this.isExpired(fresh)) return [];
468
+ this.cachedToken = fresh;
469
+ }
463
470
  const out = [];
464
471
  let cursor = null;
465
472
  do {
@@ -3177,6 +3184,14 @@ var Vizu = class {
3177
3184
  this.selfHealedThisSession = /* @__PURE__ */ new Set();
3178
3185
  this.actions = [];
3179
3186
  this.user = null;
3187
+ /**
3188
+ * Flipped true once the initial loadComments() at construction has
3189
+ * settled (success or failure). The cloud onAuthChanged callback uses
3190
+ * this to decide whether a subsequent auth change should trigger a
3191
+ * re-load: skipped for the initial fire (the explicit loadComments
3192
+ * below handles it), fired for every later token acquisition.
3193
+ */
3194
+ this.hasLoadedComments = false;
3180
3195
  this.enabled = false;
3181
3196
  this.rafScheduled = false;
3182
3197
  this.bus = new EventBus();
@@ -3245,21 +3260,28 @@ var Vizu = class {
3245
3260
  this.opts.namespace = options.namespace ?? DEFAULTS.namespace;
3246
3261
  this.opts.accent = options.accent ?? DEFAULTS.accent;
3247
3262
  this.parsedShortcut = parseShortcut(this.opts.shortcut);
3263
+ this.user = options.user ?? null;
3248
3264
  this.storage = resolveStorage(
3249
3265
  options.storage,
3250
3266
  options.cloud,
3251
3267
  _defaultStorage,
3252
3268
  (info) => {
3253
3269
  if (info.user) this.setUser(info.user);
3270
+ if (this.hasLoadedComments) {
3271
+ void this.loadComments();
3272
+ }
3254
3273
  }
3255
3274
  );
3256
- this.user = options.user ?? null;
3257
3275
  if (options.actions) this.actions = [...options.actions];
3258
3276
  if (options.onCommentAdded) this.bus.on("comment:added", (p) => options.onCommentAdded(p.comment));
3259
3277
  if (options.onCommentRemoved) this.bus.on("comment:removed", (p) => options.onCommentRemoved(p.id));
3260
3278
  if (typeof window !== "undefined") {
3261
3279
  window.addEventListener("keydown", this.onKeyDown);
3262
- void this.loadComments().then(() => this.subscribeStorage());
3280
+ void this.loadComments().catch(() => {
3281
+ }).finally(() => {
3282
+ this.hasLoadedComments = true;
3283
+ this.subscribeStorage();
3284
+ });
3263
3285
  if (options.startEnabled) this.deferred(() => this.enable());
3264
3286
  }
3265
3287
  }