mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Cascade different refactorings to eliminate global shared state from app paths
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
1 parent
908a40975e
commit
9d33fff906
336 files changed
+9105
-3307
No files matched your search
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* 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 { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||
import directoryForLensLocalStorageInjectable from "../../../common/directory-for-lens-local-storage/directory-for-lens-local-storage.injectable";
|
||||
import { createStorage } from "./create-storage";
|
||||
import readJsonFileInjectable from "../../../common/fs/read-json-file/read-json-file.injectable";
|
||||
import writeJsonFileInjectable from "../../../common/fs/write-json-file/write-json-file.injectable";
|
||||
|
||||
const createStorageInjectable = getInjectable({
|
||||
instantiate: (di) =>
|
||||
createStorage({
|
||||
readJsonFile: di.inject(readJsonFileInjectable),
|
||||
writeJsonFile: di.inject(writeJsonFileInjectable),
|
||||
|
||||
directoryForLensLocalStorage: di.inject(
|
||||
directoryForLensLocalStorageInjectable,
|
||||
),
|
||||
}),
|
||||
|
||||
lifecycle: lifecycleEnum.singleton,
|
||||
});
|
||||
|
||||
export default createStorageInjectable;
|
||||
+17
-12
@@ -23,10 +23,12 @@
|
||||
// Because app creates random port between restarts => storage session wiped out each time.
|
||||
import path from "path";
|
||||
import { comparer, observable, reaction, toJS, when } from "mobx";
|
||||
import fse from "fs-extra";
|
||||
import { StorageHelper } from "./storageHelper";
|
||||
import logger from "../../main/logger";
|
||||
import { isTestEnv } from "../../common/vars";
|
||||
import { StorageHelper } from "../storageHelper";
|
||||
import logger from "../../../main/logger";
|
||||
import { isTestEnv } from "../../../common/vars";
|
||||
|
||||
import { getHostedClusterId } from "../../../common/utils";
|
||||
import type { JsonObject } from "type-fest";
|
||||
|
||||
const storage = observable({
|
||||
initialized: false,
|
||||
@@ -34,22 +36,26 @@ const storage = observable({
|
||||
data: {} as Record<string/*key*/, any>, // json-serializable
|
||||
});
|
||||
|
||||
interface Dependencies {
|
||||
directoryForLensLocalStorage: string;
|
||||
readJsonFile: (filePath: string) => Promise<JsonObject>;
|
||||
writeJsonFile: (filePath: string, contentObject: JsonObject) => Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a helper for saving data under the "key" intended for window.localStorage
|
||||
* @param key The descriptor of the data
|
||||
* @param defaultValue The default value of the data, must be JSON serializable
|
||||
*/
|
||||
export function createStorage<T>(key: string, defaultValue: T) {
|
||||
export const createStorage = ({ directoryForLensLocalStorage, readJsonFile, writeJsonFile }: Dependencies) => <T>(key: string, defaultValue: T) => {
|
||||
const { logPrefix } = StorageHelper;
|
||||
|
||||
if (!storage.initialized) {
|
||||
storage.initialized = true;
|
||||
|
||||
(async () => {
|
||||
const filePath = await StorageHelper.getLocalStoragePath();
|
||||
const filePath = path.resolve(directoryForLensLocalStorage, `${getHostedClusterId() || "app"}.json`);
|
||||
|
||||
try {
|
||||
storage.data = await fse.readJson(filePath);
|
||||
storage.data = await readJsonFile(filePath);
|
||||
} catch {} finally {
|
||||
if (!isTestEnv) {
|
||||
logger.info(`${logPrefix} loading finished for ${filePath}`);
|
||||
@@ -68,8 +74,7 @@ export function createStorage<T>(key: string, defaultValue: T) {
|
||||
logger.info(`${logPrefix} saving ${filePath}`);
|
||||
|
||||
try {
|
||||
await fse.ensureDir(path.dirname(filePath), { mode: 0o755 });
|
||||
await fse.writeJson(filePath, state, { spaces: 2 });
|
||||
await writeJsonFile(filePath, state);
|
||||
} catch (error) {
|
||||
logger.error(`${logPrefix} saving failed: ${error}`, {
|
||||
json: state, jsonFilePath: filePath,
|
||||
@@ -97,4 +102,4 @@ export function createStorage<T>(key: string, defaultValue: T) {
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -20,11 +20,9 @@
|
||||
*/
|
||||
|
||||
// Common usage utils & helpers
|
||||
|
||||
export * from "../../common/utils";
|
||||
export * from "../../common/event-emitter";
|
||||
|
||||
export * from "./createStorage";
|
||||
export * from "./cssNames";
|
||||
export * from "./cssVar";
|
||||
export * from "./display-booleans";
|
||||
|
||||
@@ -24,9 +24,6 @@ import { action, comparer, makeObservable, observable, toJS, when } from "mobx";
|
||||
import produce, { Draft, isDraft } from "immer";
|
||||
import { isEqual, isPlainObject } from "lodash";
|
||||
import logger from "../../main/logger";
|
||||
import { getHostedClusterId } from "../../common/utils";
|
||||
import path from "path";
|
||||
import { AppPaths } from "../../common/app-paths";
|
||||
|
||||
export interface StorageAdapter<T> {
|
||||
[metadata: string]: any;
|
||||
@@ -43,10 +40,6 @@ export interface StorageHelperOptions<T> {
|
||||
}
|
||||
|
||||
export class StorageHelper<T> {
|
||||
static async getLocalStoragePath() {
|
||||
return path.resolve(await AppPaths.getAsync("userData"), "lens-local-storage", `${getHostedClusterId() || "app"}.json`);
|
||||
}
|
||||
|
||||
static logPrefix = "[StorageHelper]:";
|
||||
readonly storage: StorageAdapter<T>;
|
||||
|
||||
|
||||
Reference in new issue
Block a user