mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fully injectable-ize BaseStore so that ApplicationBuilder tests work
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
cf0ca414ea
commit
01d3914b5a
@ -1,154 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import mockFs from "mock-fs";
|
||||
import type { BaseStoreDependencies } from "../base-store/base-store";
|
||||
import { BaseStore } from "../base-store/base-store";
|
||||
import { action, comparer, makeObservable, observable, toJS } from "mobx";
|
||||
import { readFileSync } from "fs";
|
||||
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
|
||||
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
|
||||
import getConfigurationFileModelInjectable from "../get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import loggerInjectable from "../logger.injectable";
|
||||
import storeMigrationVersionInjectable from "../vars/store-migration-version.injectable";
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
ipcMain: {
|
||||
on: jest.fn(),
|
||||
off: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
interface TestStoreModel {
|
||||
a: string;
|
||||
b: string;
|
||||
c: string;
|
||||
}
|
||||
|
||||
class TestStore extends BaseStore<TestStoreModel> {
|
||||
@observable a = "";
|
||||
@observable b = "";
|
||||
@observable c = "";
|
||||
|
||||
constructor(deps: BaseStoreDependencies) {
|
||||
super(deps, {
|
||||
configName: "test-store",
|
||||
accessPropertiesByDotNotation: false, // To make dots safe in cluster context names
|
||||
syncOptions: {
|
||||
equals: comparer.structural,
|
||||
},
|
||||
});
|
||||
|
||||
makeObservable(this);
|
||||
this.load();
|
||||
}
|
||||
|
||||
@action updateAll(data: TestStoreModel) {
|
||||
this.a = data.a;
|
||||
this.b = data.b;
|
||||
this.c = data.c;
|
||||
}
|
||||
|
||||
@action fromStore(data: Partial<TestStoreModel> = {}) {
|
||||
this.a = data.a || "";
|
||||
this.b = data.b || "";
|
||||
this.c = data.c || "";
|
||||
}
|
||||
|
||||
onSync(data: TestStoreModel) {
|
||||
super.onSync(data);
|
||||
}
|
||||
|
||||
async saveToFile(model: TestStoreModel) {
|
||||
return super.saveToFile(model);
|
||||
}
|
||||
|
||||
toJSON(): TestStoreModel {
|
||||
const data: TestStoreModel = {
|
||||
a: this.a,
|
||||
b: this.b,
|
||||
c: this.c,
|
||||
};
|
||||
|
||||
return toJS(data);
|
||||
}
|
||||
}
|
||||
|
||||
describe("BaseStore", () => {
|
||||
let store: TestStore;
|
||||
|
||||
beforeEach(() => {
|
||||
const mainDi = getDiForUnitTesting({ doGeneralOverrides: true });
|
||||
|
||||
mainDi.override(directoryForUserDataInjectable, () => "some-user-data-directory");
|
||||
mainDi.permitSideEffects(getConfigurationFileModelInjectable);
|
||||
|
||||
const mockOpts = {
|
||||
"some-user-data-directory": {
|
||||
"test-store.json": JSON.stringify({}),
|
||||
},
|
||||
};
|
||||
|
||||
mockFs(mockOpts);
|
||||
|
||||
store = new TestStore({
|
||||
directoryForUserData: mainDi.inject(directoryForUserDataInjectable),
|
||||
getConfigurationFileModel: mainDi.inject(getConfigurationFileModelInjectable),
|
||||
logger: mainDi.inject(loggerInjectable),
|
||||
storeMigrationVersion: mainDi.inject(storeMigrationVersionInjectable),
|
||||
migrations: {},
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mockFs.restore();
|
||||
store.disableSync();
|
||||
});
|
||||
|
||||
describe("persistence", () => {
|
||||
it("persists changes to the filesystem", () => {
|
||||
store.updateAll({
|
||||
a: "foo", b: "bar", c: "hello",
|
||||
});
|
||||
|
||||
const data = JSON.parse(readFileSync("some-user-data-directory/test-store.json").toString());
|
||||
|
||||
expect(data).toMatchObject({ a: "foo", b: "bar", c: "hello" });
|
||||
});
|
||||
|
||||
it("persists transaction only once", () => {
|
||||
const fileSpy = jest.spyOn(store, "saveToFile");
|
||||
|
||||
store.updateAll({
|
||||
a: "foo", b: "bar", c: "hello",
|
||||
});
|
||||
|
||||
expect(fileSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("persists changes one-by-one without transaction", () => {
|
||||
const fileSpy = jest.spyOn(store, "saveToFile");
|
||||
|
||||
store.a = "a";
|
||||
store.b = "b";
|
||||
|
||||
expect(fileSpy).toHaveBeenCalledTimes(2);
|
||||
|
||||
const data = JSON.parse(readFileSync("some-user-data-directory/test-store.json").toString());
|
||||
|
||||
expect(data).toMatchObject({ a: "a", b: "b", c: "" });
|
||||
});
|
||||
|
||||
it("persists changes coming via onSync (sync from different process)", () => {
|
||||
const fileSpy = jest.spyOn(store, "saveToFile");
|
||||
|
||||
store.onSync({ a: "foo", b: "", c: "bar" });
|
||||
|
||||
expect(store.toJSON()).toMatchObject({ a: "foo", b: "", c: "bar" });
|
||||
|
||||
expect(fileSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -122,8 +122,6 @@ describe("cluster-store", () => {
|
||||
createCluster = mainDi.inject(createClusterInjectionToken);
|
||||
|
||||
clusterStore = mainDi.inject(clusterStoreInjectable);
|
||||
|
||||
clusterStore.unregisterIpcListener();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
@ -3,19 +3,19 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import path from "path";
|
||||
import type Config from "conf";
|
||||
import type { Migrations, Options as ConfOptions } from "conf/dist/source/types";
|
||||
import { ipcMain, ipcRenderer } from "electron";
|
||||
import type { IEqualsComparer } from "mobx";
|
||||
import { makeObservable, reaction, runInAction } from "mobx";
|
||||
import type { Disposer } from "../utils";
|
||||
import { isPromiseLike, toJS } from "../utils";
|
||||
import { broadcastMessage, ipcMainOn, ipcRendererOn } from "../ipc";
|
||||
import { makeObservable, reaction } from "mobx";
|
||||
import { disposer, isPromiseLike, toJS } from "../utils";
|
||||
import { broadcastMessage } from "../ipc";
|
||||
import isEqual from "lodash/isEqual";
|
||||
import { kebabCase } from "lodash";
|
||||
import type { GetConfigurationFileModel } from "../get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import type { Logger } from "../logger";
|
||||
import type { PersistStateToConfig } from "./save-to-file";
|
||||
import type { GetBasenameOfPath } from "../path/get-basename.injectable";
|
||||
import type { EnlistMessageChannelListener } from "../utils/channel/enlist-message-channel-listener-injection-token";
|
||||
|
||||
export interface BaseStoreParams<T> extends Omit<ConfOptions<T>, "migrations"> {
|
||||
syncOptions?: {
|
||||
@ -30,15 +30,19 @@ export interface BaseStoreDependencies {
|
||||
readonly storeMigrationVersion: string;
|
||||
readonly directoryForUserData: string;
|
||||
readonly migrations: Migrations<Record<string, unknown>>;
|
||||
readonly ipcChannelPrefix: string;
|
||||
readonly shouldDisableSyncInListener: boolean;
|
||||
getConfigurationFileModel: GetConfigurationFileModel;
|
||||
persistStateToConfig: PersistStateToConfig;
|
||||
getBasenameOfPath: GetBasenameOfPath;
|
||||
enlistMessageChannelListener: EnlistMessageChannelListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note: T should only contain base JSON serializable types.
|
||||
*/
|
||||
export abstract class BaseStore<T extends object> {
|
||||
protected storeConfig?: Config<T>;
|
||||
protected syncDisposers: Disposer[] = [];
|
||||
private readonly syncDisposers = disposer();
|
||||
|
||||
readonly displayName = kebabCase(this.params.configName).toUpperCase();
|
||||
|
||||
@ -54,7 +58,8 @@ export abstract class BaseStore<T extends object> {
|
||||
*/
|
||||
load() {
|
||||
this.dependencies.logger.info(`[${this.displayName}]: LOADING ...`);
|
||||
this.storeConfig = this.dependencies.getConfigurationFileModel({
|
||||
|
||||
const config = this.dependencies.getConfigurationFileModel({
|
||||
projectName: "lens",
|
||||
projectVersion: this.dependencies.storeMigrationVersion,
|
||||
cwd: this.cwd(),
|
||||
@ -62,107 +67,60 @@ export abstract class BaseStore<T extends object> {
|
||||
migrations: this.dependencies.migrations as Migrations<T>,
|
||||
});
|
||||
|
||||
const res = this.fromStore(this.storeConfig.store);
|
||||
const res = this.fromStore(config.store);
|
||||
|
||||
if (isPromiseLike(res)) {
|
||||
this.dependencies.logger.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();
|
||||
this.dependencies.logger.info(`[${this.displayName}]: LOADED from ${this.path}`);
|
||||
}
|
||||
|
||||
get name() {
|
||||
return path.basename(this.path);
|
||||
}
|
||||
|
||||
protected get syncRendererChannel() {
|
||||
return `store-sync-renderer:${this.path}`;
|
||||
}
|
||||
|
||||
protected get syncMainChannel() {
|
||||
return `store-sync-main:${this.path}`;
|
||||
}
|
||||
|
||||
get path() {
|
||||
return this.storeConfig?.path || "";
|
||||
this.startSyncing(config);
|
||||
this.dependencies.logger.info(`[${this.displayName}]: LOADED from ${config.path}`);
|
||||
}
|
||||
|
||||
protected cwd() {
|
||||
return this.dependencies.directoryForUserData;
|
||||
}
|
||||
|
||||
protected saveToFile(model: T) {
|
||||
this.dependencies.logger.info(`[${this.displayName}]: SAVING ${this.path}`);
|
||||
private startSyncing(config: Config<T>) {
|
||||
const name = this.dependencies.getBasenameOfPath(config.path);
|
||||
const channelName = `${this.dependencies.ipcChannelPrefix}:${config.path}`;
|
||||
|
||||
// todo: update when fixed https://github.com/sindresorhus/conf/issues/114
|
||||
if (this.storeConfig) {
|
||||
for (const [key, value] of Object.entries(model)) {
|
||||
this.storeConfig.set(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
const disableSync = () => this.syncDisposers();
|
||||
const enableSync = () => {
|
||||
this.syncDisposers.push(
|
||||
reaction(
|
||||
() => toJS(this.toJSON()), // unwrap possible observables and react to everything
|
||||
model => {
|
||||
this.dependencies.persistStateToConfig(config, model);
|
||||
broadcastMessage(channelName, model);
|
||||
},
|
||||
this.params.syncOptions,
|
||||
),
|
||||
this.dependencies.enlistMessageChannelListener({
|
||||
channel: {
|
||||
id: channelName,
|
||||
},
|
||||
handler: (model) => {
|
||||
this.dependencies.logger.silly(`[${this.displayName}]: syncing ${name}`, { model });
|
||||
|
||||
enableSync() {
|
||||
this.syncDisposers.push(
|
||||
reaction(
|
||||
() => toJS(this.toJSON()), // unwrap possible observables and react to everything
|
||||
model => this.onModelChange(model),
|
||||
this.params.syncOptions,
|
||||
),
|
||||
);
|
||||
if (this.dependencies.shouldDisableSyncInListener) {
|
||||
disableSync();
|
||||
}
|
||||
|
||||
if (ipcMain) {
|
||||
this.syncDisposers.push(ipcMainOn(this.syncMainChannel, (event, model: T) => {
|
||||
this.dependencies.logger.silly(`[${this.displayName}]: SYNC ${this.name} from renderer`, { model });
|
||||
this.onSync(model);
|
||||
}));
|
||||
}
|
||||
// todo: use "resourceVersion" if merge required (to avoid equality checks => better performance)
|
||||
if (!isEqual(this.toJSON(), model)) {
|
||||
this.fromStore(model as T);
|
||||
}
|
||||
|
||||
if (ipcRenderer) {
|
||||
this.syncDisposers.push(ipcRendererOn(this.syncRendererChannel, (event, model: T) => {
|
||||
this.dependencies.logger.silly(`[${this.displayName}]: SYNC ${this.name} from main`, { model });
|
||||
this.onSyncFromMain(model);
|
||||
}));
|
||||
}
|
||||
}
|
||||
if (this.dependencies.shouldDisableSyncInListener) {
|
||||
enableSync();
|
||||
}
|
||||
},
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
protected onSyncFromMain(model: T) {
|
||||
this.applyWithoutSync(() => {
|
||||
this.onSync(model);
|
||||
});
|
||||
}
|
||||
|
||||
unregisterIpcListener() {
|
||||
ipcRenderer?.removeAllListeners(this.syncMainChannel);
|
||||
ipcRenderer?.removeAllListeners(this.syncRendererChannel);
|
||||
}
|
||||
|
||||
disableSync() {
|
||||
this.syncDisposers.forEach(dispose => dispose());
|
||||
this.syncDisposers.length = 0;
|
||||
}
|
||||
|
||||
protected applyWithoutSync(callback: () => void) {
|
||||
this.disableSync();
|
||||
runInAction(callback);
|
||||
this.enableSync();
|
||||
}
|
||||
|
||||
protected onSync(model: T) {
|
||||
// todo: use "resourceVersion" if merge required (to avoid equality checks => better performance)
|
||||
if (!isEqual(this.toJSON(), model)) {
|
||||
this.fromStore(model);
|
||||
}
|
||||
}
|
||||
|
||||
protected onModelChange(model: T) {
|
||||
if (ipcMain) {
|
||||
this.saveToFile(model); // save config file
|
||||
broadcastMessage(this.syncRendererChannel, model);
|
||||
} else {
|
||||
broadcastMessage(this.syncMainChannel, model);
|
||||
}
|
||||
enableSync();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
10
src/common/base-store/channel-prefix.ts
Normal file
10
src/common/base-store/channel-prefix.ts
Normal file
@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
|
||||
export const baseStoreIpcChannelPrefixInjectionToken = getInjectionToken<string>({
|
||||
id: "base-store-ipc-channel-prefix-token",
|
||||
});
|
||||
10
src/common/base-store/disable-sync.ts
Normal file
10
src/common/base-store/disable-sync.ts
Normal file
@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
|
||||
export const shouldBaseStoreDisableSyncInIpcListenerInjectionToken = getInjectionToken<boolean>({
|
||||
id: "should-base-store-disable-sync-in-ipc-listener-token",
|
||||
});
|
||||
12
src/common/base-store/save-to-file.ts
Normal file
12
src/common/base-store/save-to-file.ts
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
import type Config from "conf";
|
||||
|
||||
export type PersistStateToConfig = <T extends object>(config: Config<T>, state: T) => void;
|
||||
|
||||
export const persistStateToConfigInjectionToken = getInjectionToken<PersistStateToConfig>({
|
||||
id: "persist-state-to-config-token",
|
||||
});
|
||||
@ -13,6 +13,11 @@ import loggerInjectable from "../logger.injectable";
|
||||
import storeMigrationVersionInjectable from "../vars/store-migration-version.injectable";
|
||||
import storeMigrationsInjectable from "../base-store/migrations.injectable";
|
||||
import { clusterStoreMigrationInjectionToken } from "./migration-token";
|
||||
import { baseStoreIpcChannelPrefixInjectionToken } from "../base-store/channel-prefix";
|
||||
import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../base-store/disable-sync";
|
||||
import { persistStateToConfigInjectionToken } from "../base-store/save-to-file";
|
||||
import getBasenameOfPathInjectable from "../path/get-basename.injectable";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "../utils/channel/enlist-message-channel-listener-injection-token";
|
||||
|
||||
const clusterStoreInjectable = getInjectable({
|
||||
id: "cluster-store",
|
||||
@ -26,6 +31,11 @@ const clusterStoreInjectable = getInjectable({
|
||||
logger: di.inject(loggerInjectable),
|
||||
storeMigrationVersion: di.inject(storeMigrationVersionInjectable),
|
||||
migrations: di.inject(storeMigrationsInjectable, clusterStoreMigrationInjectionToken),
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
ipcChannelPrefix: di.inject(baseStoreIpcChannelPrefixInjectionToken),
|
||||
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
|
||||
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
|
||||
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@ -70,11 +70,6 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
});
|
||||
}
|
||||
|
||||
unregisterIpcListener() {
|
||||
super.unregisterIpcListener();
|
||||
this.disposer();
|
||||
}
|
||||
|
||||
pushState() {
|
||||
this.clusters.forEach((c) => {
|
||||
c.pushState();
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
*/
|
||||
|
||||
import assert from "assert";
|
||||
import { action } from "mobx";
|
||||
import path from "path";
|
||||
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||
import getConfigurationFileModelInjectable from "./get-configuration-file-model.injectable";
|
||||
@ -21,17 +20,20 @@ export default getGlobalOverride(getConfigurationFileModelInjectable, (di) => {
|
||||
assert(options.configName, "Missing options.configName");
|
||||
|
||||
const configFilePath = path.posix.join(options.cwd, options.configName);
|
||||
let store: object = {};
|
||||
|
||||
try {
|
||||
store = readJsonSync(configFilePath);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
return {
|
||||
get store() {
|
||||
try {
|
||||
return readJsonSync(configFilePath);
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
return store;
|
||||
},
|
||||
path: configFilePath,
|
||||
set: action((key: string, value: unknown) => {
|
||||
set: (key: string, value: unknown) => {
|
||||
let currentState: object;
|
||||
|
||||
try {
|
||||
@ -44,7 +46,8 @@ export default getGlobalOverride(getConfigurationFileModelInjectable, (di) => {
|
||||
...currentState,
|
||||
[key]: value,
|
||||
});
|
||||
}),
|
||||
store = readJsonSync(configFilePath);
|
||||
},
|
||||
} as Partial<Config> as Config<any>;
|
||||
};
|
||||
});
|
||||
|
||||
@ -11,6 +11,11 @@ import getConfigurationFileModelInjectable from "../get-configuration-file-model
|
||||
import storeMigrationVersionInjectable from "../vars/store-migration-version.injectable";
|
||||
import storeMigrationsInjectable from "../base-store/migrations.injectable";
|
||||
import { hotbarStoreMigrationInjectionToken } from "./migrations-token";
|
||||
import getBasenameOfPathInjectable from "../path/get-basename.injectable";
|
||||
import { baseStoreIpcChannelPrefixInjectionToken } from "../base-store/channel-prefix";
|
||||
import { persistStateToConfigInjectionToken } from "../base-store/save-to-file";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "../utils/channel/enlist-message-channel-listener-injection-token";
|
||||
import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../base-store/disable-sync";
|
||||
|
||||
const hotbarStoreInjectable = getInjectable({
|
||||
id: "hotbar-store",
|
||||
@ -22,6 +27,11 @@ const hotbarStoreInjectable = getInjectable({
|
||||
getConfigurationFileModel: di.inject(getConfigurationFileModelInjectable),
|
||||
storeMigrationVersion: di.inject(storeMigrationVersionInjectable),
|
||||
migrations: di.inject(storeMigrationsInjectable, hotbarStoreMigrationInjectionToken),
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
ipcChannelPrefix: di.inject(baseStoreIpcChannelPrefixInjectionToken),
|
||||
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
|
||||
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
|
||||
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@ -38,7 +38,6 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
|
||||
equals: comparer.structural,
|
||||
},
|
||||
});
|
||||
|
||||
makeObservable(this);
|
||||
}
|
||||
|
||||
@ -54,6 +53,7 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
|
||||
if (typeof hotbar === "number") {
|
||||
if (hotbar >= 0 && hotbar < this.hotbars.length) {
|
||||
this._activeHotbarId = this.hotbars[hotbar].id;
|
||||
console.log("in _activeHotbarId", this._activeHotbarId);
|
||||
}
|
||||
} else if (typeof hotbar === "string") {
|
||||
if (this.findById(hotbar)) {
|
||||
@ -97,21 +97,19 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
|
||||
this.hotbars.forEach(ensureExactHotbarItemLength);
|
||||
|
||||
if (data.activeHotbarId) {
|
||||
this.setActiveHotbar(data.activeHotbarId);
|
||||
this._activeHotbarId = data.activeHotbarId;
|
||||
}
|
||||
|
||||
if (!this.activeHotbarId) {
|
||||
this.setActiveHotbar(0);
|
||||
this._activeHotbarId = this.hotbars[0].id;
|
||||
}
|
||||
}
|
||||
|
||||
toJSON(): HotbarStoreModel {
|
||||
const model: HotbarStoreModel = {
|
||||
return toJS({
|
||||
hotbars: this.hotbars,
|
||||
activeHotbarId: this.activeHotbarId,
|
||||
};
|
||||
|
||||
return toJS(model);
|
||||
});
|
||||
}
|
||||
|
||||
getActive(): Hotbar {
|
||||
|
||||
@ -12,6 +12,11 @@ import loggerInjectable from "../logger.injectable";
|
||||
import storeMigrationVersionInjectable from "../vars/store-migration-version.injectable";
|
||||
import storeMigrationsInjectable from "../base-store/migrations.injectable";
|
||||
import { userStoreMigrationInjectionToken } from "./migrations-token";
|
||||
import { baseStoreIpcChannelPrefixInjectionToken } from "../base-store/channel-prefix";
|
||||
import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../base-store/disable-sync";
|
||||
import { persistStateToConfigInjectionToken } from "../base-store/save-to-file";
|
||||
import getBasenameOfPathInjectable from "../path/get-basename.injectable";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "../utils/channel/enlist-message-channel-listener-injection-token";
|
||||
|
||||
const userStoreInjectable = getInjectable({
|
||||
id: "user-store",
|
||||
@ -24,6 +29,11 @@ const userStoreInjectable = getInjectable({
|
||||
logger: di.inject(loggerInjectable),
|
||||
storeMigrationVersion: di.inject(storeMigrationVersionInjectable),
|
||||
migrations: di.inject(storeMigrationsInjectable, userStoreMigrationInjectionToken),
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
ipcChannelPrefix: di.inject(baseStoreIpcChannelPrefixInjectionToken),
|
||||
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
|
||||
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
|
||||
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@ -4,9 +4,14 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
|
||||
import { baseStoreIpcChannelPrefixInjectionToken } from "../base-store/channel-prefix";
|
||||
import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../base-store/disable-sync";
|
||||
import storeMigrationsInjectable from "../base-store/migrations.injectable";
|
||||
import { persistStateToConfigInjectionToken } from "../base-store/save-to-file";
|
||||
import getConfigurationFileModelInjectable from "../get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import loggerInjectable from "../logger.injectable";
|
||||
import getBasenameOfPathInjectable from "../path/get-basename.injectable";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "../utils/channel/enlist-message-channel-listener-injection-token";
|
||||
import storeMigrationVersionInjectable from "../vars/store-migration-version.injectable";
|
||||
import { weblinkStoreMigrationInjectionToken } from "./migration-token";
|
||||
import { WeblinkStore } from "./weblink-store";
|
||||
@ -19,6 +24,11 @@ const weblinkStoreInjectable = getInjectable({
|
||||
logger: di.inject(loggerInjectable),
|
||||
storeMigrationVersion: di.inject(storeMigrationVersionInjectable),
|
||||
migrations: di.inject(storeMigrationsInjectable, weblinkStoreMigrationInjectionToken),
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
ipcChannelPrefix: di.inject(baseStoreIpcChannelPrefixInjectionToken),
|
||||
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
|
||||
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
|
||||
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@ -12,6 +12,11 @@ import directoryForUserDataInjectable from "../../../common/app-paths/directory-
|
||||
import getConfigurationFileModelInjectable from "../../../common/get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import loggerInjectable from "../../../common/logger.injectable";
|
||||
import storeMigrationVersionInjectable from "../../../common/vars/store-migration-version.injectable";
|
||||
import { baseStoreIpcChannelPrefixInjectionToken } from "../../../common/base-store/channel-prefix";
|
||||
import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../../../common/base-store/disable-sync";
|
||||
import { persistStateToConfigInjectionToken } from "../../../common/base-store/save-to-file";
|
||||
import getBasenameOfPathInjectable from "../../../common/path/get-basename.injectable";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "../../../common/utils/channel/enlist-message-channel-listener-injection-token";
|
||||
|
||||
const fileSystemProvisionerStoreInjectable = getInjectable({
|
||||
id: "file-system-provisioner-store",
|
||||
@ -26,6 +31,11 @@ const fileSystemProvisionerStoreInjectable = getInjectable({
|
||||
logger: di.inject(loggerInjectable),
|
||||
storeMigrationVersion: di.inject(storeMigrationVersionInjectable),
|
||||
migrations: {},
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
ipcChannelPrefix: di.inject(baseStoreIpcChannelPrefixInjectionToken),
|
||||
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
|
||||
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
|
||||
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@ -16,6 +16,11 @@ import getConfigurationFileModelInjectable from "../common/get-configuration-fil
|
||||
import loggerInjectable from "../common/logger.injectable";
|
||||
import storeMigrationVersionInjectable from "../common/vars/store-migration-version.injectable";
|
||||
import type { Migrations } from "conf/dist/source/types";
|
||||
import { baseStoreIpcChannelPrefixInjectionToken } from "../common/base-store/channel-prefix";
|
||||
import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../common/base-store/disable-sync";
|
||||
import { persistStateToConfigInjectionToken } from "../common/base-store/save-to-file";
|
||||
import getBasenameOfPathInjectable from "../common/path/get-basename.injectable";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "../common/utils/channel/enlist-message-channel-listener-injection-token";
|
||||
|
||||
export interface ExtensionStoreParams<T extends object> extends BaseStoreParams<T> {
|
||||
migrations?: Migrations<T>;
|
||||
@ -53,6 +58,11 @@ export abstract class ExtensionStore<T extends object> extends BaseStore<T> {
|
||||
logger: di.inject(loggerInjectable),
|
||||
storeMigrationVersion: di.inject(storeMigrationVersionInjectable),
|
||||
migrations: migrations as Migrations<Record<string, unknown>>,
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
ipcChannelPrefix: di.inject(baseStoreIpcChannelPrefixInjectionToken),
|
||||
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
|
||||
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
|
||||
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
|
||||
}, params);
|
||||
}
|
||||
|
||||
|
||||
@ -4,8 +4,13 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import directoryForUserDataInjectable from "../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
|
||||
import { baseStoreIpcChannelPrefixInjectionToken } from "../../common/base-store/channel-prefix";
|
||||
import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../../common/base-store/disable-sync";
|
||||
import { persistStateToConfigInjectionToken } from "../../common/base-store/save-to-file";
|
||||
import getConfigurationFileModelInjectable from "../../common/get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import loggerInjectable from "../../common/logger.injectable";
|
||||
import getBasenameOfPathInjectable from "../../common/path/get-basename.injectable";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "../../common/utils/channel/enlist-message-channel-listener-injection-token";
|
||||
import storeMigrationVersionInjectable from "../../common/vars/store-migration-version.injectable";
|
||||
import { ExtensionsStore } from "./extensions-store";
|
||||
|
||||
@ -17,6 +22,11 @@ const extensionsStoreInjectable = getInjectable({
|
||||
logger: di.inject(loggerInjectable),
|
||||
storeMigrationVersion: di.inject(storeMigrationVersionInjectable),
|
||||
migrations: {},
|
||||
getBasenameOfPath: di.inject(getBasenameOfPathInjectable),
|
||||
ipcChannelPrefix: di.inject(baseStoreIpcChannelPrefixInjectionToken),
|
||||
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
|
||||
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
|
||||
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@ -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 { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../../common/base-store/disable-sync";
|
||||
|
||||
const shouldBaseStoreDisableSyncInIpcListenerInjectable = getInjectable({
|
||||
id: "should-base-store-disable-sync-in-ipc-listener",
|
||||
instantiate: () => false,
|
||||
injectionToken: shouldBaseStoreDisableSyncInIpcListenerInjectionToken,
|
||||
});
|
||||
|
||||
export default shouldBaseStoreDisableSyncInIpcListenerInjectable;
|
||||
14
src/main/base-store/ipc-channel-prefix.injectable.ts
Normal file
14
src/main/base-store/ipc-channel-prefix.injectable.ts
Normal 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 { baseStoreIpcChannelPrefixInjectionToken } from "../../common/base-store/channel-prefix";
|
||||
|
||||
const baseStoreIpcChannelPrefixInjectable = getInjectable({
|
||||
id: "base-store-ipc-channel-prefix",
|
||||
instantiate: () => "store-sync-main",
|
||||
injectionToken: baseStoreIpcChannelPrefixInjectionToken,
|
||||
});
|
||||
|
||||
export default baseStoreIpcChannelPrefixInjectable;
|
||||
25
src/main/base-store/persist-state-to-config.injectable.ts
Normal file
25
src/main/base-store/persist-state-to-config.injectable.ts
Normal file
@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 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 { persistStateToConfigInjectionToken } from "../../common/base-store/save-to-file";
|
||||
import loggerInjectable from "../../common/logger.injectable";
|
||||
|
||||
const persistStateToConfigInjectable = getInjectable({
|
||||
id: "persist-state-to-config",
|
||||
instantiate: (di) => {
|
||||
const logger = di.inject(loggerInjectable);
|
||||
|
||||
return (config, state) => {
|
||||
logger.info(`[BASE-STORE]: saving ${config.path}...`);
|
||||
|
||||
for (const [key, value] of Object.entries(state)) {
|
||||
config.set(key, value);
|
||||
}
|
||||
};
|
||||
},
|
||||
injectionToken: persistStateToConfigInjectionToken,
|
||||
});
|
||||
|
||||
export default persistStateToConfigInjectable;
|
||||
@ -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 { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../../common/base-store/disable-sync";
|
||||
|
||||
const shouldBaseStoreDisableSyncInIpcListenerInjectable = getInjectable({
|
||||
id: "should-base-store-disable-sync-in-ipc-listener",
|
||||
instantiate: () => true,
|
||||
injectionToken: shouldBaseStoreDisableSyncInIpcListenerInjectionToken,
|
||||
});
|
||||
|
||||
export default shouldBaseStoreDisableSyncInIpcListenerInjectable;
|
||||
14
src/renderer/base-store/ipc-channel-prefix.injectable.ts
Normal file
14
src/renderer/base-store/ipc-channel-prefix.injectable.ts
Normal 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 { baseStoreIpcChannelPrefixInjectionToken } from "../../common/base-store/channel-prefix";
|
||||
|
||||
const baseStoreIpcChannelPrefixInjectable = getInjectable({
|
||||
id: "base-store-ipc-channel-prefix",
|
||||
instantiate: () => "store-sync-renderer",
|
||||
injectionToken: baseStoreIpcChannelPrefixInjectionToken,
|
||||
});
|
||||
|
||||
export default baseStoreIpcChannelPrefixInjectable;
|
||||
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* 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 { persistStateToConfigInjectionToken } from "../../common/base-store/save-to-file";
|
||||
import { noop } from "../utils";
|
||||
|
||||
const persistStateToConfigInjectable = getInjectable({
|
||||
id: "persist-state-to-config",
|
||||
instantiate: () => noop,
|
||||
injectionToken: persistStateToConfigInjectionToken,
|
||||
});
|
||||
|
||||
export default persistStateToConfigInjectable;
|
||||
Loading…
Reference in New Issue
Block a user