@netless/window-manager 0.4.0-canary.0 → 0.4.0-canary.1

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/window-manager",
3
- "version": "0.4.0-canary.0",
3
+ "version": "0.4.0-canary.1",
4
4
  "description": "",
5
5
  "main": "dist/index.es.js",
6
6
  "module": "dist/index.es.js",
@@ -28,6 +28,7 @@
28
28
  "emittery": "^0.9.2",
29
29
  "lodash": "^4.17.21",
30
30
  "p-retry": "^4.6.1",
31
+ "side-effect-manager": "^0.1.5",
31
32
  "uuid": "^7.0.3",
32
33
  "video.js": ">=7"
33
34
  },
@@ -46,6 +46,10 @@ export class AppListeners {
46
46
  this.setMainViewScenePathHandler(data.payload);
47
47
  break;
48
48
  }
49
+ case Events.MoveCameraToContain: {
50
+ this.moveCameraToContainHandler(data.payload);
51
+ break;
52
+ }
49
53
  default:
50
54
  break;
51
55
  }
@@ -72,4 +76,8 @@ export class AppListeners {
72
76
  private setMainViewScenePathHandler = ({ nextScenePath }: { nextScenePath: string }) => {
73
77
  setViewFocusScenePath(this.manager.mainView, nextScenePath);
74
78
  }
79
+
80
+ private moveCameraToContainHandler = (payload: any) => {
81
+ this.manager.mainView.moveCameraToContain(payload);
82
+ }
75
83
  }
