@netless/window-manager 0.4.0-canary.1 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/window-manager",
3
- "version": "0.4.0-canary.1",
3
+ "version": "0.4.0-canary.2",
4
4
  "description": "",
5
5
  "main": "dist/index.es.js",
6
6
  "module": "dist/index.es.js",
package/src/AppManager.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import pRetry from "p-retry";
2
- import { sortBy } from "lodash";
3
2
  import { AppAttributes, AppStatus, Events, MagixEventName } from "./constants";
4
3
  import { AppListeners } from "./AppListener";
5
4
  import { AppProxy } from "./AppProxy";
6
- import { store } from "./AttributesDelegate";
7
5
  import { autorun, isPlayer, isRoom, ScenePathType, ViewVisionMode } from "white-web-sdk";
8
6
  import { callbacks, emitter, WindowManager } from "./index";
9
7
  import { CameraStore } from "./Utils/CameraStore";
@@ -11,11 +9,15 @@ import { genAppId, makeValidScenePath, setScenePath } from "./Utils/Common";
11
9
  import { log } from "./Utils/log";
12
10
  import { MainViewProxy } from "./MainView";
13
11
  import { onObjectRemoved, safeListenPropsUpdated } from "./Utils/Reactive";
14
- import { ReconnectRefresher } from "./ReconnectRefresher";
12
+ import { reconnectRefresher } from "./ReconnectRefresher";
13
+ import { sortBy } from "lodash";
14
+ import { store } from "./AttributesDelegate";
15
15
  import { ViewManager } from "./ViewManager";
16
+ import type { ReconnectRefresher } from "./ReconnectRefresher";
16
17
  import type { BoxManager } from "./BoxManager";
17
18
  import type { Displayer, DisplayerState, Room } from "white-web-sdk";
18
19
  import type { AddAppParams, BaseInsertParams, TeleBoxRect, EmitterEvent } from "./index";
