Skip to content

Commit

Permalink
synchronize changes to settings from main process
Browse files Browse the repository at this point in the history
If the main process has changes to the settings, said settings will now
also be sent to the renderer, making them synchronized.
  • Loading branch information
0neGal committed Sep 13, 2023
1 parent 14279b4 commit 2302d70
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ if (fs.existsSync("viper.json")) {
setpath();
}

ipcRenderer.on("changed-settings", (e, new_settings) => {
// attempt to set `settings` to `new_settings`
try {
settings = {
...settings,
...new_settings
}
}catch(e) {}
})

// Show a toast message if no Internet connection has been detected.
if (! navigator.onLine) {
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ function start() {
}
});

ipcMain.on("save-settings", (event, obj) => {settings.save(obj)});
ipcMain.on("save-settings", (event, obj) => {
settings.save(obj, false)
});

ipcMain.on("saved-settings", (event, obj) => {
send("changed-settings", obj);
});

// allows renderer to check for updates
ipcMain.on("ns-update-event", (event) => {send("ns-update-event", event)});
Expand Down
8 changes: 6 additions & 2 deletions src/modules/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require("fs");
const path = require("path");
const app = require("electron").app;
const { app, ipcMain } = require("electron");

const json = require("./json");
const lang = require("../lang");
Expand Down Expand Up @@ -58,7 +58,7 @@ if (fs.existsSync("viper.json")) {
//
// you can also pass a settings object to the function and it'll try and
// merge it together with the already existing settings
settings.save = (obj = {}) => {
settings.save = (obj = {}, notify_renderer = true) => {
// refuse to save if settings aren't valid
if (invalid_settings) {
return false;
Expand Down Expand Up @@ -88,6 +88,10 @@ settings.save = (obj = {}) => {
settings.gamepath, "ns_startup_args.txt"
), settings.nsargs);
}

if (notify_renderer) {
ipcMain.emit("saved-settings", settings_content);
}
}

module.exports = settings;

0 comments on commit 2302d70

Please sign in to comment.