๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Development/Electron

[Electron] ipcRenderer.invoke & ipcMin.handle

by DevIseo 2023. 6. 15.

IpcRenderer.invoke

 ๐Ÿ’ก Electron์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜.

  • ipcRenderer ๊ฐ์ฒด๋Š” ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ฃผ๋กœ ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์™€์˜ ๋น„๋™๊ธฐ์ ์ธ ํ†ต์‹ ์„ ์œ„ํ•ด ์‚ฌ์šฉ.
  • ipcRenderer.invoke๋Š” ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์— ๋น„๋™๊ธฐ์ ์ธ ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
  • ์ด ๋ฉ”์„œ๋“œ๋Š” Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์—์„œ ์ฒ˜๋ฆฌ๋˜๊ณ  ์™„๋ฃŒ๋œ ํ›„์— Promise ๊ฒฐ๊ณผ๊ฐ€ ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์— ์ „๋‹ฌ.
  • ipcRenderer.invoke ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์—์„œ ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ ipcMain.handle์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜

์˜ˆ์ œ

// ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ์˜ ์˜ˆ์ œ ์ฝ”๋“œ
const { ipcRenderer } = require('electron');

async function fetchDataFromMainProcess() {
  try {
		// 'fetch-data'๋ผ๋Š” ๋น„๋™๊ธฐ์ ์ธ ์š”์ฒญ์„ ๋ณด๋ƒ„
    const result = await ipcRenderer.invoke('fetch-data'); 
    console.log(result); // ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์—์„œ ๋ฐ˜ํ™˜๋œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
  } catch (error) {
    console.error(error); // ์—๋Ÿฌ ์ฒ˜๋ฆฌ
  }
}

fetchDataFromMainProcess();

โœ… ipcRenderer.invoke | ipcMain.handle

๊ฐœ๋… ๋ฐ ์˜ˆ์‹œ

ipcRenderer.invoke์™€ ipcMain.handle๋Š” Electron ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋‘ ๊ฐ€์ง€ IPC(Inter-Process Communication) ๋ฉ”์„œ๋“œ

ipcRenderer

ipcRenderer.invoke๋Š” ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ ipcMain.handle์—์„œ ๋“ฑ๋กํ•œ ์ด๋ฒคํŠธ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‘๋‹ต์„ ๋ฐ›๋Š” ๋ฐ ์‚ฌ์šฉ

์˜ˆ๋ฅผ ๋“ค์–ด, ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ ipcMain.handle๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์— ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๊ณ , ipcRenderer.invoke๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ.

 

Main process

const { ipcMain } = require('electron')

ipcMain.handle('getData', async (event, arg) => {
  // handle the data request
  return result;
})

Renderer process

const { ipcRenderer } = require('electron')

async function getDataFromMainProcess() {
  const result = await ipcRenderer.invoke('getData', arg);
  // handle the result from the main process
}

ipcMain

ipcMain.handle๋Š” ๋ฐ˜๋ฉด, ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์—์„œ ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ ํ˜ธ์ถœ๋˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์—์„œ**ipcRenderer.invoke**๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•  ๋•Œ, ipcMain.handle๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜

main process

const { ipcMain } = require('electron')

ipcMain.handle('getData', async (event, arg) => {
  // handle the data request
  return result;
})

Renderer process

const { ipcRenderer } = require('electron')

async function getDataFromMainProcess() {
  const result = await ipcRenderer.invoke('getData', arg);
  // handle the result from the main process
}

์ฆ‰, ipcRenderer.invoke์™€ ipcMain.handle๋Š” Electron์—์„œ ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์™€ ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค ๊ฐ„์˜ ํ†ต์‹ ์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ

 

๋Œ“๊ธ€