electron#IpcMainInvokeEvent TypeScript Examples

The following examples show how to use electron#IpcMainInvokeEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: app.ts    From noteworthy with GNU Affero General Public License v3.0 6 votes vote down vote up
// INITIALIZATION //////////////////////////////////////

	init(){
		ipcMain.handle(
			"command", 
			<T extends MainIpcChannel>(
				evt: IpcMainInvokeEvent, channel: T,
				key: FunctionPropertyNames<MainIpcHandlers[T]>, data: any
			) => {
				console.log(`MainIPC :: handling event :: ${channel} ${key}`);
				return this.handle(channel, key, data);
			}
		);
	}
Example #2
Source File: i18nMainBindings.ts    From TidGi-Desktop with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This is the code that will go into the main.js file
 * in order to set up the ipc main bindings
 */
export function mainBindings(): void {
  ipcMain.handle(I18NChannels.readFileRequest, (_event: IpcMainInvokeEvent, readFileArguments: IReadFileRequest) => {
    const localeFilePath = path.join(LOCALIZATION_FOLDER, readFileArguments.filename);
    const windowService = container.get<IWindowService>(serviceIdentifier.Window);
    fs.readFile(localeFilePath, 'utf8', (error, data) => {
      void windowService.sendToAllWindows(I18NChannels.readFileResponse, {
        key: readFileArguments.key,
        error,
        data: typeof data !== 'undefined' && data !== null ? data.toString() : '',
      });
    });
  });

  ipcMain.handle(I18NChannels.writeFileRequest, (_event: IpcMainInvokeEvent, writeFileArguments: IWriteFileRequest) => {
    const localeFilePath = path.join(LOCALIZATION_FOLDER, writeFileArguments.filename);
    const localeFileFolderPath = path.dirname(localeFilePath);
    const windowService = container.get<IWindowService>(serviceIdentifier.Window);
    fs.ensureDir(localeFileFolderPath, (directoryCreationError?: Error) => {
      // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
      if (directoryCreationError) {
        console.error(directoryCreationError);
        return;
      }
      fs.writeFile(localeFilePath, JSON.stringify(writeFileArguments.data), (error: Error) => {
        void windowService.sendToAllWindows(I18NChannels.writeFileResponse, {
          keys: writeFileArguments.keys,
          error,
        });
      });
    });
  });
}
Example #3
Source File: ipc-events.ts    From WowUp with GNU General Public License v3.0 5 votes vote down vote up
function handle(
  channel: RendererChannels,
  listener: (event: IpcMainInvokeEvent, ...args: any[]) => Promise<void> | any
) {
  ipcMain.handle(channel, listener);
}
Example #4
Source File: stores.ts    From WowUp with GNU General Public License v3.0 5 votes vote down vote up
export function initializeStoreIpcHandlers(): void {
  // Return the store value for a specific key
  ipcMain.handle(IPC_STORE_GET_ALL, (evt: IpcMainInvokeEvent, storeName: string): any => {
    const store = stores[storeName];

    const items = [];
    for (const result of store) {
      const item = result[1];
      items.push(item);
    }

    return items;
  });

  // Return the store value for a specific key
  ipcMain.handle(IPC_STORE_GET_OBJECT, (evt: IpcMainInvokeEvent, storeName: string, key: string): any => {
    const store = stores[storeName];
    return store ? store.get(key, undefined) : undefined;
  });

  ipcMain.on(IPC_STORE_GET_OBJECT_SYNC, (evt, storeName: string, key: string) => {
    const store = stores[storeName];
    evt.returnValue = store ? store.get(key, undefined) : undefined;
  });

  // Set the store value for a specific key
  ipcMain.handle(IPC_STORE_SET_OBJECT, (evt: IpcMainInvokeEvent, storeName: string, key: string, value: any): void => {
    const store = stores[storeName];

    if (typeof value === "object" || Array.isArray(value)) {
      store?.set(key, value);
    } else {
      store?.set(key, value.toString());
    }
  });

  // Remove the store value for a specific key
  ipcMain.handle(IPC_STORE_REMOVE_OBJECT, (evt: IpcMainInvokeEvent, storeName: string, key: string): void => {
    const store = stores[storeName];
    store?.delete(key);
  });
}
Example #5
Source File: mysteriumNode.ts    From mysterium-vpn-desktop with MIT License 5 votes vote down vote up
registerIPC(getMainWindow: () => BrowserWindow | null): void {
        ipcMain.handle(MainIpcListenChannels.StartNode, () => {
            return this.start()
        })
        ipcMain.handle(MainIpcListenChannels.StopNode, () => {
            return this.stop()
        })
        ipcMain.handle(MainIpcListenChannels.KillGhosts, async () => {
            if (isProduction()) {
                await Promise.all([this.killGhost(4050), this.killGhost(44050)])
            }
        })
        ipcMain.handle(
            MainIpcListenChannels.ImportIdentity,
            async (event: IpcMainInvokeEvent, opts: ImportIdentityOpts): Promise<IpcResponse> => {
                return this.importIdentity(opts)
            },
        )
        ipcMain.handle(MainIpcListenChannels.ImportIdentityChooseFile, async (): Promise<IpcResponse> => {
            const mainWindow = getMainWindow()
            if (!mainWindow) {
                return {}
            }
            const filename = dialog
                .showOpenDialogSync(mainWindow, {
                    filters: [{ extensions: ["json"], name: "keystore" }],
                })
                ?.find(Boolean)
            return Promise.resolve({ result: filename })
        })
        ipcMain.handle(
            MainIpcListenChannels.ExportIdentity,
            async (event: IpcMainInvokeEvent, opts: ExportIdentityOpts): Promise<IpcResponse> => {
                const mainWindow = getMainWindow()
                if (!mainWindow) {
                    return {}
                }
                const filename = dialog.showSaveDialogSync(mainWindow, {
                    filters: [{ extensions: ["json"], name: "keystore" }],
                    defaultPath: `${opts.id}.json`,
                })
                if (!filename) {
                    return {}
                }
                return await this.exportIdentity({ id: opts.id, filename: filename, passphrase: opts.passphrase })
            },
        )
    }
Example #6
Source File: ipc-main.ts    From electron-playground with MIT License 5 votes vote down vote up
ipcMainHandle = <T>(
  eventName: IPCEventName,
  listener: (event: IpcMainInvokeEvent, ...args: any[]) => Promise<T> | void | T,
): void => {
  ipcMain.handle(eventName, async (event, ...args: any[]) => {
    return listener(event, ...args)
  })
}