@netless/app-slide 0.1.1 → 0.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/app-slide",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "main": "dist/main.cjs.js",
5
5
  "module": "dist/main.es.js",
6
6
  "types": "dist/index.d.ts",
@@ -55,6 +55,9 @@ export class SlideController {
55
55
 
56
56
  private syncStateOnceFlag: boolean;
57
57
 
58
+ private visible: boolean;
59
+ private savedIsFrozen: boolean;
60
+
58
61
  public constructor({
59
62
  context,
60
63
  anchor,
@@ -73,6 +76,8 @@ export class SlideController {
73
76
  this.slide = this.createSlide(anchor);
74
77
  // the adder does not need to sync state
75
78
  this.syncStateOnceFlag = !this.context.isAddApp;
79
+ this.visible = document.visibilityState === "visible";
80
+ this.savedIsFrozen = false;
76
81
  this.initialize();
77
82
  }
78
83
 
@@ -152,6 +157,11 @@ export class SlideController {
152
157
  slide.on(SLIDE_EVENTS.syncDispatch, this.onSyncDispatch);
153
158
 
154
159
  slide.on(SLIDE_EVENTS.renderEnd, this.resolveReady);
160
+
161
+ this.sideEffect.add(() => {
162
+ document.addEventListener("visibilitychange", this.onVisibilityChange);
163
+ return () => document.removeEventListener("visibilitychange", this.onVisibilityChange);
164
+ });
155
165
  }
156
166
 
157
167
  private onSyncDispatch = (event: SyncEvent) => {
@@ -308,6 +318,7 @@ export class SlideController {
308
318
  };
309
319
 
310
320
  public unfreeze = async () => {
321
+ if (!this.visible) return;
311
322
  if (this.ready) {
312
323
  log("[Slide] unfreeze", this.context.appId);
313
324
  if (this.freezePromise) {
@@ -320,4 +331,17 @@ export class SlideController {
320
331
  this._toFreeze = -1;
321
332
  }
322
333
  };
334
+
335
+ private onVisibilityChange = async () => {
336
+ if (!(this.visible = document.visibilityState === "visible")) {
337
+ this.savedIsFrozen = this.freezePromise ? this._toFreeze === 1 : this.isFrozen;
338
+ log("[Slide] freeze because tab becomes invisible");
339
+ this.freeze();
340
+ } else {
341
+ log("[Slide] unfreeze because tab becomes visible", { savedIsFrozen: this.savedIsFrozen });
342
+ if (!this.savedIsFrozen) {
343
+ this.unfreeze();
344
+ }
345
+ }
346
+ };
323
347
  }