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

Switch to using sendMessageToChannel

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-01-31 08:50:15 -05:00
parent a73f166f1f
commit f571de24f0
8 changed files with 34 additions and 12 deletions

View File

@ -8,7 +8,6 @@ import type { Migrations, Options as ConfOptions } from "conf/dist/source/types"
import type { IEqualsComparer } from "mobx";
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";
@ -16,18 +15,20 @@ 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";
import type { SendMessageToChannel } from "../utils/channel/message-to-channel-injection-token";
import type { MessageChannel } from "../utils/channel/message-channel-listener-injection-token";
export interface BaseStoreParams<T> extends Omit<ConfOptions<T>, "migrations"> {
export interface BaseStoreParams<T> extends Omit<ConfOptions<T>, "migrations" | "projectVersion"> {
syncOptions?: {
fireImmediately?: boolean;
equals?: IEqualsComparer<T>;
};
configName: string;
readonly configName: string;
}
export interface IpcChannelPrefixes {
local: string;
remote: string;
readonly local: string;
readonly remote: string;
}
export interface BaseStoreDependencies {
@ -41,6 +42,7 @@ export interface BaseStoreDependencies {
persistStateToConfig: PersistStateToConfig;
getBasenameOfPath: GetBasenameOfPath;
enlistMessageChannelListener: EnlistMessageChannelListener;
sendMessageToChannel: SendMessageToChannel;
}
/**
@ -96,20 +98,26 @@ export abstract class BaseStore<T extends object> {
const name = this.dependencies.getBasenameOfPath(config.path);
const disableSync = () => this.syncDisposers();
const sendChannel: MessageChannel<T> = {
id: `${this.dependencies.ipcChannelPrefixes.remote}:${config.path}`,
};
const receiveChannel: MessageChannel<T> = {
id: `${this.dependencies.ipcChannelPrefixes.local}:${config.path}`,
};
const enableSync = () => {
this.syncDisposers.push(
reaction(
() => toJS(this.toJSON()), // unwrap possible observables and react to everything
model => {
this.dependencies.persistStateToConfig(config, model);
broadcastMessage(`${this.dependencies.ipcChannelPrefixes.remote}:${config.path}`, model);
this.dependencies.sendMessageToChannel(sendChannel, model);
},
this.params.syncOptions,
),
this.dependencies.enlistMessageChannelListener({
channel: {
id: `${this.dependencies.ipcChannelPrefixes.local}:${config.path}`,
},
channel: receiveChannel,
handler: (model) => {
this.dependencies.logger.silly(`[${this.displayName}]: syncing ${name}`, { model });
@ -119,11 +127,13 @@ export abstract class BaseStore<T extends object> {
// todo: use "resourceVersion" if merge required (to avoid equality checks => better performance)
if (!isEqual(this.toJSON(), model)) {
this.fromStore(model as T);
this.fromStore(model);
}
if (this.dependencies.shouldDisableSyncInListener) {
enableSync();
setImmediate(() => {
enableSync();
});
}
},
}),

View File

@ -18,6 +18,7 @@ import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../base-s
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";
import { sendMessageToChannelInjectionToken } from "../utils/channel/message-to-channel-injection-token";
const clusterStoreInjectable = getInjectable({
id: "cluster-store",
@ -36,6 +37,7 @@ const clusterStoreInjectable = getInjectable({
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
sendMessageToChannel: di.inject(sendMessageToChannelInjectionToken),
}),
});

View File

@ -16,6 +16,7 @@ import { baseStoreIpcChannelPrefixesInjectionToken } from "../base-store/channel
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";
import { sendMessageToChannelInjectionToken } from "../utils/channel/message-to-channel-injection-token";
const hotbarStoreInjectable = getInjectable({
id: "hotbar-store",
@ -32,6 +33,7 @@ const hotbarStoreInjectable = getInjectable({
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
sendMessageToChannel: di.inject(sendMessageToChannelInjectionToken),
}),
});

View File

@ -18,6 +18,7 @@ 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";
import userStorePreferenceDescriptorsInjectable from "./preference-descriptors.injectable";
import { sendMessageToChannelInjectionToken } from "../utils/channel/message-to-channel-injection-token";
const userStoreInjectable = getInjectable({
id: "user-store",
@ -36,6 +37,7 @@ const userStoreInjectable = getInjectable({
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
preferenceDescriptors: di.inject(userStorePreferenceDescriptorsInjectable),
sendMessageToChannel: di.inject(sendMessageToChannelInjectionToken),
}),
});

View File

@ -6,7 +6,7 @@ import { getInjectionToken } from "@ogre-tools/injectable";
import type { Disposer } from "../disposer";
import type { MessageChannel, MessageChannelListener } from "./message-channel-listener-injection-token";
export type EnlistMessageChannelListener = (listener: MessageChannelListener<MessageChannel<unknown>>) => Disposer;
export type EnlistMessageChannelListener = <T>(listener: MessageChannelListener<MessageChannel<T>>) => Disposer;
export const enlistMessageChannelListenerInjectionToken = getInjectionToken<EnlistMessageChannelListener>({
id: "enlist-message-channel-listener",

View File

@ -12,6 +12,7 @@ import getConfigurationFileModelInjectable from "../get-configuration-file-model
import loggerInjectable from "../logger.injectable";
import getBasenameOfPathInjectable from "../path/get-basename.injectable";
import { enlistMessageChannelListenerInjectionToken } from "../utils/channel/enlist-message-channel-listener-injection-token";
import { sendMessageToChannelInjectionToken } from "../utils/channel/message-to-channel-injection-token";
import storeMigrationVersionInjectable from "../vars/store-migration-version.injectable";
import { weblinkStoreMigrationInjectionToken } from "./migration-token";
import { WeblinkStore } from "./weblink-store";
@ -29,6 +30,7 @@ const weblinkStoreInjectable = getInjectable({
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
sendMessageToChannel: di.inject(sendMessageToChannelInjectionToken),
}),
});

View File

@ -17,6 +17,7 @@ import { shouldBaseStoreDisableSyncInIpcListenerInjectionToken } from "../../../
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";
import { sendMessageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token";
const fileSystemProvisionerStoreInjectable = getInjectable({
id: "file-system-provisioner-store",
@ -36,6 +37,7 @@ const fileSystemProvisionerStoreInjectable = getInjectable({
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
sendMessageToChannel: di.inject(sendMessageToChannelInjectionToken),
}),
});

View File

@ -11,6 +11,7 @@ import getConfigurationFileModelInjectable from "../../common/get-configuration-
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 { sendMessageToChannelInjectionToken } from "../../common/utils/channel/message-to-channel-injection-token";
import storeMigrationVersionInjectable from "../../common/vars/store-migration-version.injectable";
import { ExtensionsStore } from "./extensions-store";
@ -27,6 +28,7 @@ const extensionsStoreInjectable = getInjectable({
persistStateToConfig: di.inject(persistStateToConfigInjectionToken),
enlistMessageChannelListener: di.inject(enlistMessageChannelListenerInjectionToken),
shouldDisableSyncInListener: di.inject(shouldBaseStoreDisableSyncInIpcListenerInjectionToken),
sendMessageToChannel: di.inject(sendMessageToChannelInjectionToken),
}),
});