@netless/window-manager 0.4.10 → 0.4.11-canary.0

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.
@@ -69,10 +69,14 @@ const Counter: NetlessApp<{ count: number }> = {
69
69
  decButton.addEventListener("click", decButtonOnClick);
70
70
  $content.appendChild(decButton);
71
71
 
72
- const event1Disposer = context.addMagixEventListener(`${context.appId}_event1`, msg => {
72
+ // 监听事件
73
+ const event1Disposer = context.addMagixEventListener("event1", msg => {
73
74
  console.log("event1", msg);
74
75
  });
75
76
 
77
+ // 向打开 app 的其他人发送消息
78
+ context.dispatchMagixEvent("event1", { count: 10 });
79
+
76
80
  // 应用销毁时, 注意清理掉监听器
77
81
  context.emitter.on("destroy", () => {
78
82
  incButton.removeEventListener("click", incButtonOnClick);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/window-manager",
3
- "version": "0.4.10",
3
+ "version": "0.4.11-canary.0",
4
4
  "description": "",
5
5
  "main": "dist/index.es.js",
6
6
  "module": "dist/index.es.js",
@@ -31,7 +31,7 @@
31
31
  "video.js": ">=7"
32
32
  },
33
33
  "devDependencies": {
34
- "@netless/app-docs-viewer": "^0.2.6",
34
+ "@netless/app-docs-viewer": "^0.2.8",
35
35
  "@netless/app-media-player": "0.1.0-beta.5",
36
36
  "@rollup/plugin-commonjs": "^20.0.0",
37
37
  "@rollup/plugin-node-resolve": "^13.0.4",
package/src/AppContext.ts CHANGED
@@ -165,7 +165,8 @@ export class AppContext<TAttributes = any, TMagixEventPayloads = any, TAppOption
165
165
  /** Dispatch events to other clients (and self). */
166
166
  public dispatchMagixEvent: MagixEventDispatcher<TMagixEventPayloads> = (...args) => {
167
167
  // can't dispatch events on replay mode
168
- return this.manager.room?.dispatchMagixEvent(...args);
168
+ const appScopeEvent = `${this.appId}:${args[0]}`;
169
+ return this.manager.room?.dispatchMagixEvent(appScopeEvent, args[1]);
169
170
  };
170
171
 
171
172
  /** Listen to events from others clients (and self messages). */
@@ -174,9 +175,17 @@ export class AppContext<TAttributes = any, TMagixEventPayloads = any, TAppOption
174
175
  handler,
175
176
  options
176
177
  ) => {
177
- this.manager.displayer.addMagixEventListener(event, handler as WhiteEventListener, options);
178
+ const appScopeEvent = `${this.appId}:${event}`;
179
+ this.manager.displayer.addMagixEventListener(
180
+ appScopeEvent,
181
+ handler as WhiteEventListener,
182
+ options
183
+ );
178
184
  return () =>
179
- this.manager.displayer.removeMagixEventListener(event, handler as WhiteEventListener);
185
+ this.manager.displayer.removeMagixEventListener(
186
+ appScopeEvent,
187
+ handler as WhiteEventListener
188
+ );
180
189
  };
181
190
 
182
191
  /** Remove a Magix event listener. */
package/src/AppManager.ts CHANGED
@@ -6,6 +6,7 @@ import { appRegister } from "./Register";
6
6
  import { autorun, isPlayer, isRoom, ScenePathType } from "white-web-sdk";
7
7
  import { callbacks } from "./callback";
8
8
  import { emitter } from "./InternalEmitter";
9
+ import { Fields, store } from "./AttributesDelegate";
9
10
  import { get, isInteger, orderBy } from "lodash";
10
11
  import { log } from "./Utils/log";
11
12
  import { MainViewProxy } from "./View/MainView";
@@ -13,8 +14,8 @@ import { onObjectRemoved, safeListenPropsUpdated } from "./Utils/Reactive";
13
14
  import { reconnectRefresher, WindowManager } from "./index";
14
15
  import { RedoUndo } from "./RedoUndo";
15
16
  import { SideEffectManager } from "side-effect-manager";
16
- import { store } from "./AttributesDelegate";
17
17
  import { ViewManager } from "./View/ViewManager";
18
+ import type { SyncRegisterAppPayload } from "./Register";
18
19
  import type { EmitterEvent } from "./InternalEmitter";
19
20
  import {
20
21
  entireScenes,
@@ -34,6 +35,7 @@ import type {
34
35
  SceneState,
35
36
  } from "white-web-sdk";
36
37
  import type { AddAppParams, BaseInsertParams, TeleBoxRect } from "./index";
38
+
37
39
  export class AppManager {
38
40
  public displayer: Displayer;
39
41
  public viewManager: ViewManager;
@@ -97,6 +99,10 @@ export class AppManager {
97
99
  emitter.on("setReadonly", this.onReadonlyChanged);
98
100
 
99
101
  this.createRootDirScenesCallback();
102
+
103
+ appRegister.setSyncRegisterApp(payload => {
104
+ this.safeUpdateAttributes([Fields.Registered, payload.kind], payload);
105
+ });
100
106
  }
101
107
 
102
108
  private onRemoveScenes = (scenePath: string) => {
@@ -113,7 +119,7 @@ export class AppManager {
113
119
  this.setMainViewScenePath(ROOT_DIR);
114
120
  }
115
121
  }
116
- }
122
+ };
117
123
 
118
124
  /**
119
125
  * 根目录被删除时所有的 scene 都会被删除.
@@ -133,7 +139,7 @@ export class AppManager {
133
139
  this.appProxies.forEach(appProxy => {
134
140
  appProxy.emitAppIsWritableChange();
135
141
  });
136
- }
142
+ };
137
143
 
138
144
  private onPlayerSeek = (time: number) => {
139
145
  this.appProxies.forEach(appProxy => {
@@ -141,7 +147,7 @@ export class AppManager {
141
147
  });
142
148
  this.attributesUpdateCallback(this.attributes.apps);
143
149
  this.onAppDelete(this.attributes.apps);
144
- }
150
+ };
145
151
 
146
152
  private createRootDirScenesCallback = () => {
147
153
  let isRecreate = false;
@@ -250,14 +256,7 @@ export class AppManager {
250
256
  this.refresher?.add("minimized", () => {
251
257
  return autorun(() => {
252
258
  const minimized = this.attributes.minimized;
253
- if (this.boxManager?.minimized !== minimized) {
254
- if (minimized === true) {
255
- this.boxManager?.blurAllBox();
256
- }
257
- setTimeout(() => {
258
- this.boxManager?.setMinimized(Boolean(minimized));
259
- }, 0);
260
- }
259
+ this.onMinimized(minimized);
261
260
  });
262
261
  });
263
262
  this.refresher?.add("mainViewIndex", () => {
@@ -292,6 +291,12 @@ export class AppManager {
292
291
  }
293
292
  });
294
293
  });
294
+ this.refresher?.add("registeredChange", () => {
295
+ return autorun(() => {
296
+ const registered = get(this.attributes, Fields.Registered);
297
+ this.onRegisteredChange(registered);
298
+ });
299
+ });
295
300
  if (!this.attributes.apps || Object.keys(this.attributes.apps).length === 0) {
296
301
  const mainScenePath = this.store.getMainViewScenePath();
297
302
  if (!mainScenePath) return;
@@ -362,6 +367,30 @@ export class AppManager {
362
367
  }
363
368
  }
364
369
 
370
+ private onRegisteredChange = (registered: Record<string, SyncRegisterAppPayload>) => {
371
+ if (!registered) return;
372
+ Object.entries(registered).forEach(([kind, payload]) => {
373
+ if (!appRegister.appClasses.has(kind)) {
374
+ appRegister.register({
375
+ kind,
376
+ src: payload.src,
377
+ name: payload.name,
378
+ });
379
+ }
380
+ });
381
+ };
382
+
383
+ private onMinimized = (minimized: boolean | undefined) => {
384
+ if (this.boxManager?.minimized !== minimized) {
385
+ if (minimized === true) {
386
+ this.boxManager?.blurAllBox();
387
+ }
388
+ setTimeout(() => {
389
+ this.boxManager?.setMinimized(Boolean(minimized));
390
+ }, 0);
391
+ }
392
+ }
393
+
365
394
  public refresh() {
366
395
  this.attributesUpdateCallback(this.attributes.apps);
367
396
  }
@@ -17,6 +17,7 @@ export enum Fields {
17
17
  Position = "position",
18
18
  CursorState = "cursorState",
19
19
  FullPath = "fullPath",
20
+ Registered = "registered",
20
21
  }
21
22
 
22
23
  export type Apps = {
@@ -8,12 +8,25 @@ export type LoadAppEvent = {
8
8
  reason?: string;
9
9
  };
10
10
 
11
+ export type SyncRegisterAppPayload = { kind: string, src: string, name: string | undefined };
12
+ export type SyncRegisterApp = (payload: SyncRegisterAppPayload) => void;
13
+
11
14
  class AppRegister {
12
15
  public kindEmitters: Map<string, Emittery<RegisterEvents>> = new Map();
13
16
  public registered: Map<string, RegisterParams> = new Map();
14
17
  public appClassesCache: Map<string, Promise<NetlessApp>> = new Map();
15
18
  public appClasses: Map<string, () => Promise<NetlessApp>> = new Map();
16
19
 
20
+ private syncRegisterApp: SyncRegisterApp | null = null;
21
+
22
+ public setSyncRegisterApp(fn: SyncRegisterApp) {
23
+ this.syncRegisterApp = fn;
24
+ }
25
+
26
+ public onSyncRegisterAppChange = (payload: SyncRegisterAppPayload) => {
27
+ this.register({ kind: payload.kind, src: payload.src });
28
+ }
29
+
17
30
  public async register(params: RegisterParams): Promise<void> {
18
31
  this.appClassesCache.delete(params.kind);
19
32
  this.registered.set(params.kind, params);
@@ -23,7 +36,7 @@ class AppRegister {
23
36
 
24
37
  if (typeof srcOrAppOrFunction === "string") {
25
38
  downloadApp = async () => {
26
- let appClass = (await loadApp(srcOrAppOrFunction, params.kind)) as any;
39
+ let appClass = (await loadApp(srcOrAppOrFunction, params.kind, params.name)) as any;
27
40
  if (appClass) {
28
41
  if (appClass.__esModule) {
29
42
  appClass = appClass.default;
@@ -35,6 +48,9 @@ class AppRegister {
35
48
  );
36
49
  }
37
50
  };
51
+ if (this.syncRegisterApp) {
52
+ this.syncRegisterApp({ kind: params.kind, src: srcOrAppOrFunction, name: params.name });
53
+ }
38
54
  } else if (typeof srcOrAppOrFunction === "function") {
39
55
  downloadApp = srcOrAppOrFunction;
40
56
  } else {
@@ -58,6 +74,17 @@ class AppRegister {
58
74
  }
59
75
  }
60
76
 
77
+ public unregister(kind: string) {
78
+ this.appClasses.delete(kind);
79
+ this.appClassesCache.delete(kind);
80
+ this.registered.delete(kind);
81
+ const kindEmitter = this.kindEmitters.get(kind);
82
+ if (kindEmitter) {
83
+ kindEmitter.clearListeners();
84
+ this.kindEmitters.delete(kind);
85
+ }
86
+ }
87
+
61
88
  public async notifyApp<T extends keyof RegisterEvents>(
62
89
  kind: string,
63
90
  event: T,
package/src/index.ts CHANGED
@@ -368,6 +368,13 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
368
368
  return appRegister.register(params);
369
369
  }
370
370
 
371
+ /**
372
+ * 注销插件
373
+ */
374
+ public static unregister(kind: string) {
375
+ return appRegister.unregister(kind);
376
+ }
377
+
371
378
  /**
372
379
  * 创建一个 app 至白板
373
380
  */
@@ -856,6 +863,9 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
856
863
  if (!this.attributes["_mainSceneIndex"]) {
857
864
  this.safeSetAttributes({ _mainSceneIndex: 0 });
858
865
  }
866
+ if (!this.attributes[Fields.Registered]) {
867
+ this.safeSetAttributes({ [Fields.Registered]: {} });
868
+ }
859
869
  }
860
870
  }
861
871
  }