@netless/window-manager 0.4.9 → 0.4.10

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.
@@ -11,28 +11,6 @@ const HelloWorld: NetlessApp = {
11
11
  kind: "HelloWorld",
12
12
  setup: (context: AppContext) => {
13
13
  context.mountView(context.getBox().$content); // 可选: 挂载 View 到 box 上
14
-
15
- const storage = context.createStorage<{ a: number }>("HelloWorld", { a: 1 });
16
- console.log(storage.state === { a: 1 });
17
-
18
- storage.addStateChangedListener(diff => {
19
- if (diff.a) {
20
- console.log(diff.a.oldValue === 1);
21
- console.log(diff.a.newValue === 2);
22
- }
23
- });
24
-
25
- if (context.getIsWritable()) {
26
- // 只有在可写状态才可以调用 setState
27
- storage.setState({ a: 2 });
28
- }
29
-
30
- // magixEvent 事件是房间内范围的, 建议 app 内使用需要添加自己的 prefix
31
- context.addMagixEventListener(`${context.appId}_event1`, message => {
32
- console.log("MagixEvent", message);
33
- });
34
-
35
- context.dispatchMagixEvent(`${context.appId}_event1`, { count: 1 });
36
14
  },
37
15
  };
38
16
 
@@ -48,3 +26,59 @@ manager.addApp({
48
26
  },
49
27
  });
50
28
  ```
29
+
30
+ ## Counter
31
+
32
+ ```ts
33
+ const Counter: NetlessApp<{ count: number }> = {
34
+ kind: "Counter",
35
+ setup: (context) => {
36
+ const storage = context.storage;
37
+ storage.ensureState({ count: 0 });
38
+
39
+ const box = context.getBox(); // box 为这个应用打开的窗口
40
+ const $content = box.$content // 获取窗口的 content
41
+
42
+ const countDom = document.createElement("div");
43
+ countDom.innerText = storage.state.count.toString();
44
+ $content.appendChild(countDom);
45
+
46
+ // state 变化回调
47
+ storage.addStateChangedListener(diff => {
48
+ if (diff.count) {
49
+ // diff 会给出 newValue 和 oldValue
50
+ console.log(diff.count.newValue);
51
+ console.log(diff.count.oldValue);
52
+ countDom.innerText = diff.count.newValue.toString();
53
+ }
54
+ });
55
+
56
+ const incButton = document.createElement("button");
57
+ incButton.innerText = "Inc";
58
+ const incButtonOnClick = () => {
59
+ storage.setState({ count: storage.state.count + 1 });
60
+ }
61
+ incButton.addEventListener("click", incButtonOnClick);
62
+ $content.appendChild(incButton);
63
+
64
+ const decButton = document.createElement("button");
65
+ decButton.innerText = "Dec";
66
+ const decButtonOnClick = () => {
67
+ storage.setState({ count: storage.state.count - 1 });
68
+ }
69
+ decButton.addEventListener("click", decButtonOnClick);
70
+ $content.appendChild(decButton);
71
+
72
+ const event1Disposer = context.addMagixEventListener(`${context.appId}_event1`, msg => {
73
+ console.log("event1", msg);
74
+ });
75
+
76
+ // 应用销毁时, 注意清理掉监听器
77
+ context.emitter.on("destroy", () => {
78
+ incButton.removeEventListener("click", incButtonOnClick);
79
+ decButton.removeEventListener("click", decButtonOnClick);
80
+ event1Disposer();
81
+ });
82
+ }
83
+ }
84
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/window-manager",
3
- "version": "0.4.9",
3
+ "version": "0.4.10",
4
4
  "description": "",
5
5
  "main": "dist/index.es.js",
6
6
  "module": "dist/index.es.js",
