mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
1 parent
ef3962f8b5
commit
323a4c141e
18 files changed
+319
-379
No files matched your search
@@ -6,7 +6,7 @@ import { action, observable, reaction, runInAction, toJS, when } from "mobx";
|
||||
import Singleton from "./utils/singleton";
|
||||
import { getAppVersion } from "./utils/app-version";
|
||||
import logger from "../main/logger";
|
||||
import { sendMessage } from "./ipc";
|
||||
import { broadcastIpc } from "./ipc";
|
||||
import isEqual from "lodash/isEqual";
|
||||
|
||||
export interface BaseStoreParams<T = any> extends ConfOptions<T> {
|
||||
@@ -118,7 +118,7 @@ export class BaseStore<T = any> extends Singleton {
|
||||
protected async onModelChange(model: T) {
|
||||
if (ipcMain) {
|
||||
this.save(model); // save config file
|
||||
sendMessage({ channel: this.syncChannel, args: [model] }); // broadcast to renderer views
|
||||
broadcastIpc({ channel: this.syncChannel, args: [model] }); // broadcast to renderer views
|
||||
}
|
||||
// send "update-request" to main-process
|
||||
if (ipcRenderer) {
|
||||
|
||||
+8
-14
@@ -19,11 +19,11 @@ export interface IpcMessageOpts<A extends any[] = any> {
|
||||
channel: IpcChannel
|
||||
webContentId?: number; // sends to single webContents view
|
||||
filter?: (webContent: WebContents) => boolean
|
||||
timeout?: number; // fixme: support
|
||||
timeout?: number; // fixme: add support
|
||||
args?: A;
|
||||
}
|
||||
|
||||
export function sendMessage({ channel, webContentId, filter, args = [] }: IpcMessageOpts) {
|
||||
export function broadcastIpc({ channel, webContentId, filter, args = [] }: IpcMessageOpts) {
|
||||
const singleView = webContentId ? webContents.fromId(webContentId) : null;
|
||||
let views = singleView ? [singleView] : webContents.getAllWebContents();
|
||||
if (filter) {
|
||||
@@ -37,16 +37,16 @@ export function sendMessage({ channel, webContentId, filter, args = [] }: IpcMes
|
||||
}
|
||||
|
||||
// todo: support timeout + merge with sendMessage?
|
||||
export async function invokeMessage<T extends any[], R = any>(channel: IpcChannel, ...args: T): Promise<R> {
|
||||
logger.debug(`[IPC]: invoke channel "${channel}"`, { args });
|
||||
export async function invokeIpc<R = any>(channel: IpcChannel, ...args: any[]): Promise<R> {
|
||||
logger.info(`[IPC]: invoke channel "${channel}"`, { args });
|
||||
return ipcRenderer.invoke(channel, ...args);
|
||||
}
|
||||
|
||||
// todo: make isomorphic api
|
||||
export function handleMessage<T extends any[]>(channel: IpcChannel, handler: IpcMessageHandler<T>, options: IpcHandleOpts = {}) {
|
||||
export function handleIpc<T extends any[]>(channel: IpcChannel, handler: IpcMessageHandler<T>, options: IpcHandleOpts = {}) {
|
||||
const { timeout = 0 } = options;
|
||||
ipcMain.handle(channel, async (event, ...args: T) => {
|
||||
logger.debug(`[IPC]: handle "${channel}"`, { args });
|
||||
logger.info(`[IPC]: handle "${channel}"`, { args });
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let timerId;
|
||||
if (timeout) {
|
||||
@@ -57,17 +57,11 @@ export function handleMessage<T extends any[]>(channel: IpcChannel, handler: Ipc
|
||||
}
|
||||
try {
|
||||
const result = await handler(...args); // todo: maybe exec in separate thread/worker
|
||||
resolve(result);
|
||||
clearTimeout(timerId);
|
||||
return result;
|
||||
} catch (err) {
|
||||
logger.debug(`[IPC]: handling "${channel}" error`, { err });
|
||||
reject(err);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function handleMessages(messages: Record<string, IpcMessageHandler>, options?: IpcHandleOpts) {
|
||||
Object.entries(messages).forEach(([channel, handler]) => {
|
||||
handleMessage(channel, handler, options);
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user