1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Extract instantiation of "conf" as injectables for causing side effects

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2022-03-22 10:50:10 +02:00 committed by Janne Savolainen
parent 1bf61b6870
commit d8fdd12a3c
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
3 changed files with 78 additions and 18 deletions

View File

@ -4,7 +4,7 @@
*/
import path from "path";
import Config from "conf";
import type Config from "conf";
import type { Options as ConfOptions } from "conf/dist/source/types";
import { ipcMain, ipcRenderer } from "electron";
import { IEqualsComparer, makeObservable, reaction, runInAction } from "mobx";
@ -15,8 +15,8 @@ import isEqual from "lodash/isEqual";
import { isTestEnv } from "./vars";
import { kebabCase } from "lodash";
import { getLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
import directoryForUserDataInjectable
from "./app-paths/directory-for-user-data/directory-for-user-data.injectable";
import directoryForUserDataInjectable from "./app-paths/directory-for-user-data/directory-for-user-data.injectable";
import getConfigurationFileModelInjectable from "./get-configuration-file-model/get-configuration-file-model.injectable";
export interface BaseStoreParams<T> extends ConfOptions<T> {
syncOptions?: {
@ -48,10 +48,17 @@ export abstract class BaseStore<T> extends Singleton {
*/
load() {
if (!isTestEnv) {
logger.info(`[${kebabCase(this.displayName).toUpperCase()}]: LOADING from ${this.path} ...`);
logger.info(
`[${kebabCase(this.displayName).toUpperCase()}]: LOADING from ${
this.path
} ...`,
);
}
const di = getLegacyGlobalDiForExtensionApi();
this.storeConfig = new Config({
const getConfigurationFileModel = di.inject(getConfigurationFileModelInjectable);
this.storeConfig = getConfigurationFileModel({
...this.params,
projectName: "lens",
projectVersion: getAppVersion(),
@ -60,14 +67,23 @@ export abstract class BaseStore<T> extends Singleton {
const res: any = this.fromStore(this.storeConfig.store);
if (res instanceof Promise || (typeof res === "object" && res && typeof res.then === "function")) {
console.error(`${this.displayName} extends BaseStore<T>'s fromStore method returns a Promise or promise-like object. This is an error and must be fixed.`);
if (
res instanceof Promise ||
(typeof res === "object" && res && typeof res.then === "function")
) {
console.error(
`${this.displayName} extends BaseStore<T>'s fromStore method returns a Promise or promise-like object. This is an error and must be fixed.`,
);
}
this.enableSync();
if (!isTestEnv) {
logger.info(`[${kebabCase(this.displayName).toUpperCase()}]: LOADED from ${this.path}`);
logger.info(
`[${kebabCase(this.displayName).toUpperCase()}]: LOADED from ${
this.path
}`,
);
}
}
@ -108,23 +124,27 @@ export abstract class BaseStore<T> extends Singleton {
this.syncDisposers.push(
reaction(
() => toJS(this.toJSON()), // unwrap possible observables and react to everything
model => this.onModelChange(model),
(model) => this.onModelChange(model),
this.params.syncOptions,
),
);
if (ipcMain) {
this.syncDisposers.push(ipcMainOn(this.syncMainChannel, (event, model: T) => {
logger.silly(`[STORE]: SYNC ${this.name} from renderer`, { model });
this.onSync(model);
}));
this.syncDisposers.push(
ipcMainOn(this.syncMainChannel, (event, model: T) => {
logger.silly(`[STORE]: SYNC ${this.name} from renderer`, { model });
this.onSync(model);
}),
);
}
if (ipcRenderer) {
this.syncDisposers.push(ipcRendererOn(this.syncRendererChannel, (event, model: T) => {
logger.silly(`[STORE]: SYNC ${this.name} from main`, { model });
this.onSyncFromMain(model);
}));
this.syncDisposers.push(
ipcRendererOn(this.syncRendererChannel, (event, model: T) => {
logger.silly(`[STORE]: SYNC ${this.name} from main`, { model });
this.onSyncFromMain(model);
}),
);
}
}
@ -140,7 +160,7 @@ export abstract class BaseStore<T> extends Singleton {
}
disableSync() {
this.syncDisposers.forEach(dispose => dispose());
this.syncDisposers.forEach((dispose) => dispose());
this.syncDisposers.length = 0;
}

View File

@ -0,0 +1,14 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import packageInfo from "../../../../package.json";
const appVersionInjectable = getInjectable({
id: "app-version",
instantiate: () => packageInfo.version,
causesSideEffects: true,
});
export default appVersionInjectable;

View File

@ -0,0 +1,26 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import Config from "conf";
import type { BaseStoreParams } from "../base-store";
import appVersionInjectable from "./app-version/app-version.injectable";
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
const getConfigurationFileModelInjectable = getInjectable({
id: "get-configuration-file-model",
instantiate:
(di) =>
<ConfigurationContent>(content: BaseStoreParams<ConfigurationContent>) =>
new Config({
...content,
projectName: "lens",
projectVersion: di.inject(appVersionInjectable),
cwd: di.inject(directoryForUserDataInjectable),
}),
causesSideEffects: true,
});
export default getConfigurationFileModelInjectable;