mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Ensure that init can only be called once and catch errors
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
cdb129ea1e
commit
9a17097543
@ -44,40 +44,40 @@ export function createStorage<T>(key: string, defaultValue: T) {
|
|||||||
|
|
||||||
if (!storage.initialized) {
|
if (!storage.initialized) {
|
||||||
storage.initialized = true;
|
storage.initialized = true;
|
||||||
init(); // called once per cluster-view
|
|
||||||
}
|
|
||||||
|
|
||||||
async function init() {
|
(async () => {
|
||||||
const filePath = await StorageHelper.getLocalStoragePath();
|
const filePath = await StorageHelper.getLocalStoragePath();
|
||||||
|
|
||||||
try {
|
|
||||||
storage.data = await fse.readJson(filePath);
|
|
||||||
} catch {} finally {
|
|
||||||
if (!isTestEnv) {
|
|
||||||
logger.info(`${logPrefix} loading finished for ${filePath}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
storage.loaded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// bind auto-saving data changes to %storage-file.json
|
|
||||||
reaction(() => toJS(storage.data), saveFile, {
|
|
||||||
delay: 250, // lazy, avoid excessive writes to fs
|
|
||||||
equals: comparer.structural, // save only when something really changed
|
|
||||||
});
|
|
||||||
|
|
||||||
async function saveFile(state: Record<string, any> = {}) {
|
|
||||||
logger.info(`${logPrefix} saving ${filePath}`);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fse.ensureDir(path.dirname(filePath), { mode: 0o755 });
|
storage.data = await fse.readJson(filePath);
|
||||||
await fse.writeJson(filePath, state, { spaces: 2 });
|
} catch {} finally {
|
||||||
} catch (error) {
|
if (!isTestEnv) {
|
||||||
logger.error(`${logPrefix} saving failed: ${error}`, {
|
logger.info(`${logPrefix} loading finished for ${filePath}`);
|
||||||
json: state, jsonFilePath: filePath
|
}
|
||||||
});
|
|
||||||
|
storage.loaded = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// bind auto-saving data changes to %storage-file.json
|
||||||
|
reaction(() => toJS(storage.data), saveFile, {
|
||||||
|
delay: 250, // lazy, avoid excessive writes to fs
|
||||||
|
equals: comparer.structural, // save only when something really changed
|
||||||
|
});
|
||||||
|
|
||||||
|
async function saveFile(state: Record<string, any> = {}) {
|
||||||
|
logger.info(`${logPrefix} saving ${filePath}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fse.ensureDir(path.dirname(filePath), { mode: 0o755 });
|
||||||
|
await fse.writeJson(filePath, state, { spaces: 2 });
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`${logPrefix} saving failed: ${error}`, {
|
||||||
|
json: state, jsonFilePath: filePath
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
.catch(error => logger.error(`${logPrefix} Failed to initialize storage: ${error}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StorageHelper<T>(key, {
|
return new StorageHelper<T>(key, {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user