mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
revert createStorage changes
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
871734e44f
commit
e746a7cadc
@ -27,7 +27,7 @@ import fse from "fs-extra";
|
|||||||
import { StorageHelper } from "./storageHelper";
|
import { StorageHelper } from "./storageHelper";
|
||||||
import { ClusterStore } from "../../common/cluster-store";
|
import { ClusterStore } from "../../common/cluster-store";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
import { getHostedClusterId, getPath, noop } from "../../common/utils";
|
import { getHostedClusterId, getPath } from "../../common/utils";
|
||||||
|
|
||||||
const storage = observable({
|
const storage = observable({
|
||||||
initialized: false,
|
initialized: false,
|
||||||
@ -51,6 +51,10 @@ export function createAppStorage<T>(key: string, defaultValue: T, clusterId?: st
|
|||||||
const filePath = path.resolve(folder, fileName);
|
const filePath = path.resolve(folder, fileName);
|
||||||
|
|
||||||
if (!storage.initialized) {
|
if (!storage.initialized) {
|
||||||
|
init(); // called once per cluster-view
|
||||||
|
}
|
||||||
|
|
||||||
|
function init() {
|
||||||
storage.initialized = true;
|
storage.initialized = true;
|
||||||
|
|
||||||
// read previously saved state (if any)
|
// read previously saved state (if any)
|
||||||
@ -63,35 +67,32 @@ export function createAppStorage<T>(key: string, defaultValue: T, clusterId?: st
|
|||||||
});
|
});
|
||||||
|
|
||||||
// bind auto-saving data changes to %storage-file.json
|
// bind auto-saving data changes to %storage-file.json
|
||||||
reaction(
|
reaction(() => toJS(storage.data), saveFile, {
|
||||||
() => toJS(storage.data),
|
delay: 250, // lazy, avoid excessive writes to fs
|
||||||
async state => {
|
equals: comparer.structural, // save only when something really changed
|
||||||
logger.info(`${logPrefix} saving ${filePath}`);
|
});
|
||||||
|
|
||||||
try {
|
|
||||||
await fse.ensureDir(folder, { mode: 0o755 });
|
|
||||||
await fse.writeJson(filePath, state, { spaces: 2 });
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(`${logPrefix} saving failed: ${error}`, {
|
|
||||||
json: state, jsonFilePath: filePath
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
delay: 250, // lazy, avoid excessive writes to fs
|
|
||||||
equals: comparer.structural, // save only when something really changed
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// remove json-file when cluster deleted
|
// remove json-file when cluster deleted
|
||||||
if (clusterId !== undefined) {
|
if (clusterId !== undefined) {
|
||||||
when(
|
when(() => ClusterStore.getInstance(false)?.removedClusters.has(clusterId)).then(removeFile);
|
||||||
() => ClusterStore.getInstance(false)?.removedClusters.has(clusterId),
|
}
|
||||||
() => {
|
|
||||||
logger.debug(`${logPrefix} removing ${filePath}`);
|
async function saveFile(state: Record<string, any> = {}) {
|
||||||
fse.unlink(filePath).catch(noop);
|
logger.info(`${logPrefix} saving ${filePath}`);
|
||||||
}
|
|
||||||
);
|
try {
|
||||||
|
await fse.ensureDir(folder, { mode: 0o755 });
|
||||||
|
await fse.writeJson(filePath, state, { spaces: 2 });
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`${logPrefix} saving failed: ${error}`, {
|
||||||
|
json: state, jsonFilePath: filePath
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeFile() {
|
||||||
|
logger.debug(`${logPrefix} removing ${filePath}`);
|
||||||
|
fse.unlink(filePath).catch(Function);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user