20
+
19
21
  export class AppManager {
20
22
  public displayer: Displayer;
21
23
  public cameraStore: CameraStore;
@@ -44,12 +46,12 @@ export class AppManager {
44
46
  this.displayer.callbacks.on(this.eventName, this.displayerStateListener);
45
47
  this.appListeners.addListeners();
46
48
 
47
- this.refresher = new ReconnectRefresher(this.room, {
48
- notifyReconnected: () => this.notifyReconnected(),
49
- });
49
+ this.refresher = reconnectRefresher;
50
+ this.refresher.setRoom(this.room);
51
+ this.refresher.setContext({ emitter });
50
52
 
51
53
  emitter.once("onCreated").then(() => this.onCreated());
52
-
54
+ emitter.on("onReconnected", () => this.onReconnected());
53
55
  if (isPlayer(this.displayer)) {
54
56
  emitter.on("seek", time => {
55
57
  this.appProxies.forEach(appProxy => {
@@ -433,7 +435,7 @@ export class AppManager {
433
435
  }
434
436
  }
435
437
 
436
- public async notifyReconnected() {
438
+ public async onReconnected() {
437
439
  const appProxies = Array.from(this.appProxies.values());
438
440
  const reconnected = appProxies.map(appProxy => {
439
441
  return appProxy.onReconnected();
@@ -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,54 +18,44 @@ 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) {
@@ -196,10 +185,10 @@ export class Cursor extends Base {
196
185
  }
197
186
 
198
187
  public destroy() {
199
- this.disposer && this.disposer();
200
188
  if (this.component) {
201
189
  this.component.$destroy();
202
190
  }
191
+ this.manager.refresher?.remove(this.memberId);
203
192
  this.cursorManager.cursorInstances.delete(this.memberId);
204
193
  }
205
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,32 +20,42 @@ 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
- this.setupWrapper(wrapper);
37
+ this.setupWrapper(wrapper);
35
38
  }
39
+ emitter.on("onReconnected", () => {
40
+ this.onReconnect();
41
+ });
36
42
  }
37
43
 
38
44
  public setupWrapper(wrapper: HTMLElement) {
39
45
  if (this.manager.refresher?.hasReactor("cursors")) {
40
46
  this.destroy();
41
47
  }
42
- wrapper.addEventListener("mousemove", this.mouseMoveListener);
43
- wrapper.addEventListener("touchstart", this.touchMoveListener);
44
- wrapper.addEventListener("touchmove", this.touchMoveListener);
45
- wrapper.addEventListener("mouseleave", this.mouseLeaveListener);
46
- wrapper.addEventListener("touchend", this.mouseLeaveListener);
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
+
47
59
  this.initCursorAttributes();
48
60
  this.wrapperRect = wrapper.getBoundingClientRect();
49
61
  this.startReaction(wrapper);
@@ -58,27 +70,25 @@ export class CursorManager extends Base {
58
70
  return onObjectInserted(this.cursors, () => {
59
71
  this.handleRoomMembersChange(wrapper);
60
72
  });
61
- })
73
+ });
62
74
  }
63
75
 
64
76
  private getUids = (members: readonly RoomMember[] | undefined) => {
65
77
  return compact(uniq(members?.map(member => member.payload?.uid)));
66
- }
78
+ };
67
79
 
68
80
  private handleRoomMembersChange = debounce((wrapper: HTMLElement) => {
69
81
  const uids = this.getUids(this.roomMembers);
70
82
  const cursors = Object.keys(this.cursors);
71
83
  if (uids?.length) {
72
84
  cursors.map(uid => {
73
- if (
74
- uids.includes(uid) &&
75
- !this.cursorInstances.has(uid)
76
- ) {
85
+ if (uids.includes(uid) && !this.cursorInstances.has(uid)) {
77
86
  if (uid === this.context.uid) {
78
87
  return;
79
88
  }
80
89
  const component = new Cursor(
81
90
  this.appManager,
91
+ this.addCursorChangeListener,
82
92
  this.cursors,
83
93
  uid,
84
94
  this,
@@ -86,7 +96,7 @@ export class CursorManager extends Base {
86
96
  );
87
97
  this.cursorInstances.set(uid, component);
88
98
  }
89
- })
99
+ });
90
100
  }
91
101
  }, 100);
92
102
 
@@ -106,13 +116,6 @@ export class CursorManager extends Base {
106
116
  this.updateCursor(this.getType(event), event.clientX, event.clientY);
107
117
  }, 5);
108
118
 
109
- private touchMoveListener = debounce((event: TouchEvent) => {
110
- if (event.touches.length === 1) {
111
- const touchEvent = event.touches[0];
112
- this.updateCursor(this.getType(touchEvent), touchEvent.clientX, touchEvent.clientY);
113
- }
114
- }, 5);
115
-
116
119
  private updateCursor(event: EventType, clientX: number, clientY: number) {
117
120
  if (this.wrapperRect && this.manager.canOperate) {
118
121
  const view = event.type === "main" ? this.appManager.mainView : this.focusView;
@@ -128,7 +131,11 @@ export class CursorManager extends Base {
128
131
  }
129
132
  }
130
133
 
131
- 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 => {
132
139
  const rect = view?.divElement?.getBoundingClientRect();
133
140
  if (rect) {
134
141
  const point = view?.convertToPointInWorld({
@@ -137,7 +144,7 @@ export class CursorManager extends Base {
137
144
  });
138
145
  return point;
139
146
  }
140
- }
147
+ };
141
148
 
142
149
  /**
143
150
  * 因为窗口内框在不同分辨率下的大小不一样,所以这里通过来鼠标事件的 target 来判断是在主白板还是在 APP 中
@@ -147,7 +154,7 @@ export class CursorManager extends Base {
147
154
  const focusApp = this.appManager.focusApp;
148
155
  switch (target.parentElement) {
149
156
  case this.mainViewElement: {
150
- return { type: "main" };
157
+ return { type: "main" };
151
158
  }
152
159
  case focusApp?.view?.divElement: {
153
160
  return { type: "app" };
@@ -224,19 +231,41 @@ export class CursorManager extends Base {
224
231
  });
225
232
  }
226
233
 
227
- public destroy() {
228
- const wrapper = WindowManager.wrapper;
229
- if (wrapper) {
230
- wrapper.removeEventListener("mousemove", this.mouseMoveListener);
231
- wrapper.removeEventListener("touchstart", this.touchMoveListener);
232
- wrapper.removeEventListener("touchmove", this.touchMoveListener);
233
- wrapper.removeEventListener("mouseleave", this.mouseLeaveListener);
234
- wrapper.removeEventListener("touchend", this.mouseLeaveListener);
235
- }
234
+ public onReconnect() {
236
235
  if (this.cursorInstances.size) {
237
236
  this.cursorInstances.forEach(cursor => cursor.destroy());
238
237
  this.cursorInstances.clear();
239
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
+ }
240
269
  this.manager.refresher?.remove("cursors");
241
270
  }
242
271
  }
@@ -1,11 +1,13 @@
1
- import { isFunction, debounce } from "lodash";
1
+ import { debounce, isFunction } from "lodash";
2
+ import { emitter } from "./index";
2
3
  import { log } from "./Utils/log";
3
4
  import { RoomPhase } from "white-web-sdk";
4
5
  import type { Room } from "white-web-sdk";
6
+ import type { EmitterType } from "./index";
5
7
 
6
8
  export type ReconnectRefresherContext = {
7
- notifyReconnected: () => void;
8
- }
9
+ emitter: EmitterType;
10
+ };
9
11
 
10
12
  // 白板重连之后会刷新所有的对象,导致 listener 失效, 所以这里在重连之后重新对所有对象进行监听
11
13
  export class ReconnectRefresher {
@@ -14,12 +16,19 @@ export class ReconnectRefresher {
14
16
  private reactors: Map<string, any> = new Map();
15
17
  private disposers: Map<string, any> = new Map();
16
18
 
17
- constructor(room: Room | undefined, private ctx: ReconnectRefresherContext) {
19
+ constructor(private ctx: ReconnectRefresherContext) {}
20
+
21
+ public setRoom(room: Room | undefined) {
18
22
  this.room = room;
19
23
  this.phase = room?.phase;
24
+ room?.callbacks.off("onPhaseChanged", this.onPhaseChanged);
20
25
  room?.callbacks.on("onPhaseChanged", this.onPhaseChanged);
21
26
  }
22
27
 
28
+ public setContext(ctx: ReconnectRefresherContext) {
29
+ this.ctx = ctx;
30
+ }
31
+
23
32
  private onPhaseChanged = (phase: RoomPhase) => {
24
33
  if (phase === RoomPhase.Connected && this.phase === RoomPhase.Reconnecting) {
25
34
  this.onReconnected();
@@ -35,7 +44,7 @@ export class ReconnectRefresher {
35
44
  this.disposers.set(id, func());
36
45
  }
37
46
  });
38
- this.ctx.notifyReconnected();
47
+ this.ctx.emitter.emit("onReconnected", undefined);
39
48
  }, 3000);
40
49
 
41
50
  private releaseDisposers() {
@@ -76,3 +85,7 @@ export class ReconnectRefresher {
76
85
  this.releaseDisposers();
77
86
  }
78
87
  }
88
+
89
+ export const reconnectRefresher = new ReconnectRefresher({
90
+ emitter: emitter,
91
+ });
package/src/index.ts CHANGED
@@ -81,14 +81,14 @@ export type AddAppOptions = {
81
81
 
82
82
  export type setAppOptions = AddAppOptions & { appOptions?: any };
83
83
 
84
- export type AddAppParams = {
84
+ export type AddAppParams<TAttributes = any> = {
85
85
  kind: string;
86
86
  // app 地址(本地 app 不需要传)
87
87
  src?: string;
88
88
  // 窗口配置
89
89
  options?: AddAppOptions;
90
90
  // 初始化 attributes
91
- attributes?: any;
91
+ attributes?: TAttributes;
92
92
  };
93
93
 
94
94
  export type BaseInsertParams = {
@@ -139,6 +139,7 @@ export type EmitterEvent = {
139
139
  observerIdChange: number;
140
140
  boxStateChange: string;
141
141
  playgroundSizeChange: DOMRect;
142
+ onReconnected: void;
142
143
  };
143
144
 
144
145
  export type EmitterType = Emittery<EmitterEvent>;
@@ -181,7 +182,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
181
182
  public static containerSizeRatio = DEFAULT_CONTAINER_RATIO;
182
183
  private static isCreated = false;
183
184
 
184
- public version = "0.4.0-canary.1";
185
+ public version = "0.4.0-canary.2";
185
186
 
186
187
  public appListeners?: AppListeners;
187
188
 
@@ -254,7 +255,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
254
255
  }
255
256
 
256
257
  if (params.container) {
257
- manager.bindContainer(params.container, params.collectorContainer);
258
+ manager.bindContainer(params.container);
258
259
  }
259
260
 
260
261
  replaceRoomFunction(room, manager);
@@ -321,7 +322,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
321
322
  return mainViewElement;
322
323
  }
323
324
 
324
- public bindContainer(container: HTMLElement, collectorContainer?: HTMLElement) {
325
+ public bindContainer(container: HTMLElement) {
325
326
  if (WindowManager.isCreated && WindowManager.container) {
326
327
  if (WindowManager.container.firstChild) {
327
328
  container.appendChild(WindowManager.container.firstChild);
@@ -336,7 +337,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
336
337
  params.overwriteStyles
337
338
  );
338
339
  const boxManager = createBoxManager(this, callbacks, emitter, {
339
- collectorContainer: collectorContainer,
340
+ collectorContainer: params.collectorContainer,
340
341
  collectorStyles: params.collectorStyles,
341
342
  prefersColorScheme: params.prefersColorScheme,
342
343
  });
@@ -355,6 +356,16 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
355
356
  WindowManager.container = container;
356
357
  }
357
358
 
359
+ public bindCollectorContainer(container: HTMLElement) {
360
+ if (WindowManager.isCreated) {
361
+ this.boxManager?.setCollectorContainer(container);
362
+ } else {
363
+ if (WindowManager.params) {
364
+ WindowManager.params.collectorContainer = container;
365
+ }
366
+ }
367
+ }
368
+
358
369
  /**
359
370
  * 注册插件
360
371
  */
@@ -364,21 +375,10 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
364
375
  return appRegister.register(params);
365
376
  }
366
377
 
367
- public static setCollectorContainer(container: HTMLElement) {
368
- const manager = this.displayer.getInvisiblePlugin(this.kind) as WindowManager;
369
- if (this.isCreated && manager) {
370
- manager.boxManager?.setCollectorContainer(container);
371
- } else {
372
- if (this.params) {
373
- this.params.collectorContainer = container;
374
- }
375
- }
376
- }
377
-
378
378
  /**
379
379
  * 创建一个 app 至白板
380
380
  */
381
- public async addApp(params: AddAppParams): Promise<string | undefined> {
381
+ public async addApp<T = any>(params: AddAppParams<T>): Promise<string | undefined> {
382
382
  if (this.appManager) {
383
383
  if (!params.kind || typeof params.kind !== "string") {
384
384
  throw new ParamsInvalidError();