@netless/app-slide 0.2.106-beta.2 → 0.2.106-beta.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.
package/dist/main.es.js CHANGED
@@ -41109,6 +41109,32 @@ function cachedGetBgColor(el) {
41109
41109
  const getRoomTracker = (displayer) => {
41110
41110
  return displayer.tracker;
41111
41111
  };
41112
+ function shouldSlideRuntimeBeActive(isFocused, isVisible, isMinimized) {
41113
+ return isFocused && isVisible && !isMinimized;
41114
+ }
41115
+ function createFocusTransitionQueue(initialState, apply, onError) {
41116
+ let acceptedState = initialState;
41117
+ let revision = 0;
41118
+ let queue = Promise.resolve();
41119
+ let acceptedPromise = Promise.resolve();
41120
+ return (isFocused, force = false) => {
41121
+ if (!force && isFocused === acceptedState)
41122
+ return acceptedPromise;
41123
+ acceptedState = isFocused;
41124
+ const transitionRevision = ++revision;
41125
+ const transition = queue.then(() => apply(isFocused)).catch((error) => {
41126
+ if (revision === transitionRevision) {
41127
+ acceptedState = void 0;
41128
+ acceptedPromise = Promise.resolve();
41129
+ }
41130
+ onError == null ? void 0 : onError(error);
41131
+ throw error;
41132
+ });
41133
+ queue = transition.catch(() => void 0);
41134
+ acceptedPromise = transition;
41135
+ return transition;
41136
+ };
41137
+ }
41112
41138
  function createDocsViewerPages(slide, previewUrls = []) {
41113
41139
  const { width, height, slideCount, slideState } = slide;
41114
41140
  const { taskId, url } = slideState;
@@ -41166,6 +41192,7 @@ const noop$1 = function noop2() {
41166
41192
  class SlideControllerBase {
41167
41193
  constructor(props) {
41168
41194
  this.sideEffect = new SideEffectManager();
41195
+ this.focused = true;
41169
41196
  this.previewList = [];
41170
41197
  this.ready = false;
41171
41198
  this.readyPromise = new Promise((resolve) => {
@@ -41174,7 +41201,11 @@ class SlideControllerBase {
41174
41201
  log("[Slide] render end", slideIndex);
41175
41202
  } else {
41176
41203
  setTimeout(() => {
41204
+ if (this.destroyed)
41205
+ return;
41177
41206
  this.ready = true;
41207
+ if (this.isLazySetupMode())
41208
+ void this.reconcileActivity(true).catch(() => void 0);
41178
41209
  resolve();
41179
41210
  }, 1e3);
41180
41211
  }
@@ -41207,6 +41238,10 @@ class SlideControllerBase {
41207
41238
  this.pollCount = 0;
41208
41239
  this.pollReadyState = () => {
41209
41240
  if (this.ready) {
41241
+ if (this.isLazySetupMode()) {
41242
+ void this.reconcileActivity().catch(() => void 0);
41243
+ return;
41244
+ }
41210
41245
  if (this._toFreeze === 1) {
41211
41246
  this.freeze();
41212
41247
  } else if (this._toFreeze === -1) {
@@ -41232,48 +41267,108 @@ class SlideControllerBase {
41232
41267
  }
41233
41268
  };
41234
41269
  this.isFrozen = false;
41270
+ this.resourceStateUnknown = false;
41271
+ this.managedActivity = false;
41235
41272
  this._toFreeze = 0;
41236
- this.freeze = () => {
41273
+ this.setFocusedState = (focused) => {
41274
+ this.focused = focused;
41275
+ return this.reconcileActivity();
41276
+ };
41277
+ this.resourceTransition = createFocusTransitionQueue(
41278
+ void 0,
41279
+ async (active) => {
41280
+ if (this.destroyed)
41281
+ return;
41282
+ if (active && this.shouldBeActive())
41283
+ await this.unfreeze();
41284
+ else
41285
+ await this.freeze();
41286
+ },
41287
+ (error) => logger.warn("[Slide] resource transition failed", this.context.appId, error)
41288
+ );
41289
+ this.shouldBeActive = () => {
41290
+ var _a, _b, _c;
41291
+ const wm = this.context.getWindowManager();
41292
+ const host = (_b = (_a = this.context).getRuntimeActivity) == null ? void 0 : _b.call(_a);
41293
+ const lazy = this.isLazySetupMode();
41294
+ const minimized = lazy && host ? !host.active : (wm == null ? void 0 : wm.boxState) === "minimized" || ((_c = wm == null ? void 0 : wm.attributes) == null ? void 0 : _c.minimized) === true || this.context.getBoxStatus() === "minimized";
41295
+ return shouldSlideRuntimeBeActive(
41296
+ !lazy || this.focused,
41297
+ document.visibilityState !== "hidden",
41298
+ minimized
41299
+ ) && (!lazy || (host == null ? void 0 : host.active) !== false);
41300
+ };
41301
+ this.reconcileActivity = (force = false) => {
41302
+ if (this.destroyed)
41303
+ return Promise.resolve();
41304
+ if (this.isLazySetupMode())
41305
+ this.managedActivity = true;
41306
+ if (!this.managedActivity)
41307
+ return Promise.resolve();
41308
+ this.visible = document.visibilityState !== "hidden";
41309
+ return this.resourceTransition(this.shouldBeActive(), force);
41310
+ };
41311
+ this.freeze = async () => {
41312
+ const previousIsFrozen = this.isFrozen;
41313
+ const previousToFreeze = this._toFreeze;
41237
41314
  this.isFrozen = true;
41238
- if (this.ready) {
41239
- log("[Slide] freeze", this.context.appId);
41240
- if (this.invisibleBehavior === "frozen") {
41241
- return Promise.resolve(this.slide.frozen());
41315
+ try {
41316
+ if (this.ready) {
41317
+ log("[Slide] freeze", this.context.appId);
41318
+ if (this.invisibleBehavior === "frozen") {
41319
+ await this.slide.frozen();
41320
+ } else {
41321
+ this.slide.pause();
41322
+ }
41242
41323
  } else {
41243
- this.slide.pause();
41324
+ this._toFreeze = 1;
41244
41325
  }
41245
- } else {
41246
- this._toFreeze = 1;
41326
+ this.resourceStateUnknown = false;
41327
+ } catch (error) {
41328
+ this.resourceStateUnknown = true;
41329
+ this.isFrozen = previousIsFrozen;
41330
+ this._toFreeze = previousToFreeze;
41331
+ throw error;
41247
41332
  }
41248
- return Promise.resolve();
41249
41333
  };
41250
41334
  this.unfreeze = async () => {
41251
41335
  if (!this.visible)
41252
41336
  return;
41253
- if (!this.isFrozen && this.ready)
41337
+ if (!this.isFrozen && this.ready && !this.resourceStateUnknown)
41254
41338
  return;
41339
+ const previousIsFrozen = this.isFrozen;
41340
+ const previousToFreeze = this._toFreeze;
41255
41341
  this.isFrozen = false;
41256
- if (this.ready) {
41257
- let isNeedSyncState = false;
41258
- log("[Slide] unfreeze", this.context.appId);
41259
- if (this.invisibleBehavior === "frozen") {
41260
- await this.slide.release(() => {
41342
+ try {
41343
+ if (this.ready) {
41344
+ let isNeedSyncState = false;
41345
+ log("[Slide] unfreeze", this.context.appId);
41346
+ if (this.invisibleBehavior === "frozen") {
41347
+ await this.slide.release(() => {
41348
+ if (!this.isLazySetupMode() || this.shouldBeActive())
41349
+ this.slide.notifyFrameResize();
41350
+ });
41351
+ isNeedSyncState = true;
41352
+ } else {
41353
+ this.slide.resume();
41261
41354
  this.slide.notifyFrameResize();
41262
- });
41263
- isNeedSyncState = true;
41264
- } else {
41265
- this.slide.resume();
41266
- this.slide.notifyFrameResize();
41267
- }
41268
- if (isNeedSyncState) {
41269
- const state = this.context.storage.state.state;
41270
- if (state) {
41271
- log("[Slide] sync storage", JSON.stringify(state));
41272
- this.slide.setSlideState(state);
41273
41355
  }
41356
+ if (isNeedSyncState && (!this.isLazySetupMode() || this.shouldBeActive())) {
41357
+ const state = this.context.storage.state.state;
41358
+ if (state) {
41359
+ log("[Slide] sync storage", JSON.stringify(state));
41360
+ await this.slide.setSlideState(state);
41361
+ }
41362
+ }
41363
+ } else {
41364
+ this._toFreeze = -1;
41274
41365
  }
41275
- } else {
41276
- this._toFreeze = -1;
41366
+ this.resourceStateUnknown = false;
41367
+ } catch (error) {
41368
+ this.resourceStateUnknown = true;
41369
+ this.isFrozen = previousIsFrozen;
41370
+ this._toFreeze = previousToFreeze;
41371
+ throw error;
41277
41372
  }
41278
41373
  };
41279
41374
  this.bindAppStateChangeEvent = () => {
@@ -41297,6 +41392,10 @@ class SlideControllerBase {
41297
41392
  }
41298
41393
  };
41299
41394
  this.onAppStateChangeHandler = (state) => {
41395
+ if (this.isLazySetupMode() || this.managedActivity) {
41396
+ void this.reconcileActivity().catch(() => void 0);
41397
+ return;
41398
+ }
41300
41399
  if (state === "minimized") {
41301
41400
  log("[Slide] freeze because app state is minimized");
41302
41401
  this.freeze();
@@ -41304,6 +41403,10 @@ class SlideControllerBase {
41304
41403
  };
41305
41404
  this.onAppStatusChangeHandler = (payload) => {
41306
41405
  const { appId, status } = payload;
41406
+ if (appId === this.context.appId && (this.isLazySetupMode() || this.managedActivity)) {
41407
+ void this.reconcileActivity().catch(() => void 0);
41408
+ return;
41409
+ }
41307
41410
  if (appId === this.context.appId && status === "minimized") {
41308
41411
  log("[Slide] freeze because app status is minimized");
41309
41412
  this.freeze();
@@ -41322,21 +41425,35 @@ class SlideControllerBase {
41322
41425
  return void 0;
41323
41426
  }
41324
41427
  };
41428
+ this.isLazySetupMode = () => {
41429
+ var _a;
41430
+ try {
41431
+ return ((_a = this.context.getWindowManager()) == null ? void 0 : _a.lazySetupInMaximizedMode) === true;
41432
+ } catch {
41433
+ return false;
41434
+ }
41435
+ };
41325
41436
  this.onVisibilityChange = async () => {
41437
+ this.visible = document.visibilityState === "visible";
41438
+ if (this.isLazySetupMode() || this.managedActivity) {
41439
+ await this.reconcileActivity().catch(() => void 0);
41440
+ return;
41441
+ }
41326
41442
  const appStatus = this.getAppStatus();
41443
+ if (!this.visible) {
41444
+ this.savedIsFrozen = this.isFrozen;
41445
+ log("[Slide] freeze because tab becomes invisible");
41446
+ if (!this.isFrozen)
41447
+ await this.freeze();
41448
+ return;
41449
+ }
41327
41450
  if (appStatus === "minimized") {
41328
41451
  log("[Slide] do nothing because app state is minimized");
41329
41452
  return;
41330
41453
  }
41331
- if (!(this.visible = document.visibilityState === "visible")) {
41332
- this.savedIsFrozen = this.isFrozen;
41333
- log("[Slide] freeze because tab becomes invisible");
41334
- this.freeze();
41335
- } else {
41336
- if (!this.savedIsFrozen) {
41337
- log("[Slide] unfreeze because tab becomes visible", { savedIsFrozen: this.savedIsFrozen });
41338
- this.unfreeze();
41339
- }
41454
+ if (!this.savedIsFrozen) {
41455
+ log("[Slide] unfreeze because tab becomes visible", { savedIsFrozen: this.savedIsFrozen });
41456
+ await this.unfreeze();
41340
41457
  }
41341
41458
  };
41342
41459
  const {
@@ -44265,7 +44382,7 @@ class SlidePreviewer {
44265
44382
  }
44266
44383
  }
44267
44384
  const usePlugin = /* @__PURE__ */ Slide.Slide.usePlugin.bind(Slide.Slide);
44268
- const version = "0.2.106-beta.2";
44385
+ const version = "0.2.106-beta.3";
44269
44386
  const DEFAULT_SLIDE_SETUP_READY_TIMEOUT = 5e3;
44270
44387
  const SlideApp = {
44271
44388
  kind: "Slide",
@@ -44392,36 +44509,19 @@ const SlideApp = {
44392
44509
  return () => room.callbacks.off("onRoomStateChanged", onRoomStateChanged);
44393
44510
  });
44394
44511
  }
44395
- const isBlurFreezeAllowed = () => {
44396
- var _a2;
44397
- const boxStatus = context.getBoxStatus();
44398
- if (boxStatus)
44399
- return boxStatus !== "normal";
44400
- const boxState = (_a2 = context.getWindowManager()) == null ? void 0 : _a2.boxState;
44401
- return boxState != null && boxState !== "normal";
44402
- };
44403
44512
  sideEffect.add(
44404
- () => context.emitter.on("focus", async (isFocused) => {
44513
+ () => context.emitter.on("focus", (isFocused) => {
44405
44514
  if (disposed)
44406
44515
  return;
44407
- if (!isLazySetupMode())
44408
- return;
44409
44516
  const controller = docsViewer == null ? void 0 : docsViewer.slideController;
44410
- if (!controller)
44411
- return;
44412
- if (!isFocused) {
44413
- if (!isBlurFreezeAllowed())
44414
- return;
44415
- if (!controller.isFrozen) {
44416
- log("[Slide] blur freeze", context.appId);
44417
- await controller.freeze();
44418
- }
44419
- return;
44420
- }
44421
- if (controller.isFrozen) {
44422
- log("[Slide] focus unfreeze", context.appId);
44423
- await controller.unfreeze();
44424
- }
44517
+ return controller == null ? void 0 : controller.setFocusedState(isFocused);
44518
+ })
44519
+ );
44520
+ sideEffect.add(
44521
+ () => context.emitter.on("runtimeActivity", () => {
44522
+ var _a2;
44523
+ if (!disposed)
44524
+ return (_a2 = docsViewer == null ? void 0 : docsViewer.slideController) == null ? void 0 : _a2.reconcileActivity();
44425
44525
  })
44426
44526
  );
44427
44527
  let disposed = false;
@@ -44453,6 +44553,7 @@ const SlideApp = {
44453
44553
  SlideApp.__teardownByContext.set(context, teardown);
44454
44554
  const waitForFirstRender = (timeoutMs) => new Promise((resolve) => {
44455
44555
  let settled = false;
44556
+ let timedOut = false;
44456
44557
  const settle = (loaded) => {
44457
44558
  if (settled)
44458
44559
  return;
@@ -44461,7 +44562,12 @@ const SlideApp = {
44461
44562
  window.clearInterval(pollTimer);
44462
44563
  resolve(loaded);
44463
44564
  };
44464
- const timeoutTimer = window.setTimeout(() => settle(false), timeoutMs);
44565
+ const timeoutTimer = window.setTimeout(() => {
44566
+ timedOut = true;
44567
+ logger.warn("[Slide] first render still pending", context.appId, timeoutMs);
44568
+ if (!context.waitForActualSetupReady)
44569
+ settle(false);
44570
+ }, timeoutMs);
44465
44571
  slideControllerRef == null ? void 0 : slideControllerRef.readyPromise.then(
44466
44572
  () => settle(!firstRenderFailed),
44467
44573
  () => settle(false)
@@ -44469,6 +44575,8 @@ const SlideApp = {
44469
44575
  const pollTimer = window.setInterval(() => {
44470
44576
  if (disposed || firstRenderFailed)
44471
44577
  settle(false);
44578
+ else if (timedOut && !context.waitForActualSetupReady)
44579
+ settle(false);
44472
44580
  }, 100);
44473
44581
  });
44474
44582
  const appResult = {
@@ -44605,6 +44713,8 @@ const SlideApp = {
44605
44713
  };
44606
44714
  const setupReadyTimeout = (_g = appOptions == null ? void 0 : appOptions.setupReadyTimeout) != null ? _g : DEFAULT_SLIDE_SETUP_READY_TIMEOUT;
44607
44715
  return waitForFirstRender(setupReadyTimeout).then(() => {
44716
+ if (disposed)
44717
+ throw new Error("[Slide] disposed before first render");
44608
44718
  if (firstRenderFailed) {
44609
44719
  throw new Error("[Slide] first render failed before ready");
44610
44720
  }