package/src/AppManager.ts CHANGED
@@ -34,7 +34,7 @@ export class AppManager {
34
34
  this.displayer = windowManger.displayer;
35
35
  this.store.setContext({
36
36
  getAttributes: () => this.attributes,
37
- safeSetAttributes: (attributes) => this.safeSetAttributes(attributes),
37
+ safeSetAttributes: attributes => this.safeSetAttributes(attributes),
38
38
  safeUpdateAttributes: (keys, val) => this.safeUpdateAttributes(keys, val),
39
39
  });
40
40
  this.cameraStore = new CameraStore();
@@ -44,7 +44,9 @@ export class AppManager {
44
44
  this.displayer.callbacks.on(this.eventName, this.displayerStateListener);
45
45
  this.appListeners.addListeners();
46
46
 
47
- this.refresher = new ReconnectRefresher(this.room, this);
47
+ this.refresher = new ReconnectRefresher(this.room, {
48
+ notifyReconnected: () => this.notifyReconnected(),
49
+ });
48
50
 
49
51
  emitter.once("onCreated").then(() => this.onCreated());
50
52
 
@@ -116,11 +118,11 @@ export class AppManager {
116
118
  public async attributesUpdateCallback(apps: any) {
117
119
  if (apps && WindowManager.container) {
118
120
  const appIds = Object.keys(apps);
119
- const appsWithCreatedAt = appIds.map(appId => {
121
+ const appsWithCreatedAt = appIds.map(appId => {
120
122
  return {
121
123
  id: appId,
122
124
  createdAt: apps[appId].createdAt,
123
- }
125
+ };
124
126
  });
125
127
  for (const { id } of sortBy(appsWithCreatedAt, "createdAt")) {
126
128
  if (!this.appProxies.has(id) && !this.appStatus.has(id)) {
package/src/AppProxy.ts CHANGED
@@ -42,7 +42,7 @@ export class AppProxy extends Base {
42
42
  public isAddApp: boolean;
43
43
  private status: "normal" | "destroyed" = "normal";
44
44
  private stateKey: string;
45
- private setupResult?: any;
45
+ private appResult?: NetlessApp<any>;
46
46
  private appContext?: AppContext<any, any>;
47
47
 
48
48
  constructor(
@@ -171,7 +171,7 @@ export class AppProxy extends Base {
171
171
  setTimeout(async () => {
172
172
  // 延迟执行 setup, 防止初始化的属性没有更新成功
173
173
  const result = await app.setup(context);
174
- this.setupResult = result;
174
+ this.appResult = result;
175
175
  appRegister.notifyApp(app.kind, "created", { appId, result });
176
176
  this.afterSetupApp(boxInitState);
177
177
  this.fixMobileSize();
package/src/BoxManager.ts CHANGED
@@ -143,7 +143,6 @@ export class BoxManager {
143
143
  callbacks.emit("prefersColorSchemeChange", colorScheme);
144
144
  });
145
145
  this.teleBoxManager.events.on("z_index", box => {
146
- console.log("on z_index", box.id, box.zIndex);
147
146
  this.context.updateAppState(box.id, AppAttributes.ZIndex, box.zIndex);
148
147
  });
149
148
  }
package/src/MainView.ts CHANGED
@@ -5,6 +5,7 @@ import { createView } from "./ViewManager";
5
5
  import { debounce, isEmpty, isEqual } from "lodash";
6
6
  import { Fields } from "./AttributesDelegate";
7
7
  import { notifyMainViewModeChange, setViewFocusScenePath, setViewMode } from "./Utils/Common";
8
+ import { SideEffectManager } from "side-effect-manager";
8
9
  import type { Camera, Size, View } from "white-web-sdk";
9
10
  import type { AppManager } from "./AppManager";
10
11
 
@@ -16,6 +17,8 @@ export class MainViewProxy extends Base {
16
17
  private mainView: View;
17
18
  private viewId = "mainView";
18
19
 
20
+ private sideEffectManager = new SideEffectManager();
21
+
19
22
  constructor(manager: AppManager) {
20
23
  super(manager);
21
24
  this.mainView = this.createMainView();
@@ -29,8 +32,12 @@ export class MainViewProxy extends Base {
29
32
  }
30
33
  }, 200); // 等待 mainView 挂载完毕再进行监听,否则会触发不必要的 onSizeUpdated
31
34
  });
32
- emitter.on("playgroundSizeChange", () => {
35
+ const playgroundSizeChangeListener = () => {
33
36
  this.sizeChangeHandler(this.mainViewSize);
37
+ }
38
+ this.sideEffectManager.add(() => {
39
+ emitter.on("playgroundSizeChange", playgroundSizeChangeListener);
40
+ return () => emitter.off("playgroundSizeChange", playgroundSizeChangeListener);
34
41
  });
35
42
  }
36
43
 
@@ -75,7 +82,7 @@ export class MainViewProxy extends Base {
75
82
  );
76
83
  };
77
84
 
78
- private sizeChangeHandler = debounce((size: Size) => {
85
+ private sizeChangeHandler = debounce((size: Size) => {
79
86
  if (size) {
80
87
  this.moveCameraToContian(size);
81
88
  this.moveCamera(this.mainViewCamera);
@@ -104,7 +111,7 @@ export class MainViewProxy extends Base {
104
111
 
105
112
  private onCameraUpdatedByDevice = (camera: Camera) => {
106
113
  this.store.setMainViewCamera({ ...camera, id: this.context.uid });
107
- if (!isEqual(this.mainViewSize, {...this.mainView.size, id: this.context.uid})) {
114
+ if (!isEqual(this.mainViewSize, { ...this.mainView.size, id: this.context.uid })) {
108
115
  this.setMainViewSize(this.view.size);
109
116
  }
110
117
  };
@@ -204,5 +211,6 @@ export class MainViewProxy extends Base {
204
211
  public destroy() {
205
212
  this.stop();
206
213
  this.cameraStore.unregister(this.viewId, this.mainView);
214
+ this.sideEffectManager.flushAll();
207
215
  }
208
216
  }
@@ -2,7 +2,10 @@ import { isFunction, debounce } from "lodash";
2
2
  import { log } from "./Utils/log";
3
3
  import { RoomPhase } from "white-web-sdk";
4
4
  import type { Room } from "white-web-sdk";
5
- import type { AppManager } from "./AppManager";
5
+
6
+ export type ReconnectRefresherContext = {
7
+ notifyReconnected: () => void;
8
+ }
6
9
 
7
10
  // 白板重连之后会刷新所有的对象,导致 listener 失效, 所以这里在重连之后重新对所有对象进行监听
8
11
  export class ReconnectRefresher {
@@ -11,7 +14,7 @@ export class ReconnectRefresher {
11
14
  private reactors: Map<string, any> = new Map();
12
15
  private disposers: Map<string, any> = new Map();
13
16
 
14
- constructor(room: Room | undefined, private manager: AppManager) {
17
+ constructor(room: Room | undefined, private ctx: ReconnectRefresherContext) {
15
18
  this.room = room;
16
19
  this.phase = room?.phase;
17
20
  room?.callbacks.on("onPhaseChanged", this.onPhaseChanged);
@@ -32,7 +35,7 @@ export class ReconnectRefresher {
32
35
  this.disposers.set(id, func());
33
36
  }
34
37
  });
35
- this.manager.notifyReconnected();
38
+ this.ctx.notifyReconnected();
36
39
  }, 3000);
37
40
 
38
41
  private releaseDisposers() {
package/src/constants.ts CHANGED
@@ -10,6 +10,7 @@ export enum Events {
10
10
  SetMainViewScenePath = "SetMainViewScenePath",
11
11
  SetMainViewSceneIndex = "SetMainViewSceneIndex",
12
12
  SwitchViewsToFreedom = "SwitchViewsToFreedom",
13
+ MoveCameraToContain = "MoveCameraToContain"
13
14
  }
14
15
 
15
16
  export const MagixEventName = "__WindowManger";
package/src/index.ts CHANGED
@@ -6,7 +6,7 @@ import { AppManager } from "./AppManager";
6
6
  import { appRegister } from "./Register";
7
7
  import { createBoxManager } from "./BoxManager";
8
8
  import { CursorManager } from "./Cursor";
9
- import { DEFAULT_CONTAINER_RATIO, REQUIRE_VERSION } from "./constants";
9
+ import { DEFAULT_CONTAINER_RATIO, Events, REQUIRE_VERSION } from "./constants";
10
10
  import { Fields } from "./AttributesDelegate";
11
11
  import { initDb } from "./Register/storage";
12
12
  import { isNull, isObject } from "lodash";
@@ -181,7 +181,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
181
181
  public static containerSizeRatio = DEFAULT_CONTAINER_RATIO;
182
182
  private static isCreated = false;
183
183
 
184
- public version = "0.4.0-canary.0";
184
+ public version = "0.4.0-canary.1";
185
185
 
186
186
  public appListeners?: AppListeners;
187
187
 
@@ -317,34 +317,42 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
317
317
  playground.appendChild(style);
318
318
  }
319
319
  manager.observePlaygroundSize(playground, sizer, wrapper);
320
+ WindowManager.wrapper = wrapper;
320
321
  return mainViewElement;
321
322
  }
322
323
 
323
324
  public bindContainer(container: HTMLElement, collectorContainer?: HTMLElement) {
324
- if (WindowManager.params) {
325
- const params = WindowManager.params;
326
- const mainViewElement = WindowManager.initContainer(
327
- this,
328
- container,
329
- params.chessboard,
330
- params.overwriteStyles
331
- );
332
- const boxManager = createBoxManager(this, callbacks, emitter, {
333
- collectorContainer: collectorContainer,
334
- collectorStyles: params.collectorStyles,
335
- prefersColorScheme: params.prefersColorScheme,
336
- });
337
- this.boxManager = boxManager;
338
- this.appManager?.setBoxManager(boxManager);
339
- this.bindMainView(mainViewElement, params.disableCameraTransform);
340
- this.boxManager.updateManagerRect();
341
- this.appManager?.refresh();
342
- this.appManager?.resetMaximized();
343
- this.appManager?.resetMinimized();
344
- if (WindowManager.wrapper) {
345
- this.cursorManager?.setupWrapper(WindowManager.wrapper);
325
+ if (WindowManager.isCreated && WindowManager.container) {
326
+ if (WindowManager.container.firstChild) {
327
+ container.appendChild(WindowManager.container.firstChild);
328
+ }
329
+ } else {
330
+ if (WindowManager.params) {
331
+ const params = WindowManager.params;
332
+ const mainViewElement = WindowManager.initContainer(
333
+ this,
334
+ container,
335
+ params.chessboard,
336
+ params.overwriteStyles
337
+ );
338
+ const boxManager = createBoxManager(this, callbacks, emitter, {
339
+ collectorContainer: collectorContainer,
340
+ collectorStyles: params.collectorStyles,
341
+ prefersColorScheme: params.prefersColorScheme,
342
+ });
343
+ this.boxManager = boxManager;
344
+ this.appManager?.setBoxManager(boxManager);
345
+ this.bindMainView(mainViewElement, params.disableCameraTransform);
346
+ if (WindowManager.wrapper) {
347
+ this.cursorManager?.setupWrapper(WindowManager.wrapper);
348
+ }
346
349
  }
347
350
  }
351
+ this.boxManager?.updateManagerRect();
352
+ this.appManager?.refresh();
353
+ this.appManager?.resetMaximized();
354
+ this.appManager?.resetMinimized();
355
+ WindowManager.container = container;
348
356
  }
349
357
 
350
358
  /**
@@ -356,15 +364,6 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
356
364
  return appRegister.register(params);
357
365
  }
358
366
 
359
- public static setContainer(container: HTMLElement) {
360
- if (this.isCreated && WindowManager.container) {
361
- if (WindowManager.container.firstChild) {
362
- container.appendChild(WindowManager.container.firstChild);
363
- }
364
- }
365
- WindowManager.container = container;
366
- }
367
-
368
367
  public static setCollectorContainer(container: HTMLElement) {
369
368
  const manager = this.displayer.getInvisiblePlugin(this.kind) as WindowManager;
370
369
  if (this.isCreated && manager) {
@@ -593,6 +592,10 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
593
592
  }>
594
593
  ): void {
595
594
  this.mainView.moveCameraToContain(rectangle);
595
+ this.appManager?.dispatchInternalEvent(Events.MoveCameraToContain, rectangle);
596
+ setTimeout(() => {
597
+ this.appManager?.mainViewProxy.setCameraAndSize();
598
+ }, 1000);
596
599
  }
597
600
 
598
601
  public convertToPointInWorld(point: Point): Point {