svelte multiple api calls with subsequent click on a button?

It seems that when using electron api each individual press of a button calls the api function x+1 times where x is the times you've pressed the button prior.
7 Replies
MartynasXS
MartynasXS10mo ago
Main.js
ipcMain.on('to-main', (event, args) => {
if(args == "get-file"){
let result = dialog.showOpenDialogSync({
title: "Select a folder!",
properties: ["openDirectory"],
})
event.reply('from-main', result.join(""));
}
event.reply('from-main', "-")
})
ipcMain.on('to-main', (event, args) => {
if(args == "get-file"){
let result = dialog.showOpenDialogSync({
title: "Select a folder!",
properties: ["openDirectory"],
})
event.reply('from-main', result.join(""));
}
event.reply('from-main', "-")
})
preload.js
const { contextBridge, ipcRenderer } = require("electron");

console.log('preload.js loaded');
contextBridge.exposeInMainWorld(
'api', {
send: (channel, data) => {
ipcRenderer.send(channel, data)
},
sendSync: (channel, data) => {
ipcRenderer.sendSync(channel, data)
},
receive: (channel, func) => {
ipcRenderer.on(channel, (event, ...args) => func(...args))
}
});
const { contextBridge, ipcRenderer } = require("electron");

console.log('preload.js loaded');
contextBridge.exposeInMainWorld(
'api', {
send: (channel, data) => {
ipcRenderer.send(channel, data)
},
sendSync: (channel, data) => {
ipcRenderer.sendSync(channel, data)
},
receive: (channel, func) => {
ipcRenderer.on(channel, (event, ...args) => func(...args))
}
});
+page.svelte
<script>
let Location = "binsplash";
const handleClick = () => {
window.api.send("to-main", "get-file");
window.api.receive("from-main", (data) => {
console.log(data);
});
};
</script>
<button on:click={()=>{handleClick()}}>koamgosmgoamsm</button>
<script>
let Location = "binsplash";
const handleClick = () => {
window.api.send("to-main", "get-file");
window.api.receive("from-main", (data) => {
console.log(data);
});
};
</script>
<button on:click={()=>{handleClick()}}>koamgosmgoamsm</button>
MartynasXS
MartynasXS10mo ago
No description
MartynasXS
MartynasXS10mo ago
as you can see i get path 1 time first time around and +1 each time i press the button
hyprsonic
hyprsonic10mo ago
@MartynasXS you're probably better off asking this in the svelte discord
MartynasXS
MartynasXS10mo ago
probably
hyprsonic
hyprsonic10mo ago
Yeah, the support in there is a lot better
MartynasXS
MartynasXS10mo ago
receive: (channel, func) => {
ipcRenderer.on(channel, (event, ...args) => {
func(...args)
ipcRenderer.removeAllListeners(channel)
})
}
receive: (channel, func) => {
ipcRenderer.on(channel, (event, ...args) => {
func(...args)
ipcRenderer.removeAllListeners(channel)
})
}
seemingly shower thoughts gave me the answer or wait i can just change it to once not on