@virid/renderer 0.3.0 → 0.3.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/README.md +71 -56
- package/README.zh.md +65 -53
- package/dist/index.cjs +4 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -12
- package/dist/index.d.ts +11 -12
- package/dist/index.js +4 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -12,18 +12,18 @@ Conversely, messages sent from the main process to the rendering process are abs
|
|
|
12
12
|
- **Type Restoration**: After passing through the IPC channel, messages are restored into their original **Message** classes before re-entering `@virid/core`. This allows them to be recognized and distributed by the dispatcher, achieving **location transparency** across different processes.
|
|
13
13
|
- **Targeted Messaging & Broadcasting**: You can achieve point-to-point communication using the IDs of other rendering processes, or use `*` to broadcast messages.
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
---
|
|
16
16
|
|
|
17
17
|
## 🔌Enable plugins
|
|
18
18
|
|
|
19
19
|
```ts
|
|
20
|
-
import { createVirid } from
|
|
21
|
-
import { RenderPlugin } from
|
|
22
|
-
const app = createVirid()
|
|
20
|
+
import { createVirid } from "@virid/core";
|
|
21
|
+
import { RenderPlugin } from "@virid/renderer";
|
|
22
|
+
const app = createVirid();
|
|
23
23
|
//Each window needs to be assigned a unique windowId to report to the main process
|
|
24
24
|
app.use(RenderPlugin, {
|
|
25
|
-
windowId:
|
|
26
|
-
})
|
|
25
|
+
windowId: "renderer",
|
|
26
|
+
});
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
## 🛠️ @virid/renderer Core API Overview
|
|
@@ -33,34 +33,34 @@ app.use(RenderPlugin, {
|
|
|
33
33
|
- **Function**: A specialized message base class designed for inheritance. All messages inheriting from `ToMainMessage` will be dispatched to other processes.
|
|
34
34
|
- **Logic**: This message type requires two specific metadata tags:
|
|
35
35
|
- `__virid_target`: Defines the destination.
|
|
36
|
-
- `
|
|
36
|
+
- `__virid_message_type`: Defines the message type it should be restored to upon reaching the destination.
|
|
37
37
|
- Example
|
|
38
38
|
|
|
39
39
|
**In the Rendering Process:**
|
|
40
40
|
|
|
41
41
|
```ts
|
|
42
|
-
import { ToMainMessage } from
|
|
42
|
+
import { ToMainMessage } from "@virid/renderer";
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* - __virid_target = 'main': Indicates the message is bound for the Main Process.
|
|
46
46
|
* - If __virid_target = '*': The message will be broadcast to all rendering processes.
|
|
47
|
-
* -
|
|
47
|
+
* - __virid_message_type: Describes the class type the message will transform back into
|
|
48
48
|
* once it arrives at the Main Process.
|
|
49
49
|
*/
|
|
50
50
|
|
|
51
51
|
export class CloseWindowMessage extends ToMainMessage {
|
|
52
|
-
__virid_target =
|
|
53
|
-
|
|
52
|
+
__virid_target = "main";
|
|
53
|
+
__virid_message_type: string = "close-window";
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
export class MinimizeWindowMessage extends ToMainMessage {
|
|
57
|
-
__virid_target =
|
|
58
|
-
|
|
57
|
+
__virid_target = "main";
|
|
58
|
+
__virid_message_type: string = "minimize-window";
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
export class MaximizeWindowMessage extends ToMainMessage {
|
|
62
|
-
__virid_target =
|
|
63
|
-
|
|
62
|
+
__virid_target = "main";
|
|
63
|
+
__virid_message_type: string = "maximize-window";
|
|
64
64
|
}
|
|
65
65
|
```
|
|
66
66
|
|
|
@@ -69,16 +69,16 @@ In the **Main Process**, you can use the `@FromRenderer` decorator in conjunctio
|
|
|
69
69
|
**In the Main Process:**
|
|
70
70
|
|
|
71
71
|
```ts
|
|
72
|
-
import { FromRendererMessage, FromRenderer } from
|
|
72
|
+
import { FromRendererMessage, FromRenderer } from "@virid/main";
|
|
73
73
|
|
|
74
|
-
// Each ID here maps to the
|
|
75
|
-
@FromRenderer(
|
|
74
|
+
// Each ID here maps to the __virid_message_type defined in the rendering process
|
|
75
|
+
@FromRenderer("close-window")
|
|
76
76
|
export class CloseWindowMessage extends FromRendererMessage {}
|
|
77
77
|
|
|
78
|
-
@FromRenderer(
|
|
78
|
+
@FromRenderer("minimize-window")
|
|
79
79
|
export class MinimizeWindowMessage extends FromRendererMessage {}
|
|
80
80
|
|
|
81
|
-
@FromRenderer(
|
|
81
|
+
@FromRenderer("maximize-window")
|
|
82
82
|
export class MaximizeWindowMessage extends FromRendererMessage {}
|
|
83
83
|
|
|
84
84
|
/**
|
|
@@ -88,20 +88,24 @@ export class MaximizeWindowMessage extends FromRendererMessage {}
|
|
|
88
88
|
export class WindowSystem {
|
|
89
89
|
@System()
|
|
90
90
|
closeWindow(@Message(CloseWindowMessage) message: CloseWindowMessage) {
|
|
91
|
-
message.senderWindow.close()
|
|
91
|
+
message.senderWindow.close();
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
@System()
|
|
95
|
-
minimizeWindow(
|
|
96
|
-
|
|
95
|
+
minimizeWindow(
|
|
96
|
+
@Message(MinimizeWindowMessage) message: MinimizeWindowMessage,
|
|
97
|
+
) {
|
|
98
|
+
message.senderWindow.minimize();
|
|
97
99
|
}
|
|
98
100
|
|
|
99
101
|
@System()
|
|
100
|
-
maximizeWindow(
|
|
102
|
+
maximizeWindow(
|
|
103
|
+
@Message(MaximizeWindowMessage) message: MaximizeWindowMessage,
|
|
104
|
+
) {
|
|
101
105
|
if (message.senderWindow.isMaximized()) {
|
|
102
|
-
message.senderWindow.unmaximize()
|
|
106
|
+
message.senderWindow.unmaximize();
|
|
103
107
|
} else {
|
|
104
|
-
message.senderWindow.maximize()
|
|
108
|
+
message.senderWindow.maximize();
|
|
105
109
|
}
|
|
106
110
|
}
|
|
107
111
|
}
|
|
@@ -110,36 +114,38 @@ export class WindowSystem {
|
|
|
110
114
|
### FromMainMessage / @FromIpc()
|
|
111
115
|
|
|
112
116
|
- **Function**: A specialized message base class for inheritance. Any message inheriting from `FromMainMessage` is automatically converted and dispatched to the current process's **Virid Dispatcher** when sent from the Main Process.
|
|
113
|
-
- **Logic**: The `@FromIpc()` decorator accepts a string-based ID. This ID must match the `
|
|
117
|
+
- **Logic**: The `@FromIpc()` decorator accepts a string-based ID. This ID must match the `__virid_message_type` of the `ToRendererMessage` sent from the Main Process.
|
|
114
118
|
|
|
115
119
|
- Example:
|
|
116
120
|
|
|
117
121
|
**In the Rendering Process:**
|
|
118
122
|
|
|
119
123
|
```ts
|
|
120
|
-
import { FromIpc, FromMainMessage, ToMainMessage } from
|
|
124
|
+
import { FromIpc, FromMainMessage, ToMainMessage } from "@virid/renderer";
|
|
121
125
|
|
|
122
126
|
// This class handles the incoming file path from the Main Process
|
|
123
|
-
@FromIpc(
|
|
127
|
+
@FromIpc("file-dialog")
|
|
124
128
|
class ChooseBgImageMessage extends FromMainMessage {
|
|
125
129
|
constructor(public path: string) {
|
|
126
|
-
super()
|
|
130
|
+
super();
|
|
127
131
|
}
|
|
128
132
|
}
|
|
129
133
|
|
|
130
134
|
// This class initiates the request to the Main Process
|
|
131
135
|
class OpenDialogMessage extends ToMainMessage {
|
|
132
|
-
__virid_target: string =
|
|
133
|
-
|
|
134
|
-
|
|
136
|
+
__virid_target: string = "main";
|
|
137
|
+
__virid_message_type: string = "open-dialog";
|
|
138
|
+
|
|
135
139
|
constructor(
|
|
136
140
|
public options: {
|
|
137
|
-
title?: string
|
|
138
|
-
filters?: Array<{ name: string; extensions: string[] }
|
|
139
|
-
properties?: Array<
|
|
140
|
-
|
|
141
|
+
title?: string;
|
|
142
|
+
filters?: Array<{ name: string; extensions: string[] }>;
|
|
143
|
+
properties?: Array<
|
|
144
|
+
"openFile" | "openDirectory" | "multiSelections" | "showHiddenFiles"
|
|
145
|
+
>;
|
|
146
|
+
},
|
|
141
147
|
) {
|
|
142
|
-
super()
|
|
148
|
+
super();
|
|
143
149
|
}
|
|
144
150
|
}
|
|
145
151
|
```
|
|
@@ -149,36 +155,42 @@ In the **Main Process**, you use `@FromRenderer` and `FromRendererMessage` to ha
|
|
|
149
155
|
**In the Main Process:**
|
|
150
156
|
|
|
151
157
|
```ts
|
|
152
|
-
import {
|
|
158
|
+
import {
|
|
159
|
+
FromRendererMessage,
|
|
160
|
+
FromRenderer,
|
|
161
|
+
ToRendererMessage,
|
|
162
|
+
} from "@virid/main";
|
|
153
163
|
|
|
154
164
|
/**
|
|
155
|
-
* @FromRenderer('open-dialog') ensures that when the Rendering Process
|
|
156
|
-
* dispatches an OpenDialogMessage, the Main Process's corresponding
|
|
165
|
+
* @FromRenderer('open-dialog') ensures that when the Rendering Process
|
|
166
|
+
* dispatches an OpenDialogMessage, the Main Process's corresponding
|
|
157
167
|
* System will be triggered automatically.
|
|
158
168
|
*/
|
|
159
|
-
@FromRenderer(
|
|
169
|
+
@FromRenderer("open-dialog")
|
|
160
170
|
export class OpenDialogMessage extends FromRendererMessage {
|
|
161
171
|
constructor(
|
|
162
172
|
public options: {
|
|
163
|
-
title?: string
|
|
164
|
-
filters?: Array<{ name: string; extensions: string[] }
|
|
165
|
-
properties?: Array<
|
|
166
|
-
|
|
173
|
+
title?: string;
|
|
174
|
+
filters?: Array<{ name: string; extensions: string[] }>;
|
|
175
|
+
properties?: Array<
|
|
176
|
+
"openFile" | "openDirectory" | "multiSelections" | "showHiddenFiles"
|
|
177
|
+
>;
|
|
178
|
+
},
|
|
167
179
|
) {
|
|
168
|
-
super()
|
|
180
|
+
super();
|
|
169
181
|
}
|
|
170
182
|
}
|
|
171
183
|
|
|
172
184
|
/**
|
|
173
185
|
* RenderDialogMessage is used to send the result back.
|
|
174
186
|
* - __virid_target = 'renderer' routes it back to the UI.
|
|
175
|
-
* -
|
|
187
|
+
* - __virid_message_type = 'file-dialog' maps it to ChooseBgImageMessage in the renderer.
|
|
176
188
|
*/
|
|
177
189
|
export class RenderDialogMessage extends ToRendererMessage {
|
|
178
|
-
__virid_target: string =
|
|
179
|
-
|
|
190
|
+
__virid_target: string = "renderer";
|
|
191
|
+
__virid_message_type: string = "file-dialog";
|
|
180
192
|
constructor(public path: string) {
|
|
181
|
-
super()
|
|
193
|
+
super();
|
|
182
194
|
}
|
|
183
195
|
}
|
|
184
196
|
|
|
@@ -186,14 +198,17 @@ export class WindowSystem {
|
|
|
186
198
|
@System()
|
|
187
199
|
async openDialog(@Message(OpenDialogMessage) message: OpenDialogMessage) {
|
|
188
200
|
// Invoke the native Electron dialog
|
|
189
|
-
const result = await dialog.showOpenDialog(
|
|
201
|
+
const result = await dialog.showOpenDialog(
|
|
202
|
+
message.senderWindow,
|
|
203
|
+
message.options,
|
|
204
|
+
);
|
|
190
205
|
|
|
191
206
|
// If the user didn't cancel and selected a file, return the result message
|
|
192
207
|
if (!result.canceled && result.filePaths.length > 0) {
|
|
193
|
-
const selectedPath = result.filePaths[0]
|
|
194
|
-
return new RenderDialogMessage(selectedPath)
|
|
208
|
+
const selectedPath = result.filePaths[0];
|
|
209
|
+
return new RenderDialogMessage(selectedPath);
|
|
195
210
|
}
|
|
196
|
-
return
|
|
211
|
+
return;
|
|
197
212
|
}
|
|
198
213
|
}
|
|
199
|
-
```
|
|
214
|
+
```
|
package/README.zh.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @virid/renderer
|
|
2
2
|
|
|
3
3
|
`@virid/renderer` 是electron应用的渲染进程适配器,提供`ToMainMessage`和`FromMainMessage`的自动路由功能与主进程报道功能。
|
|
4
4
|
|
|
@@ -8,18 +8,18 @@
|
|
|
8
8
|
|
|
9
9
|
- **路由自动注册**:渲染进程窗口开启后,自动向主进程注册自身,以后所有的消息都将携带自身的窗口信息。
|
|
10
10
|
- **类型恢复**:消息在通过IPC通道后,恢复成真正的Message类重新进入`@virid/core`并被调度器识别并分发。实现不同进程的位置无关性。
|
|
11
|
-
- **消息定向与广播**:可以通过其他渲染进程的ID
|
|
11
|
+
- **消息定向与广播**:可以通过其他渲染进程的ID来实现定向通信,或者使用\*来广播消息。
|
|
12
12
|
|
|
13
13
|
## 🔌启用插件
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
|
-
import { createVirid } from
|
|
17
|
-
import { RenderPlugin } from
|
|
18
|
-
const app = createVirid()
|
|
16
|
+
import { createVirid } from "@virid/core";
|
|
17
|
+
import { RenderPlugin } from "@virid/renderer";
|
|
18
|
+
const app = createVirid();
|
|
19
19
|
//需要给每个窗口指定一个唯一的windowId来向主进程报道
|
|
20
20
|
app.use(RenderPlugin, {
|
|
21
|
-
windowId:
|
|
22
|
-
})
|
|
21
|
+
windowId: "renderer",
|
|
22
|
+
});
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## 🛠️ @virid/renderer 核心 API 概览
|
|
@@ -27,27 +27,27 @@ app.use(RenderPlugin, {
|
|
|
27
27
|
### `ToMainMessage`
|
|
28
28
|
|
|
29
29
|
- **功能**:一个特殊的消息基类,可被继承。所有继承自`ToMainMessage`的Message将被发往其他进程
|
|
30
|
-
- **逻辑**:该消息类型需要两个特殊标记,`__virid_target`标记了目的地,`
|
|
30
|
+
- **逻辑**:该消息类型需要两个特殊标记,`__virid_target`标记了目的地,`__virid_message_type`标记了在目的地应该被还原为的Message类型
|
|
31
31
|
- **示例**:
|
|
32
32
|
|
|
33
33
|
```ts
|
|
34
34
|
//在渲染进程
|
|
35
|
-
import { ToMainMessage } from
|
|
35
|
+
import { ToMainMessage } from "@virid/renderer";
|
|
36
36
|
// __virid_target=‘main’,说明消息需要发往主进程
|
|
37
37
|
// 当指定__virid_target=‘*’时,消息将会对所有渲染进程广播
|
|
38
|
-
//
|
|
38
|
+
// __virid_message_type: string = 'close-window',描述了在主进程,该消息将会重新变为的类型
|
|
39
39
|
|
|
40
40
|
export class CloseWindowMessage extends ToMainMessage {
|
|
41
|
-
__virid_target =
|
|
42
|
-
|
|
41
|
+
__virid_target = "main";
|
|
42
|
+
__virid_message_type: string = "close-window";
|
|
43
43
|
}
|
|
44
44
|
export class MinimizeWindowMessage extends ToMainMessage {
|
|
45
|
-
__virid_target =
|
|
46
|
-
|
|
45
|
+
__virid_target = "main";
|
|
46
|
+
__virid_message_type: string = "minimize-window";
|
|
47
47
|
}
|
|
48
48
|
export class MaximizeWindowMessage extends ToMainMessage {
|
|
49
|
-
__virid_target =
|
|
50
|
-
|
|
49
|
+
__virid_target = "main";
|
|
50
|
+
__virid_message_type: string = "maximize-window";
|
|
51
51
|
}
|
|
52
52
|
```
|
|
53
53
|
|
|
@@ -55,13 +55,13 @@ export class MaximizeWindowMessage extends ToMainMessage {
|
|
|
55
55
|
|
|
56
56
|
```ts
|
|
57
57
|
//在主进程
|
|
58
|
-
import { FromRenderMessage, FromRenderer, ToRenderMessage } from
|
|
58
|
+
import { FromRenderMessage, FromRenderer, ToRenderMessage } from "@virid/main";
|
|
59
59
|
//这里的每个id都和渲染进程的消息对应
|
|
60
|
-
@FromRenderer(
|
|
60
|
+
@FromRenderer("close-window")
|
|
61
61
|
export class CloseWindowMessage extends FromRendererMessage {}
|
|
62
|
-
@FromRenderer(
|
|
62
|
+
@FromRenderer("minimize-window")
|
|
63
63
|
export class MinimizeWindowMessage extends FromRendererMessage {}
|
|
64
|
-
@FromRenderer(
|
|
64
|
+
@FromRenderer("maximize-window")
|
|
65
65
|
export class MaximizeWindowMessage extends FromRendererMessage {}
|
|
66
66
|
|
|
67
67
|
// 通过这三个System,可以实现所有窗口的最小化、最大化、关闭功能
|
|
@@ -69,18 +69,22 @@ export class MaximizeWindowMessage extends FromRendererMessage {}
|
|
|
69
69
|
export class WindowSystem {
|
|
70
70
|
@System()
|
|
71
71
|
closeWindow(@Message(CloseWindowMessage) message: CloseWindowMessage) {
|
|
72
|
-
message.senderWindow.close()
|
|
72
|
+
message.senderWindow.close();
|
|
73
73
|
}
|
|
74
74
|
@System()
|
|
75
|
-
minimizeWindow(
|
|
76
|
-
|
|
75
|
+
minimizeWindow(
|
|
76
|
+
@Message(MinimizeWindowMessage) message: MinimizeWindowMessage,
|
|
77
|
+
) {
|
|
78
|
+
message.senderWindow.minimize();
|
|
77
79
|
}
|
|
78
80
|
@System()
|
|
79
|
-
maximizeWindow(
|
|
81
|
+
maximizeWindow(
|
|
82
|
+
@Message(MaximizeWindowMessage) message: MaximizeWindowMessage,
|
|
83
|
+
) {
|
|
80
84
|
if (message.senderWindow.isMaximized()) {
|
|
81
|
-
message.senderWindow.unmaximize()
|
|
85
|
+
message.senderWindow.unmaximize();
|
|
82
86
|
} else {
|
|
83
|
-
message.senderWindow.maximize()
|
|
87
|
+
message.senderWindow.maximize();
|
|
84
88
|
}
|
|
85
89
|
}
|
|
86
90
|
}
|
|
@@ -94,26 +98,28 @@ export class WindowSystem {
|
|
|
94
98
|
|
|
95
99
|
```ts
|
|
96
100
|
//在渲染进程
|
|
97
|
-
import { FromIpc, FromMainMessage, ToMainMessage } from
|
|
101
|
+
import { FromIpc, FromMainMessage, ToMainMessage } from "@virid/renderer";
|
|
98
102
|
|
|
99
|
-
@FromIpc(
|
|
103
|
+
@FromIpc("file-dialog")
|
|
100
104
|
class ChooseBgImageMessage extends FromMainMessage {
|
|
101
105
|
constructor(public path: string) {
|
|
102
|
-
super()
|
|
106
|
+
super();
|
|
103
107
|
}
|
|
104
108
|
}
|
|
105
109
|
|
|
106
110
|
class OpenDialogMessage extends ToMainMessage {
|
|
107
|
-
__virid_target: string =
|
|
108
|
-
|
|
111
|
+
__virid_target: string = "main";
|
|
112
|
+
__virid_message_type: string = "open-dialog";
|
|
109
113
|
constructor(
|
|
110
114
|
public options: {
|
|
111
|
-
title?: string
|
|
112
|
-
filters?: Array<{ name: string; extensions: string[] }
|
|
113
|
-
properties?: Array<
|
|
114
|
-
|
|
115
|
+
title?: string;
|
|
116
|
+
filters?: Array<{ name: string; extensions: string[] }>;
|
|
117
|
+
properties?: Array<
|
|
118
|
+
"openFile" | "openDirectory" | "multiSelections" | "showHiddenFiles"
|
|
119
|
+
>;
|
|
120
|
+
},
|
|
115
121
|
) {
|
|
116
|
-
super()
|
|
122
|
+
super();
|
|
117
123
|
}
|
|
118
124
|
}
|
|
119
125
|
```
|
|
@@ -122,31 +128,37 @@ class OpenDialogMessage extends ToMainMessage {
|
|
|
122
128
|
|
|
123
129
|
```ts
|
|
124
130
|
//在主进程
|
|
125
|
-
import {
|
|
131
|
+
import {
|
|
132
|
+
FromRendererMessage,
|
|
133
|
+
FromRenderer,
|
|
134
|
+
ToRenderMessage,
|
|
135
|
+
} from "@virid/main";
|
|
126
136
|
|
|
127
137
|
// @FromRender('open-dialog')表明,当上面的渲染进程调用OpenDialogMessage.send(options)时
|
|
128
138
|
// 主进程的OpenDialogMessage将被自动投递,因此下面的openDialog System将被virid自动调用
|
|
129
139
|
// 当openDialog执行完毕,将返回一个RenderDialogMessage,该RenderDialogMessage标记了目的地与类型
|
|
130
140
|
// 其会转换为渲染进程的OpenDialogMessage并触发渲染进程的System或者Listener执行
|
|
131
141
|
|
|
132
|
-
@FromRenderer(
|
|
142
|
+
@FromRenderer("open-dialog")
|
|
133
143
|
export class OpenDialogMessage extends FromRendererMessage {
|
|
134
144
|
constructor(
|
|
135
145
|
public options: {
|
|
136
|
-
title?: string
|
|
137
|
-
filters?: Array<{ name: string; extensions: string[] }
|
|
138
|
-
properties?: Array<
|
|
139
|
-
|
|
146
|
+
title?: string;
|
|
147
|
+
filters?: Array<{ name: string; extensions: string[] }>;
|
|
148
|
+
properties?: Array<
|
|
149
|
+
"openFile" | "openDirectory" | "multiSelections" | "showHiddenFiles"
|
|
150
|
+
>;
|
|
151
|
+
},
|
|
140
152
|
) {
|
|
141
|
-
super()
|
|
153
|
+
super();
|
|
142
154
|
}
|
|
143
155
|
}
|
|
144
156
|
|
|
145
157
|
export class RenderDialogMessage extends ToRendererMessage {
|
|
146
|
-
__virid_target: string =
|
|
147
|
-
|
|
158
|
+
__virid_target: string = "renderer";
|
|
159
|
+
__virid_message_type: string = "file-dialog";
|
|
148
160
|
constructor(public path: string) {
|
|
149
|
-
super()
|
|
161
|
+
super();
|
|
150
162
|
}
|
|
151
163
|
}
|
|
152
164
|
|
|
@@ -154,16 +166,16 @@ export class WindowSystem {
|
|
|
154
166
|
@System()
|
|
155
167
|
async openDialog(@Message(OpenDialogMessage) message: OpenDialogMessage) {
|
|
156
168
|
// 调用原生对话框
|
|
157
|
-
const result = await dialog.showOpenDialog(
|
|
169
|
+
const result = await dialog.showOpenDialog(
|
|
170
|
+
message.senderWindow,
|
|
171
|
+
message.options,
|
|
172
|
+
);
|
|
158
173
|
// 如果用户没有取消,并且确实选择了文件
|
|
159
174
|
if (!result.canceled && result.filePaths.length > 0) {
|
|
160
|
-
const selectedPath = result.filePaths[0]
|
|
161
|
-
return new RenderDialogMessage(selectedPath)
|
|
175
|
+
const selectedPath = result.filePaths[0];
|
|
176
|
+
return new RenderDialogMessage(selectedPath);
|
|
162
177
|
}
|
|
163
|
-
return
|
|
178
|
+
return;
|
|
164
179
|
}
|
|
165
180
|
}
|
|
166
181
|
```
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
package/dist/index.cjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @virid/renderer v0.3.
|
|
2
|
+
* @virid/renderer v0.3.2
|
|
3
3
|
* Electron renderer process adapter for virid, responsible for forwarding and receiving messages from the main process
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var m=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var x=(i,e,r)=>e in i?m(i,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):i[e]=r;var t=(i,e)=>m(i,"name",{value:e,configurable:!0});var y=(i,e)=>{for(var r in e)m(i,r,{get:e[r],enumerable:!0})},T=(i,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let _ of A(e))!V.call(i,_)&&_!==r&&m(i,_,{get:()=>e[_],enumerable:!(s=M(e,_))||s.enumerable});return i};var h=i=>T(m({},"__esModule",{value:!0}),i);var d=(i,e,r)=>x(i,typeof e!="symbol"?e+"":e,r);var $={};y($,{FromMain:()=>N,FromMainMessage:()=>R,RenderPlugin:()=>v,ToMainMessage:()=>o,middleWare:()=>f});module.exports=h($);var a=require("@virid/core");var I=require("@virid/core");var w=class w extends I.EventMessage{constructor(){super(...arguments);d(this,"__virid_source","unknown");d(this,"__virid_target","");d(this,"__virid_message_type","")}};t(w,"FromMainMessage");var R=w,p=class p extends I.EventMessage{};t(p,"ToMainMessage"),d(p,"__virid_source");var o=p;var E=require("@virid/core");var f=t((i,e)=>{if(i instanceof o){let{__virid_target:r,__virid_message_type:s,..._}=i;if(r==o.__virid_source){E.MessageWriter.warn(`[Virid Render] Prohibit Sending To Oneself: ${r} is not allowed in ToRenderMessage.`);return}window.__VIRID_BRIDGE__.post({__virid_source:o.__virid_source,__virid_target:r,__virid_message_type:s,payload:_})}else e()},"middleWare");var D=require("@virid/core"),c={...D.VIRID_METADATA,FROMMAIN:"virid:renderer:frommain"};function N(i){return function(e){Reflect.defineMetadata(c.FROMMAIN,i,e)}}t(N,"FromMain");var g=class g{constructor(){d(this,"name","@virid/render");d(this,"message_map",new Map)}install(e,r){window.__VIRID_BRIDGE__||a.MessageWriter.error(new Error("[Virid Render] Preloading Failed: Please initialize in the preloaded script first.")),r?.windowId||a.MessageWriter.error(new Error(`[Virid Render] Activate Failed: Please provide the windowId:${r?.windowId}.`)),o.__virid_source=r.windowId,window.__VIRID_BRIDGE__.post({__virid_source:r.windowId,__virid_target:"main",__virid_message_type:"VIRID_INTERNAL_REGISTER",payload:{windowId:r.windowId}}),window.__VIRID_BRIDGE__.subscribe(this.convertFromMainMessage),e.useMiddleware(f)}bindRoute(e){let r=Reflect.getMetadata(c.FROMMAIN,e);this.message_map.has(r)&&a.MessageWriter.error(new Error(`[Virid Renderer] Duplicate IpcMessage: Registration for route: ${r}, message class: ${e}`)),this.message_map.set(r,e)}convertFromMainMessage(e){let{__virid_source:r,__virid_target:s,__virid_message_type:_,payload:l}=e;if(!_||!r||!s){a.MessageWriter.error(new Error(`[Virid Render] Incomplete Data:
|
|
6
6
|
__virid_source: ${r}
|
|
7
|
-
__virid_target:${
|
|
8
|
-
|
|
9
|
-
Please provide the windowId:${r?.windowId}.`)),d.__virid_source=r.windowId,window.__VIRID_BRIDGE__.post({__virid_source:r.windowId,__virid_target:"main",__virid_messageType:"VIRID_INTERNAL_REGISTER",payload:{windowId:r.windowId}}),window.__VIRID_BRIDGE__.subscribe(I),e.useMiddleware(g)}_(E,"activateApp");var R=class R{constructor(){n(this,"name","@virid/render")}install(r,i){E(r,i)}};_(R,"RenderPluginClass");var u=R;0&&(module.exports={FromMain,FromMainMessage,RenderPluginClass,ToMainMessage,convertFromMainMessage,middleWare});
|
|
7
|
+
__virid_target:${s}
|
|
8
|
+
__virid_message_type: ${_}.`));return}if(!this.message_map.has(_)){a.MessageWriter.error(new Error(`[Virid Render] Unregistered type: ${_} `));return}let u=this.message_map.get(_),n=new u;n.__virid_source=r,n.__virid_target=s,n.__virid_message_type=_,l&&Object.assign(n,l),a.MessageWriter.write(n)}};t(g,"RenderPlugin");var v=g;0&&(module.exports={FromMain,FromMainMessage,RenderPlugin,ToMainMessage,middleWare});
|
|
10
9
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/renderer/message.ts","../src/renderer/middleware.ts","../src/renderer/router.ts","../src/app.ts"],"sourcesContent":["/*\n * Copyright (c) 2026-present Ailrid\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * Description: Electron renderer process adapter for virid, responsible for forwarding and receiving messages from the main process.\n */\nimport { ViridPlugin, type ViridApp } from \"@virid/core\";\nexport { ToMainMessage, FromMainMessage } from \"./renderer\";\nexport * from \"./interfaces\";\nexport * from \"./renderer\";\nimport { activateApp } from \"./app\";\nimport { type PluginOption } from \"./interfaces\";\n\nexport class RenderPluginClass implements ViridPlugin<PluginOption> {\n name = \"@virid/render\";\n install(app: ViridApp, options: PluginOption) {\n activateApp(app, options);\n }\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { EventMessage } from \"@virid/core\";\n/**\n * RenderMessage: Message sent by the main process\n */\nexport abstract class FromMainMessage extends EventMessage {\n /** Where am I from?\n */\n public __virid_source: string = \"unknown\";\n /**Where is my destination?\n *'main ': Sent to the main process for processing\n *'all': Broadcast to all windows (relayed through the main process)\n *String: Specify the ID of a window (windowId)\n */\n public __virid_target: string = \"\";\n /**\n *What message should I turn into at the destination?\n */\n public __virid_messageType: string = \"\";\n}\n\n/**\n *RenderMessage: Initiated by the rendering process, targeting the main process or other windows\n */\nexport abstract class ToMainMessage extends EventMessage {\n /**Where am I from?\n */\n public static __virid_source: string;\n\n /**Where is my destination?\n *'main ': Sent to the main process for processing\n *'all': Broadcast to all windows (relayed through the main process)\n *String: Specify the ID of a window (windowId)\n */\n public abstract __virid_target: string;\n\n /**\n *What message should I turn into at the destination?\n */\n public abstract __virid_messageType: string;\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { type Middleware, MessageWriter } from \"@virid/core\";\nimport { ToMainMessage } from \"./message\";\nexport const middleWare: Middleware = (message, next) => {\n // If the message inherits from ToMainMessage, intercept concurrency to the main process\n if (message instanceof ToMainMessage) {\n const { __virid_target, __virid_messageType, ...payload } = message;\n if (__virid_target == ToMainMessage.__virid_source) {\n MessageWriter.warn(\n `[Virid Render] Prohibit Sending To Oneself: ${__virid_target} is not allowed in ToRenderMessage.`,\n );\n return;\n }\n window.__VIRID_BRIDGE__.post({\n __virid_source: ToMainMessage.__virid_source,\n __virid_target: __virid_target,\n __virid_messageType: __virid_messageType,\n payload: payload, // Expand all attributes on the instance\n });\n } else {\n next();\n }\n};\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { MessageWriter, Newable } from \"@virid/core\";\nimport { type FromMainMessage } from \"./message\";\nconst MESSAGE_MAP = new Map<string, Newable<FromMainMessage>>();\n\nexport function FromMain(type: string) {\n return function (target: Newable<FromMainMessage>) {\n if (MESSAGE_MAP.has(type)) {\n MessageWriter.error(\n new Error(\n `[Virid Main] Duplicate IpcMessage: Registration for type: ${type}`,\n ),\n );\n }\n MESSAGE_MAP.set(type, target);\n };\n}\nexport function convertFromMainMessage(ipcMessage: any): void {\n const { __virid_source, __virid_target, __virid_messageType, payload } =\n ipcMessage;\n if (!__virid_messageType || !__virid_source || !__virid_target) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Incomplete Data:\\n__virid_source: ${__virid_source}\\n__virid_target:${__virid_target}\\n__virid_messageType: ${__virid_messageType}.`,\n ),\n );\n return;\n }\n if (!MESSAGE_MAP.has(__virid_messageType)) {\n MessageWriter.error(\n new Error(`[Virid Render] Unregistered type: ${__virid_messageType} `),\n );\n return;\n }\n // Find the corresponding constructor\n const MessageClass = MESSAGE_MAP.get(__virid_messageType)!;\n // Instantiate and inject parameters\n const instance = new MessageClass();\n\n // Explicitly assigning base class identifiers to ensure complete identity information of instances\n instance.__virid_source = __virid_source;\n instance.__virid_target = __virid_target;\n instance.__virid_messageType = __virid_messageType;\n\n // Restore data\n if (payload) {\n Object.assign(instance, payload);\n }\n\n // Redistribution\n MessageWriter.write(instance);\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\n\nimport { MessageWriter, type ViridApp } from \"@virid/core\";\nimport { middleWare, convertFromMainMessage, ToMainMessage } from \"./renderer\";\nimport { type PluginOption } from \"./interfaces\";\nexport function activateApp(app: ViridApp, options: PluginOption) {\n // first, check if the preload script has been loaded\n if (!window.__VIRID_BRIDGE__) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Preloading Failed: Please initialize in the preloaded script first.`,\n ),\n );\n }\n // check whether parameters are passed\n if (!options?.windowId) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Activate Failed:\\nPlease provide the windowId:${options?.windowId}.`,\n ),\n );\n }\n // register your own id, so that all messages sent to the main process will carry your own id in the future\n ToMainMessage.__virid_source = options.windowId;\n // actively send a registration message to the main process to register itself\n window.__VIRID_BRIDGE__.post({\n __virid_source: options.windowId,\n __virid_target: \"main\",\n __virid_messageType: \"VIRID_INTERNAL_REGISTER\",\n payload: {\n windowId: options.windowId,\n },\n });\n // Subscribe to the ipc channel, and convert all returned messages into our own message types according to the registry\n window.__VIRID_BRIDGE__.subscribe(convertFromMainMessage);\n // register your own middleware function to intercept messages of type ToMainMessage and forward them to the main process of electron\n app.useMiddleware(middleWare);\n}\n"],"mappings":";;;;ulBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,EAAA,oBAAAC,EAAA,sBAAAC,EAAA,kBAAAC,EAAA,2BAAAC,EAAA,eAAAC,IAAA,eAAAC,EAAAR,GCKA,IAAAS,EAA6B,uBAItB,IAAeC,EAAf,MAAeA,UAAwBC,cAAAA,CAAvC,kCAGEC,EAAAA,sBAAyB,WAMzBC,EAAAA,sBAAyB,IAIzBC,EAAAA,2BAA8B,IACvC,EAd8CH,EAAAA,EAAAA,mBAAvC,IAAeD,EAAfK,EAmBeC,EAAf,MAAeA,UAAsBL,cAAAA,CAgB5C,EAhB4CA,EAAAA,EAAAA,iBAG1CM,EAHoBD,EAGNJ,kBAHT,IAAeI,EAAfE,ECvBP,IAAAC,EAA+C,uBAExC,IAAMC,EAAyBC,EAAA,CAACC,EAASC,IAAAA,CAE9C,GAAID,aAAmBE,EAAe,CACpC,GAAM,CAAEC,eAAAA,EAAgBC,oBAAAA,EAAqB,GAAGC,CAAAA,EAAYL,EAC5D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C,MACZ,IAAIC,MACF,oFAAoF,CAAA,EAKrFL,GAASM,UACZH,gBAAcC,MACZ,IAAIC,MACF;8BAAgEL,GAASM,QAAAA,GAAW,CAAA,EAK1FC,EAAcC,eAAiBR,EAAQM,SAEvCL,OAAOC,iBAAiBO,KAAK,CAC3BD,eAAgBR,EAAQM,SACxBI,eAAgB,OAChBC,oBAAqB,0BACrBC,QAAS,CACPN,SAAUN,EAAQM,QACpB,CACF,CAAA,EAEAL,OAAOC,iBAAiBW,UAAUC,CAAAA,EAElCf,EAAIgB,cAAcC,CAAAA,CACpB,CAhCgBlB,EAAAA,EAAAA,eJeT,IAAMmB,EAAN,MAAMA,CAAAA,CAAN,cACLC,EAAAA,YAAO,iBACPC,QAAQC,EAAeC,EAAuB,CAC5CC,EAAYF,EAAKC,CAAAA,CACnB,CACF,EALaJ,EAAAA,EAAAA,qBAAN,IAAMA,EAANM","names":["index_exports","__export","FromMain","FromMainMessage","RenderPluginClass","ToMainMessage","convertFromMainMessage","middleWare","__toCommonJS","import_core","FromMainMessage","EventMessage","__virid_source","__virid_target","__virid_messageType","_FromMainMessage","ToMainMessage","__publicField","_ToMainMessage","import_core","middleWare","__name","message","next","ToMainMessage","__virid_target","__virid_messageType","payload","__virid_source","MessageWriter","warn","window","__VIRID_BRIDGE__","post","import_core","MESSAGE_MAP","Map","FromMain","type","target","has","MessageWriter","error","Error","set","convertFromMainMessage","ipcMessage","__virid_source","__virid_target","__virid_messageType","payload","MessageClass","get","instance","Object","assign","write","import_core","activateApp","app","options","window","__VIRID_BRIDGE__","MessageWriter","error","Error","windowId","ToMainMessage","__virid_source","post","__virid_target","__virid_messageType","payload","subscribe","convertFromMainMessage","useMiddleware","middleWare","RenderPluginClass","name","install","app","options","activateApp","_RenderPluginClass"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/renderer/message.ts","../src/renderer/middleware.ts","../src/renderer/constant.ts","../src/renderer/decorator.ts"],"sourcesContent":["/*\n * Copyright (c) 2026-present Ailrid\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * Description: Electron renderer process adapter for virid, responsible for forwarding and receiving messages from the main process.\n */\nimport {\n MessageWriter,\n Newable,\n ViridPlugin,\n type ViridApp,\n} from \"@virid/core\";\nimport { type PluginOption } from \"./interfaces\";\nimport { FromMainMessage } from \"./renderer\";\nimport { middleWare, ToMainMessage } from \"./renderer\";\nimport { VIRID_RENDERER_METADATA } from \"./renderer/constant\";\n\nexport * from \"./interfaces\";\nexport * from \"./renderer\";\n\nexport class RenderPlugin implements ViridPlugin<PluginOption> {\n name = \"@virid/render\";\n public message_map = new Map<string, Newable<FromMainMessage>>();\n\n install(app: ViridApp, options: PluginOption) {\n // first, check if the preload script has been loaded\n if (!window.__VIRID_BRIDGE__) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Preloading Failed: Please initialize in the preloaded script first.`,\n ),\n );\n }\n // check whether parameters are passed\n if (!options?.windowId) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Activate Failed: Please provide the windowId:${options?.windowId}.`,\n ),\n );\n }\n // register your own id, so that all messages sent to the main process will carry your own id in the future\n ToMainMessage.__virid_source = options.windowId;\n // actively send a registration message to the main process to register itself\n window.__VIRID_BRIDGE__.post({\n __virid_source: options.windowId,\n __virid_target: \"main\",\n __virid_message_type: \"VIRID_INTERNAL_REGISTER\",\n payload: {\n windowId: options.windowId,\n },\n });\n // Subscribe to the ipc channel, and convert all returned messages into our own message types according to the registry\n window.__VIRID_BRIDGE__.subscribe(this.convertFromMainMessage);\n // register your own middleware function to intercept messages of type ToMainMessage and forward them to the main process of electron\n app.useMiddleware(middleWare);\n }\n bindRoute(target: Newable<FromMainMessage>) {\n const route = Reflect.getMetadata(VIRID_RENDERER_METADATA.FROMMAIN, target);\n if (this.message_map.has(route)) {\n MessageWriter.error(\n new Error(\n `[Virid Renderer] Duplicate IpcMessage: Registration for route: ${route}, message class: ${target}`,\n ),\n );\n }\n this.message_map.set(route, target);\n }\n\n convertFromMainMessage(ipcMessage: any): void {\n const { __virid_source, __virid_target, __virid_message_type, payload } =\n ipcMessage;\n if (!__virid_message_type || !__virid_source || !__virid_target) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Incomplete Data:\\n__virid_source: ${__virid_source}\\n__virid_target:${__virid_target}\\n__virid_message_type: ${__virid_message_type}.`,\n ),\n );\n return;\n }\n if (!this.message_map.has(__virid_message_type)) {\n MessageWriter.error(\n new Error(`[Virid Render] Unregistered type: ${__virid_message_type} `),\n );\n return;\n }\n // Find the corresponding constructor\n const MessageClass = this.message_map.get(__virid_message_type)!;\n // Instantiate and inject parameters\n const instance = new MessageClass();\n\n // Explicitly assigning base class identifiers to ensure complete identity information of instances\n instance.__virid_source = __virid_source;\n instance.__virid_target = __virid_target;\n instance.__virid_message_type = __virid_message_type;\n\n // Restore data\n if (payload) {\n Object.assign(instance, payload);\n }\n\n // Redistribution\n MessageWriter.write(instance);\n }\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { EventMessage } from \"@virid/core\";\n/**\n * RenderMessage: Message sent by the main process\n */\nexport abstract class FromMainMessage extends EventMessage {\n /** Where am I from?\n */\n public __virid_source: string = \"unknown\";\n /**Where is my destination?\n *'main ': Sent to the main process for processing\n *'all': Broadcast to all windows (relayed through the main process)\n *String: Specify the ID of a window (windowId)\n */\n public __virid_target: string = \"\";\n /**\n *What message should I turn into at the destination?\n */\n public __virid_message_type: string = \"\";\n}\n\n/**\n *RenderMessage: Initiated by the rendering process, targeting the main process or other windows\n */\nexport abstract class ToMainMessage extends EventMessage {\n /**Where am I from?\n */\n public static __virid_source: string;\n\n /**Where is my destination?\n *'main ': Sent to the main process for processing\n *'all': Broadcast to all windows (relayed through the main process)\n *String: Specify the ID of a window (windowId)\n */\n public abstract __virid_target: string;\n\n /**\n *What message should I turn into at the destination?\n */\n public abstract __virid_message_type: string;\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { type Middleware, MessageWriter } from \"@virid/core\";\nimport { ToMainMessage } from \"./message\";\nexport const middleWare: Middleware = (message, next) => {\n // If the message inherits from ToMainMessage, intercept concurrency to the main process\n if (message instanceof ToMainMessage) {\n const { __virid_target, __virid_message_type, ...payload } = message;\n if (__virid_target == ToMainMessage.__virid_source) {\n MessageWriter.warn(\n `[Virid Render] Prohibit Sending To Oneself: ${__virid_target} is not allowed in ToRenderMessage.`,\n );\n return;\n }\n window.__VIRID_BRIDGE__.post({\n __virid_source: ToMainMessage.__virid_source,\n __virid_target: __virid_target,\n __virid_message_type: __virid_message_type,\n payload: payload, // Expand all attributes on the instance\n });\n } else {\n next();\n }\n};\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { VIRID_METADATA } from \"@virid/core\";\n\nexport const VIRID_RENDERER_METADATA = {\n ...VIRID_METADATA,\n FROMMAIN: \"virid:renderer:frommain\",\n} as const;\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { Newable } from \"@virid/core\";\nimport { type FromMainMessage } from \"./message\";\nimport { VIRID_RENDERER_METADATA } from \"./constant\";\nexport function FromMain(type: string) {\n return function (target: Newable<FromMainMessage>) {\n Reflect.defineMetadata(VIRID_RENDERER_METADATA.FROMMAIN, type, target);\n };\n}\n"],"mappings":";;;;ulBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,EAAA,oBAAAC,EAAA,iBAAAC,EAAA,kBAAA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","FromMain","FromMainMessage","RenderPlugin","ToMainMessage","middleWare","__toCommonJS","import_core","import_core","FromMainMessage","EventMessage","__virid_source","__virid_target","__virid_message_type","_FromMainMessage","ToMainMessage","__publicField","_ToMainMessage","import_core","middleWare","__name","message","next","ToMainMessage","__virid_target","__virid_message_type","payload","__virid_source","MessageWriter","warn","window","__VIRID_BRIDGE__","post","import_core","VIRID_RENDERER_METADATA","VIRID_METADATA","FROMMAIN","FromMain","type","target","Reflect","defineMetadata","VIRID_RENDERER_METADATA","FROMMAIN","RenderPlugin","name","message_map","Map","install","app","options","window","__VIRID_BRIDGE__","MessageWriter","error","Error","windowId","ToMainMessage","__virid_source","post","__virid_target","__virid_message_type","payload","subscribe","convertFromMainMessage","useMiddleware","middleWare","bindRoute","target","route","Reflect","getMetadata","VIRID_RENDERER_METADATA","FROMMAIN","has","set","ipcMessage","MessageClass","get","instance","Object","assign","write","_RenderPlugin"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { EventMessage, Middleware, Newable, ViridPlugin, ViridApp } from '@virid/core';
|
|
2
2
|
|
|
3
|
+
interface PluginOption {
|
|
4
|
+
windowId: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
3
7
|
/**
|
|
4
8
|
* RenderMessage: Message sent by the main process
|
|
5
9
|
*/
|
|
@@ -16,7 +20,7 @@ declare abstract class FromMainMessage extends EventMessage {
|
|
|
16
20
|
/**
|
|
17
21
|
*What message should I turn into at the destination?
|
|
18
22
|
*/
|
|
19
|
-
|
|
23
|
+
__virid_message_type: string;
|
|
20
24
|
}
|
|
21
25
|
/**
|
|
22
26
|
*RenderMessage: Initiated by the rendering process, targeting the main process or other windows
|
|
@@ -34,24 +38,19 @@ declare abstract class ToMainMessage extends EventMessage {
|
|
|
34
38
|
/**
|
|
35
39
|
*What message should I turn into at the destination?
|
|
36
40
|
*/
|
|
37
|
-
abstract
|
|
41
|
+
abstract __virid_message_type: string;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
declare const middleWare: Middleware;
|
|
41
45
|
|
|
42
46
|
declare function FromMain(type: string): (target: Newable<FromMainMessage>) => void;
|
|
43
|
-
declare function convertFromMainMessage(ipcMessage: any): void;
|
|
44
|
-
|
|
45
|
-
interface PluginOption {
|
|
46
|
-
/**
|
|
47
|
-
* 窗口的 ID
|
|
48
|
-
*/
|
|
49
|
-
windowId: string;
|
|
50
|
-
}
|
|
51
47
|
|
|
52
|
-
declare class
|
|
48
|
+
declare class RenderPlugin implements ViridPlugin<PluginOption> {
|
|
53
49
|
name: string;
|
|
50
|
+
message_map: Map<string, Newable<FromMainMessage>>;
|
|
54
51
|
install(app: ViridApp, options: PluginOption): void;
|
|
52
|
+
bindRoute(target: Newable<FromMainMessage>): void;
|
|
53
|
+
convertFromMainMessage(ipcMessage: any): void;
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
export { FromMain, FromMainMessage, type PluginOption,
|
|
56
|
+
export { FromMain, FromMainMessage, type PluginOption, RenderPlugin, ToMainMessage, middleWare };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { EventMessage, Middleware, Newable, ViridPlugin, ViridApp } from '@virid/core';
|
|
2
2
|
|
|
3
|
+
interface PluginOption {
|
|
4
|
+
windowId: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
3
7
|
/**
|
|
4
8
|
* RenderMessage: Message sent by the main process
|
|
5
9
|
*/
|
|
@@ -16,7 +20,7 @@ declare abstract class FromMainMessage extends EventMessage {
|
|
|
16
20
|
/**
|
|
17
21
|
*What message should I turn into at the destination?
|
|
18
22
|
*/
|
|
19
|
-
|
|
23
|
+
__virid_message_type: string;
|
|
20
24
|
}
|
|
21
25
|
/**
|
|
22
26
|
*RenderMessage: Initiated by the rendering process, targeting the main process or other windows
|
|
@@ -34,24 +38,19 @@ declare abstract class ToMainMessage extends EventMessage {
|
|
|
34
38
|
/**
|
|
35
39
|
*What message should I turn into at the destination?
|
|
36
40
|
*/
|
|
37
|
-
abstract
|
|
41
|
+
abstract __virid_message_type: string;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
declare const middleWare: Middleware;
|
|
41
45
|
|
|
42
46
|
declare function FromMain(type: string): (target: Newable<FromMainMessage>) => void;
|
|
43
|
-
declare function convertFromMainMessage(ipcMessage: any): void;
|
|
44
|
-
|
|
45
|
-
interface PluginOption {
|
|
46
|
-
/**
|
|
47
|
-
* 窗口的 ID
|
|
48
|
-
*/
|
|
49
|
-
windowId: string;
|
|
50
|
-
}
|
|
51
47
|
|
|
52
|
-
declare class
|
|
48
|
+
declare class RenderPlugin implements ViridPlugin<PluginOption> {
|
|
53
49
|
name: string;
|
|
50
|
+
message_map: Map<string, Newable<FromMainMessage>>;
|
|
54
51
|
install(app: ViridApp, options: PluginOption): void;
|
|
52
|
+
bindRoute(target: Newable<FromMainMessage>): void;
|
|
53
|
+
convertFromMainMessage(ipcMessage: any): void;
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
export { FromMain, FromMainMessage, type PluginOption,
|
|
56
|
+
export { FromMain, FromMainMessage, type PluginOption, RenderPlugin, ToMainMessage, middleWare };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @virid/renderer v0.3.
|
|
2
|
+
* @virid/renderer v0.3.2
|
|
3
3
|
* Electron renderer process adapter for virid, responsible for forwarding and receiving messages from the main process
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var w=Object.defineProperty;var D=(i,e,r)=>e in i?w(i,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):i[e]=r;var _=(i,e)=>w(i,"name",{value:e,configurable:!0});var o=(i,e,r)=>D(i,typeof e!="symbol"?e+"":e,r);import{MessageWriter as d}from"@virid/core";import{EventMessage as v}from"@virid/core";var c=class c extends v{constructor(){super(...arguments);o(this,"__virid_source","unknown");o(this,"__virid_target","");o(this,"__virid_message_type","")}};_(c,"FromMainMessage");var f=c,m=class m extends v{};_(m,"ToMainMessage"),o(m,"__virid_source");var s=m;import{MessageWriter as u}from"@virid/core";var g=_((i,e)=>{if(i instanceof s){let{__virid_target:r,__virid_message_type:a,...t}=i;if(r==s.__virid_source){u.warn(`[Virid Render] Prohibit Sending To Oneself: ${r} is not allowed in ToRenderMessage.`);return}window.__VIRID_BRIDGE__.post({__virid_source:s.__virid_source,__virid_target:r,__virid_message_type:a,payload:t})}else e()},"middleWare");import{VIRID_METADATA as M}from"@virid/core";var p={...M,FROMMAIN:"virid:renderer:frommain"};function O(i){return function(e){Reflect.defineMetadata(p.FROMMAIN,i,e)}}_(O,"FromMain");var R=class R{constructor(){o(this,"name","@virid/render");o(this,"message_map",new Map)}install(e,r){window.__VIRID_BRIDGE__||d.error(new Error("[Virid Render] Preloading Failed: Please initialize in the preloaded script first.")),r?.windowId||d.error(new Error(`[Virid Render] Activate Failed: Please provide the windowId:${r?.windowId}.`)),s.__virid_source=r.windowId,window.__VIRID_BRIDGE__.post({__virid_source:r.windowId,__virid_target:"main",__virid_message_type:"VIRID_INTERNAL_REGISTER",payload:{windowId:r.windowId}}),window.__VIRID_BRIDGE__.subscribe(this.convertFromMainMessage),e.useMiddleware(g)}bindRoute(e){let r=Reflect.getMetadata(p.FROMMAIN,e);this.message_map.has(r)&&d.error(new Error(`[Virid Renderer] Duplicate IpcMessage: Registration for route: ${r}, message class: ${e}`)),this.message_map.set(r,e)}convertFromMainMessage(e){let{__virid_source:r,__virid_target:a,__virid_message_type:t,payload:I}=e;if(!t||!r||!a){d.error(new Error(`[Virid Render] Incomplete Data:
|
|
6
6
|
__virid_source: ${r}
|
|
7
|
-
__virid_target:${
|
|
8
|
-
|
|
9
|
-
Please provide the windowId:${r?.windowId}.`)),o.__virid_source=r.windowId,window.__VIRID_BRIDGE__.post({__virid_source:r.windowId,__virid_target:"main",__virid_messageType:"VIRID_INTERNAL_REGISTER",payload:{windowId:r.windowId}}),window.__VIRID_BRIDGE__.subscribe(l),e.useMiddleware(I)}_(R,"activateApp");var v=class v{constructor(){d(this,"name","@virid/render")}install(r,i){R(r,i)}};_(v,"RenderPluginClass");var x=v;export{B as FromMain,w as FromMainMessage,x as RenderPluginClass,o as ToMainMessage,l as convertFromMainMessage,I as middleWare};
|
|
7
|
+
__virid_target:${a}
|
|
8
|
+
__virid_message_type: ${t}.`));return}if(!this.message_map.has(t)){d.error(new Error(`[Virid Render] Unregistered type: ${t} `));return}let E=this.message_map.get(t),n=new E;n.__virid_source=r,n.__virid_target=a,n.__virid_message_type=t,I&&Object.assign(n,I),d.write(n)}};_(R,"RenderPlugin");var l=R;export{O as FromMain,f as FromMainMessage,l as RenderPlugin,s as ToMainMessage,g as middleWare};
|
|
10
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/renderer/message.ts","../src/renderer/middleware.ts","../src/renderer/router.ts","../src/app.ts","../src/index.ts"],"sourcesContent":["/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { EventMessage } from \"@virid/core\";\n/**\n * RenderMessage: Message sent by the main process\n */\nexport abstract class FromMainMessage extends EventMessage {\n /** Where am I from?\n */\n public __virid_source: string = \"unknown\";\n /**Where is my destination?\n *'main ': Sent to the main process for processing\n *'all': Broadcast to all windows (relayed through the main process)\n *String: Specify the ID of a window (windowId)\n */\n public __virid_target: string = \"\";\n /**\n *What message should I turn into at the destination?\n */\n public __virid_messageType: string = \"\";\n}\n\n/**\n *RenderMessage: Initiated by the rendering process, targeting the main process or other windows\n */\nexport abstract class ToMainMessage extends EventMessage {\n /**Where am I from?\n */\n public static __virid_source: string;\n\n /**Where is my destination?\n *'main ': Sent to the main process for processing\n *'all': Broadcast to all windows (relayed through the main process)\n *String: Specify the ID of a window (windowId)\n */\n public abstract __virid_target: string;\n\n /**\n *What message should I turn into at the destination?\n */\n public abstract __virid_messageType: string;\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { type Middleware, MessageWriter } from \"@virid/core\";\nimport { ToMainMessage } from \"./message\";\nexport const middleWare: Middleware = (message, next) => {\n // If the message inherits from ToMainMessage, intercept concurrency to the main process\n if (message instanceof ToMainMessage) {\n const { __virid_target, __virid_messageType, ...payload } = message;\n if (__virid_target == ToMainMessage.__virid_source) {\n MessageWriter.warn(\n `[Virid Render] Prohibit Sending To Oneself: ${__virid_target} is not allowed in ToRenderMessage.`,\n );\n return;\n }\n window.__VIRID_BRIDGE__.post({\n __virid_source: ToMainMessage.__virid_source,\n __virid_target: __virid_target,\n __virid_messageType: __virid_messageType,\n payload: payload, // Expand all attributes on the instance\n });\n } else {\n next();\n }\n};\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { MessageWriter, Newable } from \"@virid/core\";\nimport { type FromMainMessage } from \"./message\";\nconst MESSAGE_MAP = new Map<string, Newable<FromMainMessage>>();\n\nexport function FromMain(type: string) {\n return function (target: Newable<FromMainMessage>) {\n if (MESSAGE_MAP.has(type)) {\n MessageWriter.error(\n new Error(\n `[Virid Main] Duplicate IpcMessage: Registration for type: ${type}`,\n ),\n );\n }\n MESSAGE_MAP.set(type, target);\n };\n}\nexport function convertFromMainMessage(ipcMessage: any): void {\n const { __virid_source, __virid_target, __virid_messageType, payload } =\n ipcMessage;\n if (!__virid_messageType || !__virid_source || !__virid_target) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Incomplete Data:\\n__virid_source: ${__virid_source}\\n__virid_target:${__virid_target}\\n__virid_messageType: ${__virid_messageType}.`,\n ),\n );\n return;\n }\n if (!MESSAGE_MAP.has(__virid_messageType)) {\n MessageWriter.error(\n new Error(`[Virid Render] Unregistered type: ${__virid_messageType} `),\n );\n return;\n }\n // Find the corresponding constructor\n const MessageClass = MESSAGE_MAP.get(__virid_messageType)!;\n // Instantiate and inject parameters\n const instance = new MessageClass();\n\n // Explicitly assigning base class identifiers to ensure complete identity information of instances\n instance.__virid_source = __virid_source;\n instance.__virid_target = __virid_target;\n instance.__virid_messageType = __virid_messageType;\n\n // Restore data\n if (payload) {\n Object.assign(instance, payload);\n }\n\n // Redistribution\n MessageWriter.write(instance);\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\n\nimport { MessageWriter, type ViridApp } from \"@virid/core\";\nimport { middleWare, convertFromMainMessage, ToMainMessage } from \"./renderer\";\nimport { type PluginOption } from \"./interfaces\";\nexport function activateApp(app: ViridApp, options: PluginOption) {\n // first, check if the preload script has been loaded\n if (!window.__VIRID_BRIDGE__) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Preloading Failed: Please initialize in the preloaded script first.`,\n ),\n );\n }\n // check whether parameters are passed\n if (!options?.windowId) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Activate Failed:\\nPlease provide the windowId:${options?.windowId}.`,\n ),\n );\n }\n // register your own id, so that all messages sent to the main process will carry your own id in the future\n ToMainMessage.__virid_source = options.windowId;\n // actively send a registration message to the main process to register itself\n window.__VIRID_BRIDGE__.post({\n __virid_source: options.windowId,\n __virid_target: \"main\",\n __virid_messageType: \"VIRID_INTERNAL_REGISTER\",\n payload: {\n windowId: options.windowId,\n },\n });\n // Subscribe to the ipc channel, and convert all returned messages into our own message types according to the registry\n window.__VIRID_BRIDGE__.subscribe(convertFromMainMessage);\n // register your own middleware function to intercept messages of type ToMainMessage and forward them to the main process of electron\n app.useMiddleware(middleWare);\n}\n","/*\n * Copyright (c) 2026-present Ailrid\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * Description: Electron renderer process adapter for virid, responsible for forwarding and receiving messages from the main process.\n */\nimport { ViridPlugin, type ViridApp } from \"@virid/core\";\nexport { ToMainMessage, FromMainMessage } from \"./renderer\";\nexport * from \"./interfaces\";\nexport * from \"./renderer\";\nimport { activateApp } from \"./app\";\nimport { type PluginOption } from \"./interfaces\";\n\nexport class RenderPluginClass implements ViridPlugin<PluginOption> {\n name = \"@virid/render\";\n install(app: ViridApp, options: PluginOption) {\n activateApp(app, options);\n }\n}\n"],"mappings":";;;;uNAKA,OAASA,gBAAAA,MAAoB,cAItB,IAAeC,EAAf,MAAeA,UAAwBC,CAAAA,CAAvC,kCAGEC,EAAAA,sBAAyB,WAMzBC,EAAAA,sBAAyB,IAIzBC,EAAAA,2BAA8B,IACvC,EAd8CH,EAAAA,EAAAA,mBAAvC,IAAeD,EAAfK,EAmBeC,EAAf,MAAeA,UAAsBL,CAAAA,CAgB5C,EAhB4CA,EAAAA,EAAAA,iBAG1CM,EAHoBD,EAGNJ,kBAHT,IAAeI,EAAfE,ECvBP,OAA0BC,iBAAAA,MAAqB,cAExC,IAAMC,EAAyBC,EAAA,CAACC,EAASC,IAAAA,CAE9C,GAAID,aAAmBE,EAAe,CACpC,GAAM,CAAEC,eAAAA,EAAgBC,oBAAAA,EAAqB,GAAGC,CAAAA,EAAYL,EAC5D,GAAIG,GAAkBD,EAAcI,eAAgB,CAClDC,EAAcC,KACZ,+CAA+CL,CAAAA,qCAAmD,EAEpG,MACF,CACAM,OAAOC,iBAAiBC,KAAK,CAC3BL,eAAgBJ,EAAcI,eAC9BH,eAAgBA,EAChBC,oBAAqBA,EACrBC,QAASA,CACX,CAAA,CACF,MACEJ,EAAAA,CAEJ,EAnBsC,cCFtC,OAASW,iBAAAA,MAA8B,cAEvC,IAAMC,EAAc,IAAIC,IAEjB,SAASC,EAASC,EAAY,CACnC,OAAO,SAAUC,E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IgB,cAAcC,CAAAA,CACpB,CAhCgBlB,EAAAA,EAAAA,eCeT,IAAMmB,EAAN,MAAMA,CAAAA,CAAN,cACLC,EAAAA,YAAO,iBACPC,QAAQC,EAAeC,EAAuB,CAC5CC,EAAYF,EAAKC,CAAAA,CACnB,CACF,EALaJ,EAAAA,EAAAA,qBAAN,IAAMA,EAANM","names":["EventMessage","FromMainMessage","EventMessage","__virid_source","__virid_target","__virid_messageType","_FromMainMessage","ToMainMessage","__publicField","_ToMainMessage","MessageWriter","middleWare","__name","message","next","ToMainMessage","__virid_target","__virid_messageType","payload","__virid_source","MessageWriter","warn","window","__VIRID_BRIDGE__","post","MessageWriter","MESSAGE_MAP","Map","FromMain","type","target","has","MessageWriter","error","Error","set","convertFromMainMessage","ipcMessage","__virid_source","__virid_target","__virid_messageType","payload","MessageClass","get","instance","Object","assign","write","MessageWriter","activateApp","app","options","window","__VIRID_BRIDGE__","MessageWriter","error","Error","windowId","ToMainMessage","__virid_source","post","__virid_target","__virid_messageType","payload","subscribe","convertFromMainMessage","useMiddleware","middleWare","RenderPluginClass","name","install","app","options","activateApp","_RenderPluginClass"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/renderer/message.ts","../src/renderer/middleware.ts","../src/renderer/constant.ts","../src/renderer/decorator.ts"],"sourcesContent":["/*\n * Copyright (c) 2026-present Ailrid\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * Description: Electron renderer process adapter for virid, responsible for forwarding and receiving messages from the main process.\n */\nimport {\n MessageWriter,\n Newable,\n ViridPlugin,\n type ViridApp,\n} from \"@virid/core\";\nimport { type PluginOption } from \"./interfaces\";\nimport { FromMainMessage } from \"./renderer\";\nimport { middleWare, ToMainMessage } from \"./renderer\";\nimport { VIRID_RENDERER_METADATA } from \"./renderer/constant\";\n\nexport * from \"./interfaces\";\nexport * from \"./renderer\";\n\nexport class RenderPlugin implements ViridPlugin<PluginOption> {\n name = \"@virid/render\";\n public message_map = new Map<string, Newable<FromMainMessage>>();\n\n install(app: ViridApp, options: PluginOption) {\n // first, check if the preload script has been loaded\n if (!window.__VIRID_BRIDGE__) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Preloading Failed: Please initialize in the preloaded script first.`,\n ),\n );\n }\n // check whether parameters are passed\n if (!options?.windowId) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Activate Failed: Please provide the windowId:${options?.windowId}.`,\n ),\n );\n }\n // register your own id, so that all messages sent to the main process will carry your own id in the future\n ToMainMessage.__virid_source = options.windowId;\n // actively send a registration message to the main process to register itself\n window.__VIRID_BRIDGE__.post({\n __virid_source: options.windowId,\n __virid_target: \"main\",\n __virid_message_type: \"VIRID_INTERNAL_REGISTER\",\n payload: {\n windowId: options.windowId,\n },\n });\n // Subscribe to the ipc channel, and convert all returned messages into our own message types according to the registry\n window.__VIRID_BRIDGE__.subscribe(this.convertFromMainMessage);\n // register your own middleware function to intercept messages of type ToMainMessage and forward them to the main process of electron\n app.useMiddleware(middleWare);\n }\n bindRoute(target: Newable<FromMainMessage>) {\n const route = Reflect.getMetadata(VIRID_RENDERER_METADATA.FROMMAIN, target);\n if (this.message_map.has(route)) {\n MessageWriter.error(\n new Error(\n `[Virid Renderer] Duplicate IpcMessage: Registration for route: ${route}, message class: ${target}`,\n ),\n );\n }\n this.message_map.set(route, target);\n }\n\n convertFromMainMessage(ipcMessage: any): void {\n const { __virid_source, __virid_target, __virid_message_type, payload } =\n ipcMessage;\n if (!__virid_message_type || !__virid_source || !__virid_target) {\n MessageWriter.error(\n new Error(\n `[Virid Render] Incomplete Data:\\n__virid_source: ${__virid_source}\\n__virid_target:${__virid_target}\\n__virid_message_type: ${__virid_message_type}.`,\n ),\n );\n return;\n }\n if (!this.message_map.has(__virid_message_type)) {\n MessageWriter.error(\n new Error(`[Virid Render] Unregistered type: ${__virid_message_type} `),\n );\n return;\n }\n // Find the corresponding constructor\n const MessageClass = this.message_map.get(__virid_message_type)!;\n // Instantiate and inject parameters\n const instance = new MessageClass();\n\n // Explicitly assigning base class identifiers to ensure complete identity information of instances\n instance.__virid_source = __virid_source;\n instance.__virid_target = __virid_target;\n instance.__virid_message_type = __virid_message_type;\n\n // Restore data\n if (payload) {\n Object.assign(instance, payload);\n }\n\n // Redistribution\n MessageWriter.write(instance);\n }\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { EventMessage } from \"@virid/core\";\n/**\n * RenderMessage: Message sent by the main process\n */\nexport abstract class FromMainMessage extends EventMessage {\n /** Where am I from?\n */\n public __virid_source: string = \"unknown\";\n /**Where is my destination?\n *'main ': Sent to the main process for processing\n *'all': Broadcast to all windows (relayed through the main process)\n *String: Specify the ID of a window (windowId)\n */\n public __virid_target: string = \"\";\n /**\n *What message should I turn into at the destination?\n */\n public __virid_message_type: string = \"\";\n}\n\n/**\n *RenderMessage: Initiated by the rendering process, targeting the main process or other windows\n */\nexport abstract class ToMainMessage extends EventMessage {\n /**Where am I from?\n */\n public static __virid_source: string;\n\n /**Where is my destination?\n *'main ': Sent to the main process for processing\n *'all': Broadcast to all windows (relayed through the main process)\n *String: Specify the ID of a window (windowId)\n */\n public abstract __virid_target: string;\n\n /**\n *What message should I turn into at the destination?\n */\n public abstract __virid_message_type: string;\n}\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { type Middleware, MessageWriter } from \"@virid/core\";\nimport { ToMainMessage } from \"./message\";\nexport const middleWare: Middleware = (message, next) => {\n // If the message inherits from ToMainMessage, intercept concurrency to the main process\n if (message instanceof ToMainMessage) {\n const { __virid_target, __virid_message_type, ...payload } = message;\n if (__virid_target == ToMainMessage.__virid_source) {\n MessageWriter.warn(\n `[Virid Render] Prohibit Sending To Oneself: ${__virid_target} is not allowed in ToRenderMessage.`,\n );\n return;\n }\n window.__VIRID_BRIDGE__.post({\n __virid_source: ToMainMessage.__virid_source,\n __virid_target: __virid_target,\n __virid_message_type: __virid_message_type,\n payload: payload, // Expand all attributes on the instance\n });\n } else {\n next();\n }\n};\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { VIRID_METADATA } from \"@virid/core\";\n\nexport const VIRID_RENDERER_METADATA = {\n ...VIRID_METADATA,\n FROMMAIN: \"virid:renderer:frommain\",\n} as const;\n","/*\n * Copyright (c) 2026-present Ailrid.\n * Licensed under the Apache License, Version 2.0.\n * Project: Virid Renderer\n */\nimport { Newable } from \"@virid/core\";\nimport { type FromMainMessage } from \"./message\";\nimport { VIRID_RENDERER_METADATA } from \"./constant\";\nexport function FromMain(type: string) {\n return function (target: Newable<FromMainMessage>) {\n Reflect.defineMetadata(VIRID_RENDERER_METADATA.FROMMAIN, type, target);\n };\n}\n"],"mappings":";;;;uNAiBA,OACEA,iBAAAA,MAIK,cCjBP,OAASC,gBAAAA,MAAoB,cAItB,IAAeC,EAAf,MAAeA,UAAwBC,CAAAA,CAAvC,kCAGEC,EAAAA,sBAAyB,WAMzBC,EAAAA,sBAAyB,IAIzBC,EAAAA,4BAA+B,IACxC,EAd8CH,EAAAA,EAAAA,mBAAvC,IAAeD,EAAfK,EAmBeC,EAAf,MAAeA,UAAsBL,CAAAA,CAgB5C,EAhB4CA,EAAAA,EAAAA,iBAG1CM,EAHoBD,EAGNJ,kBAHT,IAAeI,EAAfE,ECvBP,OAA0BC,iBAAAA,MAAqB,cAExC,IAAMC,EAAyBC,EAAA,CAACC,EAASC,IAAAA,CAE9C,GAAID,aAAmBE,EAAe,CACpC,GAAM,CAAEC,eAAAA,EAA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AY6B,IAAIN,EAAOD,CAAAA,CAC9B,CAEAJ,uBAAuBY,EAAuB,CAC5C,GAAM,CAAElB,eAAAA,EAAgBE,eAAAA,EAAgBC,qBAAAA,EAAsBC,QAAAA,CAAO,EACnEc,EACF,GAAI,CAACf,GAAwB,CAACH,GAAkB,CAACE,EAAgB,CAC/DP,EAAcC,MACZ,IAAIC,MACF;kBAAoDG,CAAAA;iBAAkCE,CAAAA;wBAAyCC,CAAAA,GAAuB,CAAA,EAG1J,MACF,CACA,GAAI,CAAC,KAAKf,YAAY4B,IAAIb,CAAAA,EAAuB,CAC/CR,EAAcC,MACZ,IAAIC,MAAM,qCAAqCM,CAAAA,GAAuB,CAAA,EAExE,MACF,CAEA,IAAMgB,EAAe,KAAK/B,YAAYgC,IAAIjB,CAAAA,EAEpCkB,EAAW,IAAIF,EAGrBE,EAASrB,eAAiBA,EAC1BqB,EAASnB,eAAiBA,EAC1BmB,EAASlB,qBAAuBA,EAG5BC,GACFkB,OAAOC,OAAOF,EAAUjB,CAAAA,EAI1BT,EAAc6B,MAAMH,CAAAA,CACtB,CACF,EApFanC,EAAAA,EAAAA,gBAAN,IAAMA,EAANuC","names":["MessageWriter","EventMessage","FromMainMessage","EventMessage","__virid_source","__virid_target","__virid_message_type","_FromMainMessage","ToMainMessage","__publicField","_ToMainMessage","MessageWriter","middleWare","__name","message","next","ToMainMessage","__virid_target","__virid_message_type","payload","__virid_source","MessageWriter","warn","window","__VIRID_BRIDGE__","post","VIRID_METADATA","VIRID_RENDERER_METADATA","FROMMAIN","FromMain","type","target","Reflect","defineMetadata","VIRID_RENDERER_METADATA","FROMMAIN","RenderPlugin","name","message_map","Map","install","app","options","window","__VIRID_BRIDGE__","MessageWriter","error","Error","windowId","ToMainMessage","__virid_source","post","__virid_target","__virid_message_type","payload","subscribe","convertFromMainMessage","useMiddleware","middleWare","bindRoute","target","route","Reflect","getMetadata","VIRID_RENDERER_METADATA","FROMMAIN","has","set","ipcMessage","MessageClass","get","instance","Object","assign","write","_RenderPlugin"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@virid/renderer",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.2",
|
|
5
5
|
"description": "Electron renderer process adapter for virid, responsible for forwarding and receiving messages from the main process",
|
|
6
6
|
"author": "Ailrid",
|
|
7
7
|
"license": "Apache 2.0",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@virid/core": "0.3.
|
|
37
|
+
"@virid/core": "0.3.2"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsup --config tsup.config.ts",
|