Yazılım Electron typescript "IPCRenderer" "IPCMain" çalıştıramadım

kilicsizoglu

Zeptopat
Katılım
4 Ağustos 2023
Mesajlar
1
Daha fazla  
Cinsiyet
Erkek
JavaScript:
import { IpcRendererEvent, ipcRenderer } from 'electron';

import todo = require('./todo');

let inputText = (<HTMLInputElement>document.getElementById("todoInputText"));
let addButton = (<HTMLButtonElement>document.getElementById("todoAddButton"));
let ToDoDataDiv = (<HTMLDivElement>document.getElementById('todoDataDiv'));

window.onload = function() {
    refreshList();
}

function refreshList() {

    let t : Array<todo.todo>;

    ipcRenderer.on('get-todo', (event : IpcRendererEvent, ts : Array<todo.todo>) => {
        t = ts;
    });

    if (t.length > 0) {
   
        if (ToDoDataDiv.hasChildNodes()) {
            let i = 0;
            while (ToDoDataDiv.children.length < i) {
                ToDoDataDiv.removeChild(ToDoDataDiv.children[i]);
                i++;
            }
        }
   
        let tableElement = (<HTMLTableElement>document.createElement('table'));
   
        let tableRow = (<HTMLTableRowElement>document.createElement('tr'));
   
        let idText = (document.createElement('th'));
        idText.innerText = "ID";
        tableRow.appendChild(idText);
   
        tableElement.appendChild(tableRow);
   
        let i : number = 0;
        while (t.length > i) {
            let tableRow1 = (<HTMLTableRowElement>document.createElement('tr'));
           
            let td1 = (document.createElement('td'));
            td1.innerText = t[i].id.toString();
   
            let td2 = (document.createElement('td'));
            td2.innerText = t[i].text.toString();
   
            tableRow1.appendChild(td1);
            tableRow1.appendChild(td2);
   
            tableElement.appendChild(tableRow1);
        }
   
        ToDoDataDiv.appendChild(tableElement);

    }

}

addButton.addEventListener('click', () => {
    console.log(inputText.value);
    if (inputText.value != "") {
        ipcRenderer.send('insert-todo', inputText.value);
        refreshList();
    }
});

JavaScript:
import { app, BrowserWindow, ipcMain } from "electron";
import * as path from "path";

import { sqliteconnect } from './sqliteconnect'
import { todo } from "./todo";

function createWindow() {
  const mainWindow = new BrowserWindow({
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
      nodeIntegration: true,
      contextIsolation: false,
    },
    width: 800,
  });

  ipcMain.on('get-todo', async (event) => {
    let t : Array<todo> = await sqliteconnect.getTodos();
    return t;
  });

  mainWindow.loadFile(path.join(__dirname, "../index.html"));

}

app.whenReady().then(() => {
  createWindow();

  app.on("activate", function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

IpcRenderer isteğini bir turlu çalıştıramadım. Sizce sorun nereden kaynaklanıyor ? :D

son durum. ipcRenderer include etmiyor. neden acaba.
Kod:
import { ipcRenderer } from 'electron'

import { todo } from "./todo"

console.log(ipcRenderer);

let inputText = (<HTMLInputElement>document.getElementById("todoInputText"));
let addButton = (<HTMLButtonElement>document.getElementById("todoAddButton"));
let ToDoDataDiv = (<HTMLDivElement>document.getElementById('todoDataDiv'));

window.onload = function() {
    refreshList();
}

function refreshList() {

    ipcRenderer.send("get-todo");

    ipcRenderer.on('get-todo-reply', (event, response : Array<todo>) => {

        console.log(response[0].text);
        let t : Array<todo> = response;

        if (t.length > 0) {
    
            if (ToDoDataDiv.hasChildNodes()) {
                let i = 0;
                while (ToDoDataDiv.children.length < i) {
                    ToDoDataDiv.removeChild(ToDoDataDiv.children[i]);
                    i++;
                }
            }
        
            let tableElement = (<HTMLTableElement>document.createElement('table'));
        
            let tableRow = (<HTMLTableRowElement>document.createElement('tr'));
        
            let idText = (document.createElement('th'));
            idText.innerText = "ID";
            tableRow.appendChild(idText);
        
            tableElement.appendChild(tableRow);
        
            let i : number = 0;
            while (t.length > i) {
                let tableRow1 = (<HTMLTableRowElement>document.createElement('tr'));
                
                let td1 = (document.createElement('td'));
                td1.innerText = t[i].id.toString();
        
                let td2 = (document.createElement('td'));
                td2.innerText = t[i].text.toString();
        
                tableRow1.appendChild(td1);
                tableRow1.appendChild(td2);
        
                tableElement.appendChild(tableRow1);
            }
        
            ToDoDataDiv.appendChild(tableElement);
    
        }
    
    });

}

addButton.addEventListener('click', () => {
    console.log(inputText.value);
    if (inputText.value != "") {
        ipcRenderer.send('insert-todo', inputText.value);
        refreshList();
    }
});
 
Son düzenleme:

Yeni konular

Geri
Yukarı