Skip to content

Commit

Permalink
use custom settings module
Browse files Browse the repository at this point in the history
  • Loading branch information
clockley committed Dec 11, 2024
1 parent 4fe368c commit a0cac2b
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 40 deletions.
2 changes: 1 addition & 1 deletion main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ along with this library. If not, see <https://www.gnu.org/licenses/>.

"use strict";
import { app, BrowserWindow, ipcMain, screen, powerSaveBlocker } from 'electron/main';
import settings from 'electron-settings';
import settings from './settings.mjs'
import { readdir, readFile } from 'fs/promises';
import path from 'path';
const isDevMode = process.env.ems_dev === 'true';
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"dependencies": {
"atem-connection": "^3.5.0",
"bonjour": "^3.5.0",
"electron-settings": "^4.0.4",
"hls.js": "^1.6.0-beta.1"
"hls.js": "^1.6.0-beta.1",
"write-file-atomic": "^6.0.0"
},
"build": {
"appId": "com.ejaxmediasystem.app",
Expand Down
217 changes: 217 additions & 0 deletions settings.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
//This is a JS port of electron-settings it only works in the main process and only only reads the file once
/*MIT License
Copyright (c) 2020 Nathan Buchar <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import { join } from 'path';
import { readFileSync, existsSync, mkdirSync } from 'fs';
import writeFileAtomic from 'write-file-atomic';
import { app } from 'electron';

// Memory cache
const cache = {
data: null,
dirPath: null,
filePath: null,
};

function getSettingsDirPath() {
if (cache.dirPath) {
return cache.dirPath;
}
cache.dirPath = app.getPath('userData');
return cache.dirPath;
}

function getSettingsFilePath() {
if (cache.filePath) {
return cache.filePath;
}
cache.filePath = join(getSettingsDirPath(), 'settings.json');
return cache.filePath;
}

// Optimized get value from object using path
function getValueByPath(obj, path) {
if (!path) return obj;
const parts = Array.isArray(path) ? path : path.split('.');
let result = obj;
for (const part of parts) {
if (result === null || result === undefined) return undefined;
// Handle array index notation
if (part.includes('[') && part.includes(']')) {
const [key, index] = part.split('[');
result = result[key]?.[parseInt(index)];
} else {
result = result[part];
}
}
return result;
}

// Optimized set value in object using path
function setValueByPath(obj, path, value) {
const parts = Array.isArray(path) ? path : path.split('.');
let current = obj;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!(part in current)) {
current[part] = {};
}
current = current[part];
}
current[parts[parts.length - 1]] = value;
return obj;
}

// Load settings from disk
function loadSettings() {
if (cache.data) {
return cache.data;
}

try {
const filePath = getSettingsFilePath();
if (!existsSync(filePath)) {
cache.data = {};
return cache.data;
}

const data = readFileSync(filePath, 'utf-8');
cache.data = JSON.parse(data);
return cache.data;
} catch (err) {
cache.data = {};
return cache.data;
}
}

function saveSettings(data) {
const filePath = getSettingsFilePath();
const dirPath = getSettingsDirPath();

if (!existsSync(dirPath)) {
mkdirSync(dirPath, { recursive: true });
}

writeFileAtomic.sync(filePath, JSON.stringify(data));
cache.data = data;
}

// Core functions
function get(keyPath) {
const settings = loadSettings();
return getValueByPath(settings, keyPath);
}

function has(keyPath) {
const settings = loadSettings();
const value = getValueByPath(settings, keyPath);
return value !== undefined;
}

async function set(keyPath, value) {
if (arguments.length === 1) {
saveSettings(keyPath);
return;
}

const settings = loadSettings();
setValueByPath(settings, keyPath, value);
saveSettings(settings);
}

function setSync(keyPath, value) {
if (arguments.length === 1) {
saveSettings(keyPath);
return;
}

const settings = loadSettings();
setValueByPath(settings, keyPath, value);
saveSettings(settings);
}

async function unset(keyPath) {
if (!keyPath) {
saveSettings({});
return;
}

const settings = loadSettings();
const parts = Array.isArray(keyPath) ? keyPath : keyPath.split('.');
let current = settings;
for (let i = 0; i < parts.length - 1; i++) {
if (!(parts[i] in current)) return;
current = current[parts[i]];
}
delete current[parts[parts.length - 1]];
saveSettings(settings);
}

function unsetSync(keyPath) {
if (!keyPath) {
saveSettings({});
return;
}

const settings = loadSettings();
const parts = Array.isArray(keyPath) ? keyPath : keyPath.split('.');
let current = settings;
for (let i = 0; i < parts.length - 1; i++) {
if (!(parts[i] in current)) return;
current = current[parts[i]];
}
delete current[parts[parts.length - 1]];
saveSettings(settings);
}

function file() {
return getSettingsFilePath();
}

// getSync is just an alias for get since get is already sync
const getSync = get;
const hasSync = has;

export {
get,
getSync,
set,
setSync,
has,
hasSync,
unset,
unsetSync,
file
};

export default {
get,
getSync,
set,
setSync,
has,
hasSync,
unset,
unsetSync,
file
};
43 changes: 6 additions & 37 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1325,19 +1325,6 @@ __metadata:
languageName: node
linkType: hard

"electron-settings@npm:^4.0.4":
version: 4.0.4
resolution: "electron-settings@npm:4.0.4"
dependencies:
lodash: "npm:^4.17.21"
mkdirp: "npm:^1.0.4"
write-file-atomic: "npm:^3.0.3"
peerDependencies:
electron: ">= 2"
checksum: 10c0/e66d47e04fa4d644ac85de810f8d0b88666954b6333bace9fb0f6484fbc200849e946d3a57cc294738797e6275c2602be7e48ddfcf0449a513781a06cfcabd22
languageName: node
linkType: hard

"emoji-regex@npm:^8.0.0":
version: 8.0.0
resolution: "emoji-regex@npm:8.0.0"
Expand All @@ -1361,8 +1348,8 @@ __metadata:
concurrently: "npm:^9.1.0"
electron-builder: "npm:^25.0.0"
electron-nightly: "npm:^35.0.0-nightly"
electron-settings: "npm:^4.0.4"
hls.js: "npm:^1.6.0-beta.1"
write-file-atomic: "npm:^6.0.0"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -2076,13 +2063,6 @@ __metadata:
languageName: node
linkType: hard

"is-typedarray@npm:^1.0.0":
version: 1.0.0
resolution: "is-typedarray@npm:1.0.0"
checksum: 10c0/4c096275ba041a17a13cca33ac21c16bc4fd2d7d7eb94525e7cd2c2f2c1a3ab956e37622290642501ff4310601e413b675cf399ad6db49855527d2163b3eeeec
languageName: node
linkType: hard

"is-unicode-supported@npm:^0.1.0":
version: 0.1.0
resolution: "is-unicode-supported@npm:0.1.0"
Expand Down Expand Up @@ -3473,15 +3453,6 @@ __metadata:
languageName: node
linkType: hard

"typedarray-to-buffer@npm:^3.1.5":
version: 3.1.5
resolution: "typedarray-to-buffer@npm:3.1.5"
dependencies:
is-typedarray: "npm:^1.0.0"
checksum: 10c0/4ac5b7a93d604edabf3ac58d3a2f7e07487e9f6e98195a080e81dbffdc4127817f470f219d794a843b87052cedef102b53ac9b539855380b8c2172054b7d5027
languageName: node
linkType: hard

"typescript@npm:^5.4.3":
version: 5.7.2
resolution: "typescript@npm:5.7.2"
Expand Down Expand Up @@ -3691,15 +3662,13 @@ __metadata:
languageName: node
linkType: hard

"write-file-atomic@npm:^3.0.3":
version: 3.0.3
resolution: "write-file-atomic@npm:3.0.3"
"write-file-atomic@npm:^6.0.0":
version: 6.0.0
resolution: "write-file-atomic@npm:6.0.0"
dependencies:
imurmurhash: "npm:^0.1.4"
is-typedarray: "npm:^1.0.0"
signal-exit: "npm:^3.0.2"
typedarray-to-buffer: "npm:^3.1.5"
checksum: 10c0/7fb67affd811c7a1221bed0c905c26e28f0041e138fb19ccf02db57a0ef93ea69220959af3906b920f9b0411d1914474cdd90b93a96e5cd9e8368d9777caac0e
signal-exit: "npm:^4.0.1"
checksum: 10c0/ae2f1c27474758a9aca92037df6c1dd9cb94c4e4983451210bd686bfe341f142662f6aa5913095e572ab037df66b1bfe661ed4ce4c0369ed0e8219e28e141786
languageName: node
linkType: hard

Expand Down

0 comments on commit a0cac2b

Please sign in to comment.