package/src/AppManager.ts CHANGED
@@ -88,31 +88,31 @@ export class AppManager {
88
88
 
89
89
  emitter.once("onCreated").then(() => this.onCreated());
90
90
  emitter.on("onReconnected", () => this.onReconnected());
91
+
91
92
  if (isPlayer(this.displayer)) {
92
- emitter.on("seek", time => {
93
- this.appProxies.forEach(appProxy => {
94
- appProxy.onSeek(time);
95
- });
96
- this.attributesUpdateCallback(this.attributes.apps);
97
- this.onAppDelete(this.attributes.apps);
98
- });
93
+ emitter.on("seek", this.onPlayerSeek);
99
94
  }
100
- emitter.on("removeScenes", scenePath => {
101
- if (scenePath === ROOT_DIR) {
95
+
96
+ emitter.on("removeScenes", this.onRemoveScenes);
97
+ emitter.on("setReadonly", this.onReadonlyChanged);
98
+
99
+ this.createRootDirScenesCallback();
100
+ }
101
+
102
+ private onRemoveScenes = (scenePath: string) => {
103
+ if (scenePath === ROOT_DIR) {
104
+ this.setMainViewScenePath(ROOT_DIR);
105
+ this.createRootDirScenesCallback();
106
+ this.onRootDirRemoved();
107
+ emitter.emit("rootDirRemoved");
108
+ return;
109
+ }
110
+ const mainViewScenePath = this.store.getMainViewScenePath();
111
+ if (this.room && mainViewScenePath) {
112
+ if (mainViewScenePath === scenePath) {
102
113
  this.setMainViewScenePath(ROOT_DIR);
103
- this.createRootDirScenesCallback();
104
- this.onRootDirRemoved();
105
- emitter.emit("rootDirRemoved");
106
- return;
107
- }
108
- const mainViewScenePath = this.store.getMainViewScenePath();
109
- if (this.room && mainViewScenePath) {
110
- if (mainViewScenePath === scenePath) {
111
- this.setMainViewScenePath(ROOT_DIR);
112
- }
113
114
  }
114
- });
115
- this.createRootDirScenesCallback();
115
+ }
116
116
  }
117
117
 
118
118
  /**
@@ -129,6 +129,20 @@ export class AppManager {
129
129
  this.mainViewProxy.rebind();
130
130
  }
131
131
 
132
+ private onReadonlyChanged = () => {
133
+ this.appProxies.forEach(appProxy => {
134
+ appProxy.emitAppIsWritableChange();
135
+ });
136
+ }
137
+
138
+ private onPlayerSeek = (time: number) => {
139
+ this.appProxies.forEach(appProxy => {
140
+ appProxy.onSeek(time);
141
+ });
142
+ this.attributesUpdateCallback(this.attributes.apps);
143
+ this.onAppDelete(this.attributes.apps);
144
+ }
145
+
132
146
  private createRootDirScenesCallback = () => {
133
147
  let isRecreate = false;
134
148
  if (this.callbacksNode) {
@@ -21,6 +21,7 @@ export type EmitterEvent = {
21
21
  updateManagerRect: undefined;
22
22
  focusedChange: { focused: string | undefined; prev: string | undefined };
23
23
  rootDirRemoved: undefined;
24
+ setReadonly: boolean;
24
25
  };
25
26
 
26
27
  export type EmitterType = Emittery<EmitterEvent>;
package/src/index.ts CHANGED
@@ -517,6 +517,7 @@ export class WindowManager extends InvisiblePlugin<WindowMangerAttributes> {
517
517
  public setReadonly(readonly: boolean): void {
518
518
  this.readonly = readonly;
519
519
  this.boxManager?.setReadonly(readonly);
520
+ emitter.emit("setReadonly", readonly);
520
521
  }
521
522
 
522
523
  /**
@@ -864,3 +865,4 @@ setupBuiltin();
864
865
  export * from "./typings";
865
866
 
866
867
  export { BuiltinApps } from "./BuiltinApps";
868
+ export type { PublicEvent } from "./callback";