There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
refactor: simplify rpc code (#2823)
Alex Yangb73c75182fe8
diff --git a/apps/electron/src/helper/index.ts b/apps/electron/src/helper/index.tsindex feeb7ba122..cfeef501e3 100644--- a/apps/electron/src/helper/index.ts+++ b/apps/electron/src/helper/index.ts@@ -1,30 +1,8 @@-import type { EventBasedChannel } from 'async-call-rpc';import { AsyncCall } from 'async-call-rpc';import { events, handlers } from './exposed';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) {const flattenedHandlers = Object.entries(handlers).flatMap(([namespace, namespaceHandlers]) => {@@ -55,7 +33,22 @@ function setupRendererConnection(rendererPort: Electron.MessagePortMain) {const rpc = AsyncCall<PeersAPIs.RendererToHelper>(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,});diff --git a/apps/electron/src/helper/main-rpc.ts b/apps/electron/src/helper/main-rpc.tsindex 7874137e0f..aa93c5da63 100644--- a/apps/electron/src/helper/main-rpc.ts+++ b/apps/electron/src/helper/main-rpc.ts@@ -1,33 +1,27 @@-import { AsyncCall, type EventBasedChannel } from 'async-call-rpc';+import { AsyncCall } from 'async-call-rpc';import { getExposedMeta } from './exposed';-function createMessagePortMainChannel(- connection: Electron.ParentPort-): EventBasedChannel {- return {+const helperToMainServer: PeersAPIs.HelperToMain = {+ getMeta: () => getExposedMeta(),+};++export const mainRPC = AsyncCall<PeersAPIs.MainToHelper>(helperToMainServer, {+ strict: {+ unknownMessage: false,+ },+ channel: {on(listener) {const f = (e: Electron.MessageEvent) => {listener(e.data);};- connection.on('message', f);+ process.parentPort.on('message', f);return () => {- connection.off('message', f);+ process.parentPort.off('message', f);};},send(data) {- connection.postMessage(data);+ process.parentPort.postMessage(data);},- };-}--const helperToMainServer: PeersAPIs.HelperToMain = {- getMeta: () => getExposedMeta(),-};--export const mainRPC = AsyncCall<PeersAPIs.MainToHelper>(helperToMainServer, {- strict: {- unknownMessage: false,},- channel: createMessagePortMainChannel(process.parentPort),});diff --git a/apps/electron/src/main/helper-process.ts b/apps/electron/src/main/helper-process.tsindex 1c59ec0d0b..66bef0c73b 100644--- a/apps/electron/src/main/helper-process.ts+++ b/apps/electron/src/main/helper-process.ts@@ -92,7 +92,7 @@ class HelperProcessManager {...appMethods,};- const server = AsyncCall<PeersAPIs.HelperToMain>(mainToHelperServer, {+ this.rpc = AsyncCall<PeersAPIs.HelperToMain>(mainToHelperServer, {strict: {// the channel is shared for other purposes as well so that we do not want to// restrict to only JSONRPC messages@@ -100,7 +100,6 @@ class HelperProcessManager {},channel: new MessageEventChannel(this.#process),});- this.rpc = server;}}diff --git a/apps/electron/src/main/index.ts b/apps/electron/src/main/index.tsindex bba291f6ce..d50609d1dd 100644--- a/apps/electron/src/main/index.ts+++ b/apps/electron/src/main/index.ts@@ -66,6 +66,5 @@ app.then(ensureHelperProcess).then(restoreOrCreateWindow).then(createApplicationMenu)- .then().then(registerUpdater).catch(e => console.error('Failed create window:', e));