@zimi/remote 0.2.3 → 0.2.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/package.json +31 -31
- package/src/adaptors/electron/constants.ts +3 -3
- package/src/adaptors/electron/main.ts +59 -59
- package/src/adaptors/electron/messenger.ts +13 -13
- package/src/adaptors/electron/preload.ts +21 -21
- package/src/adaptors/electron/renderer.ts +52 -52
- package/src/index.ts +17 -17
package/package.json
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@zimi/remote",
|
|
3
|
-
"license": "MIT",
|
|
4
|
-
"version": "0.2.
|
|
5
|
-
"author": "xiaomingTang",
|
|
6
|
-
"description": "call remote functions as local",
|
|
7
|
-
"private": false,
|
|
8
|
-
"sideEffects": false,
|
|
9
|
-
"main": "dist/index.js",
|
|
10
|
-
"module": "dist/index.mjs",
|
|
11
|
-
"types": "dist/index.d.ts",
|
|
12
|
-
"repository": "https://github.com/xiaomingTang/xiaoming/tree/master/%40zimi/remote",
|
|
13
|
-
"files": [
|
|
14
|
-
"src",
|
|
15
|
-
"dist"
|
|
16
|
-
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"build": "tsup",
|
|
19
|
-
"build:watch": "tsup --watch"
|
|
20
|
-
},
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"electron": "^39.2.7",
|
|
23
|
-
"eventemitter3": "^5.0.1"
|
|
24
|
-
},
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"tsup": "^8.5.1"
|
|
27
|
-
},
|
|
28
|
-
"peerDependencies": {
|
|
29
|
-
"electron": "^39.2.7"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@zimi/remote",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"version": "0.2.4",
|
|
5
|
+
"author": "xiaomingTang",
|
|
6
|
+
"description": "call remote functions as local",
|
|
7
|
+
"private": false,
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"module": "dist/index.mjs",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"repository": "https://github.com/xiaomingTang/xiaoming/tree/master/%40zimi/remote",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup",
|
|
19
|
+
"build:watch": "tsup --watch"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"electron": "^39.2.7",
|
|
23
|
+
"eventemitter3": "^5.0.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"tsup": "^8.5.1"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"electron": "^39.2.7"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const MESSENGER_EVENT_NAME = '__remote-messenger-event__'
|
|
2
|
-
export const MESSENGER_KEY = '__remote_messenger__'
|
|
3
|
-
export const KEYOF_GET_ID = '__get-webcontents-id__'
|
|
1
|
+
export const MESSENGER_EVENT_NAME = '__remote-messenger-event__'
|
|
2
|
+
export const MESSENGER_KEY = '__remote_messenger__'
|
|
3
|
+
export const KEYOF_GET_ID = '__get-webcontents-id__'
|
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import EventEmitter from 'eventemitter3'
|
|
2
|
-
|
|
3
|
-
import { BrowserWindow, ipcMain } from 'electron'
|
|
4
|
-
import { Adaptor, AdaptorPackageData } from '../../adaptor'
|
|
5
|
-
import { isRemoteAdaptorData } from '../../remote'
|
|
6
|
-
import { KEYOF_GET_ID, MESSENGER_EVENT_NAME } from './constants'
|
|
7
|
-
|
|
8
|
-
class RemoteEventManager extends EventEmitter<{
|
|
9
|
-
[key: string]: [AdaptorPackageData]
|
|
10
|
-
}> {
|
|
11
|
-
EVERY_EVENT_NAME = '__remote_every__'
|
|
12
|
-
|
|
13
|
-
constructor() {
|
|
14
|
-
super()
|
|
15
|
-
ipcMain.on(MESSENGER_EVENT_NAME, (_, data) => {
|
|
16
|
-
if (isRemoteAdaptorData(data)) {
|
|
17
|
-
this.emit(data.name, data)
|
|
18
|
-
// 一定要抛出 every 事件,remote 包基于此处理远端的响应
|
|
19
|
-
this.emit(this.EVERY_EVENT_NAME, data)
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
onEvery(fn: (data: AdaptorPackageData) => void) {
|
|
25
|
-
this.on(this.EVERY_EVENT_NAME, fn)
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function onElectronMainEmit(data: AdaptorPackageData) {
|
|
30
|
-
// 主进程通过 webContents 发送消息到渲染进程
|
|
31
|
-
const allWindows = BrowserWindow.getAllWindows()
|
|
32
|
-
const win = allWindows.find(
|
|
33
|
-
(w) => w.webContents.id.toString() === data.targetDeviceId
|
|
34
|
-
)
|
|
35
|
-
win?.webContents.send(MESSENGER_EVENT_NAME, data)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
interface Options {
|
|
39
|
-
onEmit?: (data: AdaptorPackageData) => void
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function createElectronMainAdaptor(options?: Options) {
|
|
43
|
-
const onEmit = options?.onEmit ?? onElectronMainEmit
|
|
44
|
-
const remoteEventManager = new RemoteEventManager()
|
|
45
|
-
|
|
46
|
-
const adaptor: Adaptor = {
|
|
47
|
-
every: remoteEventManager.onEvery.bind(remoteEventManager),
|
|
48
|
-
on: remoteEventManager.on.bind(remoteEventManager),
|
|
49
|
-
once: remoteEventManager.once.bind(remoteEventManager),
|
|
50
|
-
off: remoteEventManager.off.bind(remoteEventManager),
|
|
51
|
-
emit: onEmit,
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return adaptor
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function initMessengerInMain() {
|
|
58
|
-
ipcMain.handle(KEYOF_GET_ID, (event) => event.sender.id.toString())
|
|
59
|
-
}
|
|
1
|
+
import EventEmitter from 'eventemitter3'
|
|
2
|
+
|
|
3
|
+
import { BrowserWindow, ipcMain } from 'electron'
|
|
4
|
+
import { Adaptor, AdaptorPackageData } from '../../adaptor'
|
|
5
|
+
import { isRemoteAdaptorData } from '../../remote'
|
|
6
|
+
import { KEYOF_GET_ID, MESSENGER_EVENT_NAME } from './constants'
|
|
7
|
+
|
|
8
|
+
class RemoteEventManager extends EventEmitter<{
|
|
9
|
+
[key: string]: [AdaptorPackageData]
|
|
10
|
+
}> {
|
|
11
|
+
EVERY_EVENT_NAME = '__remote_every__'
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
super()
|
|
15
|
+
ipcMain.on(MESSENGER_EVENT_NAME, (_, data) => {
|
|
16
|
+
if (isRemoteAdaptorData(data)) {
|
|
17
|
+
this.emit(data.name, data)
|
|
18
|
+
// 一定要抛出 every 事件,remote 包基于此处理远端的响应
|
|
19
|
+
this.emit(this.EVERY_EVENT_NAME, data)
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
onEvery(fn: (data: AdaptorPackageData) => void) {
|
|
25
|
+
this.on(this.EVERY_EVENT_NAME, fn)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function onElectronMainEmit(data: AdaptorPackageData) {
|
|
30
|
+
// 主进程通过 webContents 发送消息到渲染进程
|
|
31
|
+
const allWindows = BrowserWindow.getAllWindows()
|
|
32
|
+
const win = allWindows.find(
|
|
33
|
+
(w) => w.webContents.id.toString() === data.targetDeviceId
|
|
34
|
+
)
|
|
35
|
+
win?.webContents.send(MESSENGER_EVENT_NAME, data)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface Options {
|
|
39
|
+
onEmit?: (data: AdaptorPackageData) => void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createElectronMainAdaptor(options?: Options) {
|
|
43
|
+
const onEmit = options?.onEmit ?? onElectronMainEmit
|
|
44
|
+
const remoteEventManager = new RemoteEventManager()
|
|
45
|
+
|
|
46
|
+
const adaptor: Adaptor = {
|
|
47
|
+
every: remoteEventManager.onEvery.bind(remoteEventManager),
|
|
48
|
+
on: remoteEventManager.on.bind(remoteEventManager),
|
|
49
|
+
once: remoteEventManager.once.bind(remoteEventManager),
|
|
50
|
+
off: remoteEventManager.off.bind(remoteEventManager),
|
|
51
|
+
emit: onEmit,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return adaptor
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function initMessengerInMain() {
|
|
58
|
+
ipcMain.handle(KEYOF_GET_ID, (event) => event.sender.id.toString())
|
|
59
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
|
|
3
|
-
import { MESSENGER_KEY } from './constants'
|
|
4
|
-
|
|
5
|
-
interface Messenger {
|
|
6
|
-
postMessage: (data: any) => void
|
|
7
|
-
getId: () => Promise<string>
|
|
8
|
-
on: (channel: string, listener: (...args: any[]) => void) => void
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function getMessenger() {
|
|
12
|
-
return window[MESSENGER_KEY as keyof Window] as Messenger
|
|
13
|
-
}
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
import { MESSENGER_KEY } from './constants'
|
|
4
|
+
|
|
5
|
+
interface Messenger {
|
|
6
|
+
postMessage: (data: any) => void
|
|
7
|
+
getId: () => Promise<string>
|
|
8
|
+
on: (channel: string, listener: (...args: any[]) => void) => void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function getMessenger() {
|
|
12
|
+
return window[MESSENGER_KEY as keyof Window] as Messenger
|
|
13
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
|
|
3
|
-
import { KEYOF_GET_ID, MESSENGER_EVENT_NAME, MESSENGER_KEY } from './constants'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 在 preload 进程中初始化 messenger 对象
|
|
7
|
-
*/
|
|
8
|
-
export function initMessengerInPreload(
|
|
9
|
-
contextBridge: Electron.ContextBridge,
|
|
10
|
-
ipcRenderer: Electron.IpcRenderer
|
|
11
|
-
) {
|
|
12
|
-
contextBridge.exposeInMainWorld(MESSENGER_KEY, {
|
|
13
|
-
postMessage: (data: any) => {
|
|
14
|
-
ipcRenderer.postMessage(MESSENGER_EVENT_NAME, data)
|
|
15
|
-
},
|
|
16
|
-
getId: () => ipcRenderer.invoke(KEYOF_GET_ID),
|
|
17
|
-
on: (channel: string, listener: (...args: any[]) => void) => {
|
|
18
|
-
ipcRenderer.on(channel, (_, ...args) => listener(...args))
|
|
19
|
-
},
|
|
20
|
-
})
|
|
21
|
-
}
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
import { KEYOF_GET_ID, MESSENGER_EVENT_NAME, MESSENGER_KEY } from './constants'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 在 preload 进程中初始化 messenger 对象
|
|
7
|
+
*/
|
|
8
|
+
export function initMessengerInPreload(
|
|
9
|
+
contextBridge: Electron.ContextBridge,
|
|
10
|
+
ipcRenderer: Electron.IpcRenderer
|
|
11
|
+
) {
|
|
12
|
+
contextBridge.exposeInMainWorld(MESSENGER_KEY, {
|
|
13
|
+
postMessage: (data: any) => {
|
|
14
|
+
ipcRenderer.postMessage(MESSENGER_EVENT_NAME, data)
|
|
15
|
+
},
|
|
16
|
+
getId: () => ipcRenderer.invoke(KEYOF_GET_ID),
|
|
17
|
+
on: (channel: string, listener: (...args: any[]) => void) => {
|
|
18
|
+
ipcRenderer.on(channel, (_, ...args) => listener(...args))
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
}
|
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import EventEmitter from 'eventemitter3'
|
|
2
|
-
import { Adaptor, AdaptorPackageData } from '../../adaptor'
|
|
3
|
-
import { isRemoteAdaptorData } from '../../remote'
|
|
4
|
-
import { getMessenger } from './messenger'
|
|
5
|
-
import { MESSENGER_EVENT_NAME } from './constants'
|
|
6
|
-
|
|
7
|
-
class RemoteEventManager extends EventEmitter<{
|
|
8
|
-
[key: string]: [AdaptorPackageData]
|
|
9
|
-
}> {
|
|
10
|
-
EVERY_EVENT_NAME = '__remote_every__'
|
|
11
|
-
|
|
12
|
-
constructor() {
|
|
13
|
-
super()
|
|
14
|
-
if (typeof window === 'undefined') {
|
|
15
|
-
return
|
|
16
|
-
}
|
|
17
|
-
getMessenger().on(MESSENGER_EVENT_NAME, (data) => {
|
|
18
|
-
if (isRemoteAdaptorData(data)) {
|
|
19
|
-
this.emit(data.name, data)
|
|
20
|
-
// 一定要抛出 every 事件,remote 包基于此处理远端的响应
|
|
21
|
-
this.emit(this.EVERY_EVENT_NAME, data)
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
onEvery(fn: (data: AdaptorPackageData) => void) {
|
|
27
|
-
this.on(this.EVERY_EVENT_NAME, fn)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function onElectronRendererEmit(data: AdaptorPackageData) {
|
|
32
|
-
getMessenger().postMessage(data)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface Options {
|
|
36
|
-
onEmit?: (data: AdaptorPackageData) => void
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function createElectronRendererAdaptor(options?: Options) {
|
|
40
|
-
const onEmit = options?.onEmit ?? onElectronRendererEmit
|
|
41
|
-
const remoteEventManager = new RemoteEventManager()
|
|
42
|
-
|
|
43
|
-
const adaptor: Adaptor = {
|
|
44
|
-
every: remoteEventManager.onEvery.bind(remoteEventManager),
|
|
45
|
-
on: remoteEventManager.on.bind(remoteEventManager),
|
|
46
|
-
once: remoteEventManager.once.bind(remoteEventManager),
|
|
47
|
-
off: remoteEventManager.off.bind(remoteEventManager),
|
|
48
|
-
emit: onEmit,
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return adaptor
|
|
52
|
-
}
|
|
1
|
+
import EventEmitter from 'eventemitter3'
|
|
2
|
+
import { Adaptor, AdaptorPackageData } from '../../adaptor'
|
|
3
|
+
import { isRemoteAdaptorData } from '../../remote'
|
|
4
|
+
import { getMessenger } from './messenger'
|
|
5
|
+
import { MESSENGER_EVENT_NAME } from './constants'
|
|
6
|
+
|
|
7
|
+
class RemoteEventManager extends EventEmitter<{
|
|
8
|
+
[key: string]: [AdaptorPackageData]
|
|
9
|
+
}> {
|
|
10
|
+
EVERY_EVENT_NAME = '__remote_every__'
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
super()
|
|
14
|
+
if (typeof window === 'undefined') {
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
getMessenger().on(MESSENGER_EVENT_NAME, (data) => {
|
|
18
|
+
if (isRemoteAdaptorData(data)) {
|
|
19
|
+
this.emit(data.name, data)
|
|
20
|
+
// 一定要抛出 every 事件,remote 包基于此处理远端的响应
|
|
21
|
+
this.emit(this.EVERY_EVENT_NAME, data)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
onEvery(fn: (data: AdaptorPackageData) => void) {
|
|
27
|
+
this.on(this.EVERY_EVENT_NAME, fn)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function onElectronRendererEmit(data: AdaptorPackageData) {
|
|
32
|
+
getMessenger().postMessage(data)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface Options {
|
|
36
|
+
onEmit?: (data: AdaptorPackageData) => void
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createElectronRendererAdaptor(options?: Options) {
|
|
40
|
+
const onEmit = options?.onEmit ?? onElectronRendererEmit
|
|
41
|
+
const remoteEventManager = new RemoteEventManager()
|
|
42
|
+
|
|
43
|
+
const adaptor: Adaptor = {
|
|
44
|
+
every: remoteEventManager.onEvery.bind(remoteEventManager),
|
|
45
|
+
on: remoteEventManager.on.bind(remoteEventManager),
|
|
46
|
+
once: remoteEventManager.once.bind(remoteEventManager),
|
|
47
|
+
off: remoteEventManager.off.bind(remoteEventManager),
|
|
48
|
+
emit: onEmit,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return adaptor
|
|
52
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
export * from './remote'
|
|
2
|
-
export * from './adaptor'
|
|
3
|
-
export * from './response'
|
|
4
|
-
|
|
5
|
-
export * from './remoteValue/remoteValue'
|
|
6
|
-
export * from './remoteValue/exposeToRemote'
|
|
7
|
-
export type { ToFunc, RemoteCallData } from './remoteValue/type'
|
|
8
|
-
|
|
9
|
-
export * from './adaptors/iframe'
|
|
10
|
-
export * from './adaptors/http'
|
|
11
|
-
export * from './adaptors/dao3/client'
|
|
12
|
-
export * from './adaptors/dao3/server'
|
|
13
|
-
export * from './adaptors/electron/constants'
|
|
14
|
-
export * from './adaptors/electron/messenger'
|
|
15
|
-
export * from './adaptors/electron/main'
|
|
16
|
-
export * from './adaptors/electron/preload'
|
|
17
|
-
export * from './adaptors/electron/renderer'
|
|
1
|
+
export * from './remote'
|
|
2
|
+
export * from './adaptor'
|
|
3
|
+
export * from './response'
|
|
4
|
+
|
|
5
|
+
export * from './remoteValue/remoteValue'
|
|
6
|
+
export * from './remoteValue/exposeToRemote'
|
|
7
|
+
export type { ToFunc, RemoteCallData } from './remoteValue/type'
|
|
8
|
+
|
|
9
|
+
export * from './adaptors/iframe'
|
|
10
|
+
export * from './adaptors/http'
|
|
11
|
+
export * from './adaptors/dao3/client'
|
|
12
|
+
export * from './adaptors/dao3/server'
|
|
13
|
+
export * from './adaptors/electron/constants'
|
|
14
|
+
export * from './adaptors/electron/messenger'
|
|
15
|
+
export * from './adaptors/electron/main'
|
|
16
|
+
export * from './adaptors/electron/preload'
|
|
17
|
+
export * from './adaptors/electron/renderer'
|