@netless/window-manager 0.3.17 → 0.4.0-canary.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/src/BoxManager.ts CHANGED
@@ -1,22 +1,22 @@
1
- import { callbacks, emitter, WindowManager } from "./index";
2
- import { debounce, maxBy } from "lodash";
3
1
  import { AppAttributes, DEFAULT_COLLECTOR_STYLE, Events, MIN_HEIGHT, MIN_WIDTH } from "./constants";
2
+ import { debounce, maxBy } from "lodash";
4
3
  import {
5
4
  TELE_BOX_MANAGER_EVENT,
6
5
  TELE_BOX_STATE,
7
6
  TeleBoxCollector,
8
7
  TeleBoxManager,
9
8
  } from "@netless/telebox-insider";
10
- import type { AddAppOptions, AppInitState } from "./index";
9
+ import { WindowManager } from "./index";
10
+ import type { AddAppOptions, AppInitState, EmitterType, CallbacksType } from "./index";
11
11
  import type {
12
12
  TeleBoxManagerUpdateConfig,
13
13
  TeleBoxManagerCreateConfig,
14
14
  ReadonlyTeleBox,
15
15
  TeleBoxManagerConfig,
16
16
  TeleBoxColorScheme,
17
+ TeleBoxRect,
17
18
  } from "@netless/telebox-insider";
18
19
  import type Emittery from "emittery";
19
- import type { AppManager } from "./AppManager";
20
20
  import type { NetlessApp } from "./typings";
21
21
  import type { View } from "white-web-sdk";
22
22
 
@@ -48,31 +48,63 @@ export type CreateTeleBoxManagerConfig = {
48
48
  prefersColorScheme?: TeleBoxColorScheme;
49
49
  };
50
50
 
