mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-26 11:43:05 +03:00
refactor: simplify rpc code (#2823)
This commit is contained in:
parent
775d6212d3
commit
b73c75182f
@ -1,30 +1,8 @@
|
|||||||
import type { EventBasedChannel } from 'async-call-rpc';
|
|
||||||
import { AsyncCall } from 'async-call-rpc';
|
import { AsyncCall } from 'async-call-rpc';
|
||||||
|
|
||||||
import { events, handlers } from './exposed';
|
import { events, handlers } from './exposed';
|
||||||
import { logger } from './logger';
|
import { logger } from './logger';
|
||||||
|
|
||||||
const createMessagePortMainChannel = (
|
|
||||||
connection: Electron.MessagePortMain
|
|
||||||
): EventBasedChannel => {
|
|
||||||
return {
|
|
||||||
on(listener) {
|
|
||||||
const f = (e: Electron.MessageEvent) => {
|
|
||||||
listener(e.data);
|
|
||||||
};
|
|
||||||
connection.on('message', f);
|
|
||||||
// MUST start the connection to receive messages
|
|
||||||
connection.start();
|
|
||||||
return () => {
|
|
||||||
connection.off('message', f);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
send(data) {
|
|
||||||
connection.postMessage(data);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
function setupRendererConnection(rendererPort: Electron.MessagePortMain) {
|
function setupRendererConnection(rendererPort: Electron.MessagePortMain) {
|
||||||
const flattenedHandlers = Object.entries(handlers).flatMap(
|
const flattenedHandlers = Object.entries(handlers).flatMap(
|
||||||
([namespace, namespaceHandlers]) => {
|
([namespace, namespaceHandlers]) => {
|
||||||
@ -55,7 +33,22 @@ function setupRendererConnection(rendererPort: Electron.MessagePortMain) {
|
|||||||
const rpc = AsyncCall<PeersAPIs.RendererToHelper>(
|
const rpc = AsyncCall<PeersAPIs.RendererToHelper>(
|
||||||
Object.fromEntries(flattenedHandlers),
|
Object.fromEntries(flattenedHandlers),
|
||||||
{
|
{
|
||||||
channel: createMessagePortMainChannel(rendererPort),
|
channel: {
|
||||||
|
on(listener) {
|
||||||
|
const f = (e: Electron.MessageEvent) => {
|
||||||
|
listener(e.data);
|
||||||
|
};
|
||||||
|
rendererPort.on('message', f);
|
||||||
|
// MUST start the connection to receive messages
|
||||||
|
rendererPort.start();
|
||||||
|
return () => {
|
||||||
|
rendererPort.off('message', f);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
send(data) {
|
||||||
|
rendererPort.postMessage(data);
|
||||||
|
},
|
||||||
|
},
|
||||||
log: false,
|
log: false,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -1,26 +1,7 @@
|
|||||||
import { AsyncCall, type EventBasedChannel } from 'async-call-rpc';
|
import { AsyncCall } from 'async-call-rpc';
|
||||||
|
|
||||||
import { getExposedMeta } from './exposed';
|
import { getExposedMeta } from './exposed';
|
||||||
|
|
||||||
function createMessagePortMainChannel(
|
|
||||||
connection: Electron.ParentPort
|
|
||||||
): EventBasedChannel {
|
|
||||||
return {
|
|
||||||
on(listener) {
|
|
||||||
const f = (e: Electron.MessageEvent) => {
|
|
||||||
listener(e.data);
|
|
||||||
};
|
|
||||||
connection.on('message', f);
|
|
||||||
return () => {
|
|
||||||
connection.off('message', f);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
send(data) {
|
|
||||||
connection.postMessage(data);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const helperToMainServer: PeersAPIs.HelperToMain = {
|
const helperToMainServer: PeersAPIs.HelperToMain = {
|
||||||
getMeta: () => getExposedMeta(),
|
getMeta: () => getExposedMeta(),
|
||||||
};
|
};
|
||||||
@ -29,5 +10,18 @@ export const mainRPC = AsyncCall<PeersAPIs.MainToHelper>(helperToMainServer, {
|
|||||||
strict: {
|
strict: {
|
||||||
unknownMessage: false,
|
unknownMessage: false,
|
||||||
},
|
},
|
||||||
channel: createMessagePortMainChannel(process.parentPort),
|
channel: {
|
||||||
|
on(listener) {
|
||||||
|
const f = (e: Electron.MessageEvent) => {
|
||||||
|
listener(e.data);
|
||||||
|
};
|
||||||
|
process.parentPort.on('message', f);
|
||||||
|
return () => {
|
||||||
|
process.parentPort.off('message', f);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
send(data) {
|
||||||
|
process.parentPort.postMessage(data);
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
@ -92,7 +92,7 @@ class HelperProcessManager {
|
|||||||
...appMethods,
|
...appMethods,
|
||||||
};
|
};
|
||||||
|
|
||||||
const server = AsyncCall<PeersAPIs.HelperToMain>(mainToHelperServer, {
|
this.rpc = AsyncCall<PeersAPIs.HelperToMain>(mainToHelperServer, {
|
||||||
strict: {
|
strict: {
|
||||||
// the channel is shared for other purposes as well so that we do not want to
|
// the channel is shared for other purposes as well so that we do not want to
|
||||||
// restrict to only JSONRPC messages
|
// restrict to only JSONRPC messages
|
||||||
@ -100,7 +100,6 @@ class HelperProcessManager {
|
|||||||
},
|
},
|
||||||
channel: new MessageEventChannel(this.#process),
|
channel: new MessageEventChannel(this.#process),
|
||||||
});
|
});
|
||||||
this.rpc = server;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +66,5 @@ app
|
|||||||
.then(ensureHelperProcess)
|
.then(ensureHelperProcess)
|
||||||
.then(restoreOrCreateWindow)
|
.then(restoreOrCreateWindow)
|
||||||
.then(createApplicationMenu)
|
.then(createApplicationMenu)
|
||||||
.then()
|
|
||||||
.then(registerUpdater)
|
.then(registerUpdater)
|
||||||
.catch(e => console.error('Failed create window:', e));
|
.catch(e => console.error('Failed create window:', e));
|
||||||
|
Loading…
Reference in New Issue
Block a user