attempt to reduce fs conflicts (storage)

needs more work... interference b/w processes corrupting file and resetting storage?
This commit is contained in:
dragonwocky 2021-12-10 23:40:05 +11:00
parent 3e927e75b8
commit 9bf68ca679
Signed by: dragonwocky
GPG Key ID: 86DFC3C312A56010
5 changed files with 45 additions and 39 deletions

View File

@ -42,6 +42,7 @@ a complete redesign & rewrite of the enhancer, with new features and a port to t
- cli can now detect and apply to user-only installations on macOS. - cli can now detect and apply to user-only installations on macOS.
- moved the tray to its own configurable and enable/disable-able mod, with window management enhancements - moved the tray to its own configurable and enable/disable-able mod, with window management enhancements
that follow more sensible defaults and work more reliably. that follow more sensible defaults and work more reliably.
- tabs will auto shrink/expand to take up available space instead of wrapping to a second line.
#### removed #### removed

@ -1 +1 @@
Subproject commit 4091fcba3fbbe7bb28daa458824107cb4790e8af Subproject commit b98db17e400ec8ba1cd41185a3e9995fedd41c48

View File

@ -10,20 +10,31 @@ const os = require('os'),
path = require('path'), path = require('path'),
fs = require('fs'), fs = require('fs'),
_cacheFile = path.resolve(`${os.homedir()}/.notion-enhancer`), _cacheFile = path.resolve(`${os.homedir()}/.notion-enhancer`),
_fsQueue = [], _fsQueue = new Set(),
_onDbChangeListeners = []; _onDbChangeListeners = [];
// handle leftover cache from prev versions // handle leftover cache from prev versions
if (fs.existsSync(_cacheFile) && fs.lstatSync(_cacheFile).isDirectory()) { if (fs.existsSync(_cacheFile) && fs.lstatSync(_cacheFile).isDirectory()) {
fs.rmdirSync(_cacheFile); fs.rmdirSync(_cacheFile);
} }
if (!fs.existsSync(_cacheFile)) fs.writeFileSync(_cacheFile, '{}', 'utf8');
const isRenderer = process && process.type === 'renderer'; const isRenderer = process && process.type === 'renderer';
const getData = () => { const getCache = async () => {
try { try {
return JSON.parse(fs.readFileSync(_cacheFile)); return fs.readFileSync(_cacheFile);
} catch (err) {
await new Promise((res, rej) => setTimeout(res, 50));
return getCache();
}
},
getData = async () => {
if (!fs.existsSync(_cacheFile)) {
fs.writeFileSync(_cacheFile, '{}', 'utf8');
return {};
}
try {
return JSON.parse(await getCache());
} catch (err) { } catch (err) {
return {}; return {};
} }
@ -31,9 +42,10 @@ const getData = () => {
saveData = (data) => fs.writeFileSync(_cacheFile, JSON.stringify(data)); saveData = (data) => fs.writeFileSync(_cacheFile, JSON.stringify(data));
const db = { const db = {
get: (path, fallback = undefined) => { get: async (path, fallback = undefined) => {
if (!path.length) return fallback; if (!path.length) return fallback;
const values = getData(); while (_fsQueue.size) await new Promise(requestIdleCallback);
const values = await getData();
let value = values; let value = values;
while (path.length) { while (path.length) {
if (value === undefined) { if (value === undefined) {
@ -42,38 +54,31 @@ const db = {
} }
value = value[path.shift()]; value = value[path.shift()];
} }
return Promise.resolve(value ?? fallback); return value ?? fallback;
}, },
set: (path, value) => { set: async (path, value) => {
if (!path.length) return undefined; if (!path.length) return undefined;
const precursor = _fsQueue[_fsQueue.length - 1] || undefined, while (_fsQueue.size) await new Promise(requestIdleCallback);
interaction = new Promise(async (res, rej) => { const op = Symbol();
if (precursor !== undefined) { _fsQueue.add(op);
await precursor; const pathClone = [...path],
_fsQueue.shift(); values = await getData();
} let pointer = values,
const pathClone = [...path], old;
values = getData(); while (path.length) {
let pointer = values, const key = path.shift();
old; if (!path.length) {
while (path.length) { old = pointer[key];
const key = path.shift(); pointer[key] = value;
if (!path.length) { break;
old = pointer[key]; }
pointer[key] = value; pointer[key] = pointer[key] ?? {};
break; pointer = pointer[key];
} }
pointer[key] = pointer[key] ?? {}; saveData(values);
pointer = pointer[key]; _onDbChangeListeners.forEach((listener) => listener({ path: pathClone, new: value, old }));
} _fsQueue.delete(op);
saveData(values); return value;
_onDbChangeListeners.forEach((listener) =>
listener({ path: pathClone, new: value, old })
);
res(value);
});
_fsQueue.push(interaction);
return interaction;
}, },
addChangeListener: (callback) => { addChangeListener: (callback) => {
_onDbChangeListeners.push(callback); _onDbChangeListeners.push(callback);

@ -1 +1 @@
Subproject commit 4cd33f48a01dadaf493987296995a01600c926bf Subproject commit 94e069426beeb7e90cd7a5df55b6499353028163

View File

@ -81,7 +81,7 @@ module.exports.focusNotion = () => {
module.exports.reload = () => { module.exports.reload = () => {
const { app } = require('electron'); const { app } = require('electron');
app.relaunch({ args: process.argv.slice(1).filter((arg) => arg !== '--startup') }); app.relaunch({ args: process.argv.slice(1).filter((arg) => arg !== '--startup') });
app.exit(0); app.quit();
}; };
module.exports.listen = () => { module.exports.listen = () => {