@delight-rpc/electron 6.0.2 → 6.0.4
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 +38 -14
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -7,14 +7,20 @@ yarn add @delight-rpc/electron
|
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
## Usage
|
|
10
|
+
If your application sends RPC requests immediately after creating the RPC client,
|
|
11
|
+
you need to create the RPC server as soon as possible,
|
|
12
|
+
which means your corresponding code needs to be as **synchronized** as possible.
|
|
13
|
+
|
|
10
14
|
### Main as Client, Renderer as Server
|
|
15
|
+
#### api.d.ts
|
|
11
16
|
```ts
|
|
12
|
-
// api.d.ts
|
|
13
17
|
interface IAPI {
|
|
14
18
|
echo(message: string): string
|
|
15
19
|
}
|
|
20
|
+
```
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
#### main.js
|
|
23
|
+
```ts
|
|
18
24
|
import { app, ipcMain } from 'electron'
|
|
19
25
|
import { createClientInMain } from '@delight-rpc/electron'
|
|
20
26
|
|
|
@@ -32,8 +38,10 @@ const window = new BrowserWindow({
|
|
|
32
38
|
webPreferences: { preload: 'preload.js' }
|
|
33
39
|
})
|
|
34
40
|
window.loadFile('renderer.html')
|
|
41
|
+
```
|
|
35
42
|
|
|
36
|
-
|
|
43
|
+
#### preload.js
|
|
44
|
+
```ts
|
|
37
45
|
import { ipcRenderer } from 'electron'
|
|
38
46
|
|
|
39
47
|
window.addEventListener('message', event => {
|
|
@@ -42,8 +50,10 @@ window.addEventListener('message', event => {
|
|
|
42
50
|
ipcRenderer.postMessage('message-port', null, [port])
|
|
43
51
|
}
|
|
44
52
|
})
|
|
53
|
+
```
|
|
45
54
|
|
|
46
|
-
|
|
55
|
+
#### renderer.js
|
|
56
|
+
```ts
|
|
47
57
|
import { createServerInRenderer } from '@delight-rpc/electron'
|
|
48
58
|
|
|
49
59
|
const api: IAPI = {
|
|
@@ -62,13 +72,15 @@ window.postMessage('message-port', '*', [channel.port2])
|
|
|
62
72
|
```
|
|
63
73
|
|
|
64
74
|
### Renderer as Client, Main as Server
|
|
75
|
+
#### api.d.ts
|
|
65
76
|
```ts
|
|
66
|
-
// api.d.ts
|
|
67
77
|
interface IAPI {
|
|
68
78
|
echo(message: string): string
|
|
69
79
|
}
|
|
80
|
+
```
|
|
70
81
|
|
|
71
|
-
|
|
82
|
+
#### main.js
|
|
83
|
+
```ts
|
|
72
84
|
import { app, ipcMain } from 'electron'
|
|
73
85
|
import { createClientInMain } from '@delight-rpc/electron'
|
|
74
86
|
|
|
@@ -80,7 +92,7 @@ const api: IAPI = {
|
|
|
80
92
|
|
|
81
93
|
await app.whenReady()
|
|
82
94
|
|
|
83
|
-
ipcMain.on('message-port',
|
|
95
|
+
ipcMain.on('message-port', event => {
|
|
84
96
|
const [port] = event.ports
|
|
85
97
|
|
|
86
98
|
port.start()
|
|
@@ -91,8 +103,10 @@ const window = new BrowserWindow({
|
|
|
91
103
|
webPreferences: { preload: 'preload.js' }
|
|
92
104
|
})
|
|
93
105
|
window.loadFile('renderer.html')
|
|
106
|
+
```
|
|
94
107
|
|
|
95
|
-
|
|
108
|
+
#### preload.js
|
|
109
|
+
```ts
|
|
96
110
|
import { ipcRenderer } from 'electron'
|
|
97
111
|
|
|
98
112
|
window.addEventListener('message', event => {
|
|
@@ -101,8 +115,10 @@ window.addEventListener('message', event => {
|
|
|
101
115
|
ipcRenderer.postMessage('message-port', null, [port])
|
|
102
116
|
}
|
|
103
117
|
})
|
|
118
|
+
```
|
|
104
119
|
|
|
105
|
-
|
|
120
|
+
#### renderer.js
|
|
121
|
+
```ts
|
|
106
122
|
import { createClientInRenderer } from '@delight-rpc/electron'
|
|
107
123
|
|
|
108
124
|
// create the MessageChannel in the renderer,
|
|
@@ -116,13 +132,15 @@ await client.echo('hello world')
|
|
|
116
132
|
```
|
|
117
133
|
|
|
118
134
|
### Renderer as Client, Renderer as Server
|
|
135
|
+
#### api.d.ts
|
|
119
136
|
```ts
|
|
120
|
-
// api.d.ts
|
|
121
137
|
interface IAPI {
|
|
122
138
|
echo(message: string): string
|
|
123
139
|
}
|
|
140
|
+
```
|
|
124
141
|
|
|
125
|
-
|
|
142
|
+
#### main.js
|
|
143
|
+
```ts
|
|
126
144
|
import { app, ipcMain, MessageChannelMain } from 'electron'
|
|
127
145
|
|
|
128
146
|
await app.whenReady()
|
|
@@ -140,8 +158,10 @@ windowB.loadFile('renderer-b.html')
|
|
|
140
158
|
const channel = new MessageChannelMain()
|
|
141
159
|
windowA.webContents.postMessage('message-port', null, [channel.port1])
|
|
142
160
|
windowB.webContents.postMessage('message-port', null, [channel.port2])
|
|
161
|
+
```
|
|
143
162
|
|
|
144
|
-
|
|
163
|
+
#### preload.ts
|
|
164
|
+
```ts
|
|
145
165
|
import { ipcRenderer, contextBridge } from 'electron'
|
|
146
166
|
import { Deferred } from 'extra-promise'
|
|
147
167
|
|
|
@@ -155,8 +175,10 @@ ipcRenderer.on('message-port', event => {
|
|
|
155
175
|
await ready
|
|
156
176
|
window.postMessage('message-port', '*', [port])
|
|
157
177
|
})
|
|
178
|
+
```
|
|
158
179
|
|
|
159
|
-
|
|
180
|
+
#### renderer-a.js
|
|
181
|
+
```ts
|
|
160
182
|
import { createServerInRenderer } from '@delight-rpc/electron'
|
|
161
183
|
|
|
162
184
|
const api: IAPI = {
|
|
@@ -175,8 +197,10 @@ window.addEventListener('message', async event => {
|
|
|
175
197
|
})
|
|
176
198
|
|
|
177
199
|
window.ready()
|
|
200
|
+
```
|
|
178
201
|
|
|
179
|
-
|
|
202
|
+
#### renderer-b.js
|
|
203
|
+
```ts
|
|
180
204
|
import { createClientInRenderer } from '@delight-rpc/electron'
|
|
181
205
|
|
|
182
206
|
window.addEventListener('message', async event => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delight-rpc/electron",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"files": [
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"@typescript-eslint/parser": "^5.55.0",
|
|
44
44
|
"cross-env": "^7.0.3",
|
|
45
45
|
"delight-rpc": "^6.0.0",
|
|
46
|
-
"electron": "^
|
|
46
|
+
"electron": "^30.0.2",
|
|
47
47
|
"eslint": "8.36.0",
|
|
48
48
|
"husky": "4",
|
|
49
49
|
"jest": "^29.5.0",
|
|
@@ -65,6 +65,6 @@
|
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
67
|
"delight-rpc": "^5.0.0 || ^6.0.0",
|
|
68
|
-
"electron": "
|
|
68
|
+
"electron": ">=16.0.1 <31"
|
|
69
69
|
}
|
|
70
70
|
}
|