51
+ export type BoxManagerContext = {
52
+ safeSetAttributes: (attributes: any) => void;
53
+ getMainView: () => View;
54
+ updateAppState: (appId: string, field: AppAttributes, value: any) => void;
55
+ emitter: EmitterType;
56
+ callbacks: CallbacksType;
57
+ canOperate: () => boolean;
58
+ notifyContainerRectUpdate: (rect: TeleBoxRect) => void;
59
+ cleanFocus: () => void;
60
+ };
61
+
62
+ export const createBoxManager = (
63
+ manager: WindowManager,
64
+ callbacks: CallbacksType,
65
+ emitter: EmitterType,
66
+ options: CreateTeleBoxManagerConfig
67
+ ) => {
68
+ return new BoxManager(
69
+ {
70
+ safeSetAttributes: (attributes: any) => manager.safeSetAttributes(attributes),
71
+ getMainView: () => manager.mainView,
72
+ updateAppState: (...args) => manager.appManager?.store.updateAppState(...args),
73
+ canOperate: () => manager.canOperate,
74
+ notifyContainerRectUpdate: (rect: TeleBoxRect) =>
75
+ manager.appManager?.notifyContainerRectUpdate(rect),
76
+ cleanFocus: () => manager.appManager?.store.cleanFocus(),
77
+ callbacks,
78
+ emitter,
79
+ },
80
+ options
81
+ );
82
+ };
83
+
51
84
  export class BoxManager {
52
85
  public teleBoxManager: TeleBoxManager;
53
- public appBoxMap: Map<string, string> = new Map();
54
- private mainView = this.manager.mainView;
55
86
 
56
87
  constructor(
57
- private manager: AppManager,
58
- createTeleBoxManagerConfig?: CreateTeleBoxManagerConfig
88
+ private context: BoxManagerContext,
89
+ private createTeleBoxManagerConfig?: CreateTeleBoxManagerConfig
59
90
  ) {
91
+ const { emitter, callbacks } = context;
60
92
  this.teleBoxManager = this.setupBoxManager(createTeleBoxManagerConfig);
61
93
  this.teleBoxManager.events.on(TELE_BOX_MANAGER_EVENT.State, state => {
62
94
  if (state) {
63
- callbacks.emit("boxStateChange", state);
64
- emitter.emit("boxStateChange", state);
95
+ this.context.callbacks.emit("boxStateChange", state);
96
+ this.context.emitter.emit("boxStateChange", state);
65
97
  }
66
98
  });
67
99
  this.teleBoxManager.events.on("minimized", minimized => {
68
- this.manager.safeSetAttributes({ minimized });
100
+ this.context.safeSetAttributes({ minimized });
69
101
  if (minimized) {
70
- this.manager.store.cleanFocus();
102
+ this.context.cleanFocus();
71
103
  this.blurAllBox();
72
104
  }
73
105
  });
74
106
  this.teleBoxManager.events.on("maximized", maximized => {
75
- this.manager.safeSetAttributes({ maximized });
107
+ this.context.safeSetAttributes({ maximized });
76
108
  });
77
109
  this.teleBoxManager.events.on("removed", boxes => {
78
110
  boxes.forEach(box => {
@@ -97,7 +129,7 @@ export class BoxManager {
97
129
  );
98
130
  this.teleBoxManager.events.on("focused", box => {
99
131
  if (box) {
100
- if (this.manager.canOperate) {
132
+ if (this.canOperate) {
101
133
  emitter.emit("focus", { appId: box.id });
102
134
  } else {
103
135
  this.teleBoxManager.blurBox(box.id);
@@ -111,10 +143,18 @@ export class BoxManager {
111
143
  callbacks.emit("prefersColorSchemeChange", colorScheme);
112
144
  });
113
145
  this.teleBoxManager.events.on("z_index", box => {
114
- this.manager.store.updateAppState(box.id, AppAttributes.ZIndex, box.zIndex);
146
+ this.context.updateAppState(box.id, AppAttributes.ZIndex, box.zIndex);
115
147
  });
116
148
  }
117
149
 
150
+ private get mainView() {
151
+ return this.context.getMainView();
152
+ }
153
+
154
+ private get canOperate() {
155
+ return this.context.canOperate();
156
+ }
157
+
118
158
  public get boxState() {
119
159
  return this.teleBoxManager.state;
120
160
  }
@@ -135,6 +175,10 @@ export class BoxManager {
135
175
  return this.teleBoxManager.prefersColorScheme;
136
176
  }
137
177
 
178
+ public get boxSize() {
179
+ return this.teleBoxManager.boxes.length;
180
+ }
181
+
138
182
  public createBox(params: CreateBoxParams): void {
139
183
  if (!this.teleBoxManager) return;
140
184
  let { minwidth = MIN_WIDTH, minheight = MIN_HEIGHT } = params.app.config ?? {};
@@ -159,14 +203,14 @@ export class BoxManager {
159
203
  id: params.appId,
160
204
  };
161
205
  this.teleBoxManager.create(createBoxConfig, params.smartPosition);
162
- emitter.emit(`${params.appId}${Events.WindowCreated}` as any);
206
+ this.context.emitter.emit(`${params.appId}${Events.WindowCreated}` as any);
163
207
  }
164
208
 
165
209
  public setBoxInitState(appId: string): void {
166
210
  const box = this.teleBoxManager.queryOne({ id: appId });
167
211
  if (box) {
168
212
  if (box.state === TELE_BOX_STATE.Maximized) {
169
- emitter.emit("resize", {
213
+ this.context.emitter.emit("resize", {
170
214
  appId: appId,
171
215
  x: box.x,
172
216
  y: box.y,
@@ -193,24 +237,30 @@ export class BoxManager {
193
237
  fence: false,
194
238
  prefersColorScheme: createTeleBoxManagerConfig?.prefersColorScheme,
195
239
  };
196
- const container = createTeleBoxManagerConfig?.collectorContainer || WindowManager.wrapper;
197
- const styles = {
198
- ...DEFAULT_COLLECTOR_STYLE,
199
- ...createTeleBoxManagerConfig?.collectorStyles,
200
- };
201
- const teleBoxCollector = new TeleBoxCollector({
202
- styles: styles,
203
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
204
- }).mount(container!);
205
- initManagerState.collector = teleBoxCollector;
240
+
206
241
  const manager = new TeleBoxManager(initManagerState);
207
242
  if (this.teleBoxManager) {
208
243
  this.teleBoxManager.destroy();
209
244
  }
210
245
  this.teleBoxManager = manager;
246
+ const container = createTeleBoxManagerConfig?.collectorContainer || WindowManager.wrapper;
247
+ if (container) {
248
+ this.setCollectorContainer(container);
249
+ }
211
250
  return manager;
212
251
  }
213
252
 
253
+ public setCollectorContainer(container: HTMLElement) {
254
+ const styles = {
255
+ ...DEFAULT_COLLECTOR_STYLE,
256
+ ...this.createTeleBoxManagerConfig?.collectorStyles,
257
+ };
258
+ const collector = new TeleBoxCollector({
259
+ styles
260
+ }).mount(container);
261
+ this.teleBoxManager.setCollector(collector);
262
+ }
263
+
214
264
  public getBox(appId: string): ReadonlyTeleBox | undefined {
215
265
  return this.teleBoxManager.queryOne({ id: appId });
216
266
  }
@@ -249,16 +299,18 @@ export class BoxManager {
249
299
  },
250
300
  true
251
301
  );
252
- if (state.maximized != null) {
253
- this.teleBoxManager.setMaximized(Boolean(state.maximized), true);
254
- this.teleBoxManager.setMinimized(Boolean(state.minimized), true);
255
- }
256
302
  setTimeout(() => {
257
303
  if (state.focus) {
258
- this.teleBoxManager.focusBox(box.id, true)
304
+ this.teleBoxManager.focusBox(box.id, true);
305
+ }
306
+ if (state.maximized != null) {
307
+ this.teleBoxManager.setMaximized(Boolean(state.maximized), true);
308
+ }
309
+ if (state.minimized != null) {
310
+ this.teleBoxManager.setMinimized(Boolean(state.minimized), true);
259
311
  }
260
312
  }, 50);
261
- callbacks.emit("boxStateChange", this.teleBoxManager.state);
313
+ this.context.callbacks.emit("boxStateChange", this.teleBoxManager.state);
262
314
  }
263
315
  }
264
316
 
@@ -267,7 +319,7 @@ export class BoxManager {
267
319
  if (rect && rect.width > 0 && rect.height > 0) {
268
320
  const containerRect = { x: 0, y: 0, width: rect.width, height: rect.height };
269
321
  this.teleBoxManager.setContainerRect(containerRect);
270
- this.manager.notifyContainerRectUpdate(this.teleBoxManager.containerRect);
322
+ this.context.notifyContainerRectUpdate(this.teleBoxManager.containerRect);
271
323
  }
272
324
  }
273
325
 
@@ -307,7 +359,9 @@ export class BoxManager {
307
359
  }
308
360
 
309
361
  public setMaximized(maximized: boolean) {
310
- this.teleBoxManager.setMaximized(maximized, true);
362
+ if (maximized !== this.maximized) {
363
+ this.teleBoxManager.setMaximized(maximized, true);
364
+ }
311
365
  }
312
366
 
313
367
  public setMinimized(minimized: boolean, skipUpdate = true) {
@@ -332,6 +386,10 @@ export class BoxManager {
332
386
  this.teleBoxManager.setPrefersColorScheme(colorScheme);
333
387
  }
334
388
 
389
+ public setZIndex(id: string, zIndex: number, skipUpdate = true) {
390
+ this.teleBoxManager.update(id, { zIndex }, skipUpdate);
391
+ }
392
+
335
393
  public destroy() {
336
394
  this.teleBoxManager.destroy();
337
395
  }
@@ -1,7 +1,6 @@
1
1
  import App from './Cursor.svelte';
2
- import pRetry from 'p-retry';
3
2
  import { ApplianceMap } from './icons';
4
- import { ApplianceNames, autorun } from 'white-web-sdk';
3
+ import { ApplianceNames } from 'white-web-sdk';
5
4
  import { CursorState } from '../constants';
6
5
  import { Fields } from '../AttributesDelegate';
7
6
  import { get, omit } from 'lodash';
@@ -19,64 +18,61 @@ export type Payload = {
19
18
 
20
19
  export class Cursor extends Base {
21
20
  private member?: RoomMember;
22
- private disposer: any;
23
21
  private timer?: number;
24
22
  private component?: SvelteComponent;
25
23
 
26
24
  constructor(
27
25
  manager: AppManager,
26
+ addCursorChangeListener: (uid: string, callback: (position: Position, state: CursorState) => void) => void,
28
27
  private cursors: any,
29
28
  private memberId: string,
30
29
  private cursorManager: CursorManager,
31
- private wrapper?: HTMLElement
30
+ private wrapper?: HTMLElement,
32
31
  ) {
33
32
  super(manager);
34
33
  this.setMember();
35
34
  this.createCursor();
36
- pRetry(() => {
37
- this.disposer && this.disposer();
38
- if (!this.cursorPosition) {
39
- console.warn(`${memberId} not exist`);
40
- }
41
- this.startReaction();
42
- }, { retries: 3 });
35
+ addCursorChangeListener(this.memberId, this.onCursorChange);
43
36
  this.autoHidden();
44
37
  }
45
38
 
46
- private startReaction() {
47
- this.disposer = autorun(() => {
48
- const cursor = this.cursorPosition;
49
- const state = this.cursorState;
50
- if (!cursor) return;
51
- if (cursor.type === "main") {
52
- const rect = this.cursorManager.wrapperRect;
53
- if (this.component && rect) {
54
- this.autoHidden();
55
- this.moveCursor(cursor, rect, this.manager.mainView);
56
- }
57
- } else {
58
- const focusView = this.cursorManager.focusView;
59
- const viewRect = focusView?.divElement?.getBoundingClientRect();
60
- const viewCamera = focusView?.camera;
61
- if (focusView && viewRect && viewCamera && this.component) {
62
- this.autoHidden();
63
- this.moveCursor(cursor, viewRect, focusView);
64
- }
39
+ private onCursorChange = (position: Position, state: CursorState) => {
40
+ if (position.type === "main") {
41
+ const rect = this.cursorManager.wrapperRect;
42
+ if (this.component && rect) {
43
+ this.autoHidden();
44
+ this.moveCursor(position, rect, this.manager.mainView);
65
45
  }
66
- if (state && state === CursorState.Leave) {
67
- this.hide();
46
+ } else {
47
+ const focusView = this.cursorManager.focusView;
48
+ // TODO 可以存一个当前 focusView 的 Rect 这样只有 focus 切换的时候才调用 getBoundingClientRect
49
+ const viewRect = focusView?.divElement?.getBoundingClientRect();
50
+ const viewCamera = focusView?.camera;
51
+ if (focusView && viewRect && viewCamera && this.component) {
52
+ this.autoHidden();
53
+ this.moveCursor(position, viewRect, focusView);
68
54
  }
69
- });
55
+ }
56
+ if (state && state === CursorState.Leave) {
57
+ this.hide();
58
+ }
70
59
  }
71
60
 
72
61
  private moveCursor(cursor: Position, rect: DOMRect, view: any) {
73
- const { x, y } = cursor;
62
+ const { x, y, type } = cursor;
74
63
  const point = view?.screen.convertPointToScreen(x, y);
75
64
  if (point) {
76
- const translateX = point.x + rect.x - 2;
77
- const translateY = point.y + rect.y - 18;
65
+ let translateX = point.x - 2;
66
+ let translateY = point.y - 18;
67
+ if (type === "app") {
68
+ const wrapperRect = this.cursorManager.wrapperRect;
69
+ if (wrapperRect) {
70
+ translateX = translateX + rect.x - wrapperRect.x;
71
+ translateY = translateY + rect.y - wrapperRect.y;
72
+ }
73
+ }
78
74
  if (point.x < 0 || point.x > rect.width || point.y < 0 || point.y > rect.height) {
79
- this.component?.$set({ visible: false });
75
+ this.component?.$set({ visible: false, x: translateX, y: translateY });
80
76
  } else {
81
77
  this.component?.$set({ visible: true, x: translateX, y: translateY });
82
78
  }
@@ -149,7 +145,7 @@ export class Cursor extends Base {
149
145
  private async createCursor() {
150
146
  if (this.member && this.wrapper) {
151
147
  this.component = new App({
152
- target: document.documentElement,
148
+ target: this.wrapper,
153
149
  props: this.initProps(),
154
150
  });
155
151
  }
@@ -189,10 +185,10 @@ export class Cursor extends Base {
189
185
  }
190
186
 
191
187
  public destroy() {
192
- this.disposer && this.disposer();
193
188
  if (this.component) {
194
189
  this.component.$destroy();
195
190
  }
191
+ this.manager.refresher?.remove(this.memberId);
196
192
  this.cursorManager.cursorInstances.delete(this.memberId);
197
193
  }
198
194
 
@@ -1,11 +1,13 @@
1
- import { Base } from '../Base';
2
- import { Cursor } from './Cursor';
3
- import { CursorState } from '../constants';
4
- import { compact, debounce, uniq } from 'lodash';
5
- import { Fields } from '../AttributesDelegate';
6
- import { onObjectInserted } from '../Utils/Reactive';
7
- import { WindowManager } from '../index';
8
- import type { PositionType } from "../AttributesDelegate";
1
+ import { autorun } from "white-web-sdk";
2
+ import { Base } from "../Base";
3
+ import { compact, debounce, get, uniq } from "lodash";
4
+ import { Cursor } from "./Cursor";
5
+ import { CursorState } from "../constants";
6
+ import { emitter, WindowManager } from "../index";
7
+ import { Fields } from "../AttributesDelegate";
8
+ import { onObjectInserted } from "../Utils/Reactive";
9
+ import { SideEffectManager } from "side-effect-manager";
10
+ import type { PositionType, Position } from "../AttributesDelegate";
9
11
  import type { Point, RoomMember, View } from "white-web-sdk";
10
12
  import type { AppManager } from "../AppManager";
11
13
 
@@ -18,28 +20,45 @@ export type MoveCursorParams = {
18
20
  uid: string;
19
21
  x: number;
20
22
  y: number;
21
- }
23
+ };
22
24
  export class CursorManager extends Base {
23
25
  public containerRect?: DOMRect;
24
26
  public wrapperRect?: DOMRect;
25
27
  public cursorInstances: Map<string, Cursor> = new Map();
26
28
  public roomMembers?: readonly RoomMember[];
27
29
  private mainViewElement?: HTMLDivElement;
30
+ private sideEffectManager = new SideEffectManager();
28
31
 
29
32
  constructor(private appManager: AppManager) {
30
33
  super(appManager);
31
34
  this.roomMembers = this.appManager.room?.state.roomMembers;
32
35
  const wrapper = WindowManager.wrapper;
33
36
  if (wrapper) {
34
- wrapper.addEventListener("mousemove", this.mouseMoveListener);
35
- wrapper.addEventListener("touchstart", this.touchMoveListener);
36
- wrapper.addEventListener("touchmove", this.touchMoveListener);
37
- wrapper.addEventListener("mouseleave", this.mouseLeaveListener);
38
- wrapper.addEventListener("touchend", this.mouseLeaveListener);
39
- this.initCursorAttributes();
40
- this.wrapperRect = wrapper.getBoundingClientRect();
41
- this.startReaction(wrapper);
37
+ this.setupWrapper(wrapper);
38
+ }
39
+ emitter.on("onReconnected", () => {
40
+ this.onReconnect();
41
+ });
42
+ }
43
+
44
+ public setupWrapper(wrapper: HTMLElement) {
45
+ if (this.manager.refresher?.hasReactor("cursors")) {
46
+ this.destroy();
42
47
  }
48
+ this.sideEffectManager.add(() => {
49
+ wrapper.addEventListener("pointerenter", this.mouseMoveListener);
50
+ wrapper.addEventListener("pointermove", this.mouseMoveListener);
51
+ wrapper.addEventListener("pointerleave", this.mouseLeaveListener);
52
+ return () => {
53
+ wrapper.removeEventListener("pointerenter", this.mouseMoveListener);
54
+ wrapper.removeEventListener("pointermove", this.mouseMoveListener);
55
+ wrapper.removeEventListener("pointerleave", this.mouseLeaveListener);
56
+ };
57
+ });
58
+
59
+ this.initCursorAttributes();
60
+ this.wrapperRect = wrapper.getBoundingClientRect();
61
+ this.startReaction(wrapper);
43
62
  }
44
63
 
45
64
  public setMainViewDivElement(div: HTMLDivElement) {
@@ -51,27 +70,25 @@ export class CursorManager extends Base {
51
70
  return onObjectInserted(this.cursors, () => {
52
71
  this.handleRoomMembersChange(wrapper);
53
72
  });
54
- })
73
+ });
55
74
  }
56
75
 
57
76
  private getUids = (members: readonly RoomMember[] | undefined) => {
58
77
  return compact(uniq(members?.map(member => member.payload?.uid)));
59
- }
78
+ };
60
79
 
61
80
  private handleRoomMembersChange = debounce((wrapper: HTMLElement) => {
62
81
  const uids = this.getUids(this.roomMembers);
63
82
  const cursors = Object.keys(this.cursors);
64
83
  if (uids?.length) {
65
84
  cursors.map(uid => {
66
- if (
67
- uids.includes(uid) &&
68
- !this.cursorInstances.has(uid)
69
- ) {
85
+ if (uids.includes(uid) && !this.cursorInstances.has(uid)) {
70
86
  if (uid === this.context.uid) {
71
87
  return;
72
88
  }
73
89
  const component = new Cursor(
74
90
  this.appManager,
91
+ this.addCursorChangeListener,
75
92
  this.cursors,
76
93
  uid,
77
94
  this,
@@ -79,7 +96,7 @@ export class CursorManager extends Base {
79
96
  );
80
97
  this.cursorInstances.set(uid, component);
81
98
  }
82
- })
99
+ });
83
100
  }
84
101
  }, 100);
85
102
 
@@ -99,13 +116,6 @@ export class CursorManager extends Base {
99
116
  this.updateCursor(this.getType(event), event.clientX, event.clientY);
100
117
  }, 5);
101
118
 
102
- private touchMoveListener = debounce((event: TouchEvent) => {
103
- if (event.touches.length === 1) {
104
- const touchEvent = event.touches[0];
105
- this.updateCursor(this.getType(touchEvent), touchEvent.clientX, touchEvent.clientY);
106
- }
107
- }, 5);
108
-
109
119
  private updateCursor(event: EventType, clientX: number, clientY: number) {
110
120
  if (this.wrapperRect && this.manager.canOperate) {
111
121
  const view = event.type === "main" ? this.appManager.mainView : this.focusView;
@@ -121,7 +131,11 @@ export class CursorManager extends Base {
121
131
  }
122
132
  }
123
133
 
124
- private getPoint = (view: View | undefined, clientX: number, clientY: number): Point | undefined => {
134
+ private getPoint = (
135
+ view: View | undefined,
136
+ clientX: number,
137
+ clientY: number
138
+ ): Point | undefined => {
125
139
  const rect = view?.divElement?.getBoundingClientRect();
126
140
  if (rect) {
127
141
  const point = view?.convertToPointInWorld({
@@ -130,7 +144,7 @@ export class CursorManager extends Base {
130
144
  });
131
145
  return point;
132
146
  }
133
- }
147
+ };
134
148
 
135
149
  /**
136
150
  * 因为窗口内框在不同分辨率下的大小不一样,所以这里通过来鼠标事件的 target 来判断是在主白板还是在 APP 中
@@ -140,7 +154,7 @@ export class CursorManager extends Base {
140
154
  const focusApp = this.appManager.focusApp;
141
155
  switch (target.parentElement) {
142
156
  case this.mainViewElement: {
143
- return { type: "main" };
157
+ return { type: "main" };
144
158
  }
145
159
  case focusApp?.view?.divElement: {
146
160
  return { type: "app" };
@@ -217,19 +231,41 @@ export class CursorManager extends Base {
217
231
  });
218
232
  }
219
233
 
220
- public destroy() {
221
- const wrapper = WindowManager.wrapper;
222
- if (wrapper) {
223
- wrapper.removeEventListener("mousemove", this.mouseMoveListener);
224
- wrapper.removeEventListener("touchstart", this.touchMoveListener);
225
- wrapper.removeEventListener("touchmove", this.touchMoveListener);
226
- wrapper.removeEventListener("mouseleave", this.mouseLeaveListener);
227
- wrapper.removeEventListener("touchend", this.mouseLeaveListener);
228
- }
234
+ public onReconnect() {
229
235
  if (this.cursorInstances.size) {
230
236
  this.cursorInstances.forEach(cursor => cursor.destroy());
231
237
  this.cursorInstances.clear();
232
238
  }
239
+ this.roomMembers = this.appManager.room?.state.roomMembers;
240
+ if (WindowManager.wrapper) {
241
+ this.handleRoomMembersChange(WindowManager.wrapper);
242
+ }
243
+ }
244
+
245
+ public addCursorChangeListener = (
246
+ uid: string,
247
+ callback: (position: Position, state: CursorState) => void
248
+ ) => {
249
+ this.manager.refresher?.add(uid, () => {
250
+ const disposer = autorun(() => {
251
+ const position = get(this.cursors, [uid, Fields.Position]);
252
+ const state = get(this.cursors, [uid, Fields.CursorState]);
253
+ if (position) {
254
+ callback(position, state);
255
+ }
256
+ });
257
+ return disposer;
258
+ });
259
+ };
260
+
261
+ public destroy() {
262
+ this.sideEffectManager.flushAll();
263
+ if (this.cursorInstances.size) {
264
+ this.cursorInstances.forEach(cursor => {
265
+ cursor.destroy();
266
+ });
267
+ this.cursorInstances.clear();
268
+ }
233
269
  this.manager.refresher?.remove("cursors");
234
270
  }
235
271
  }
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
  }