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.
- 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.
- tabs will auto shrink/expand to take up available space instead of wrapping to a second line.
#### removed

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

View File

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

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

View File

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