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

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

WIPWIP
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2023-05-23 10:32:28 +03:00
parent 7258d51a91
commit 5f82cc50e4
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
3 changed files with 39 additions and 39 deletions

View File

@ -7,18 +7,15 @@ import { getInjectable } from "@ogre-tools/injectable";
import type { Options } from "conf/dist/source"; import type { Options } from "conf/dist/source";
import { isEqual, kebabCase } from "lodash"; import { isEqual, kebabCase } from "lodash";
import type { IEqualsComparer } from "mobx"; import type { IEqualsComparer } from "mobx";
import { reaction } from "mobx"; import { action, runInAction, reaction } from "mobx";
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"; import getConfigurationFileModelInjectable from "../get-configuration-file-model/get-configuration-file-model.injectable";
import { loggerInjectionToken } from "@k8slens/logger"; import { loggerInjectionToken } from "@k8slens/logger";
import getBasenameOfPathInjectable from "../path/get-basename.injectable"; import getBasenameOfPathInjectable from "../path/get-basename.injectable";
import { enlistMessageChannelListenerInjectionToken, sendMessageToChannelInjectionToken } from "@k8slens/messaging"; import { sendMessageToChannelInjectionToken, getMessageChannelListenerInjectable } from "@k8slens/messaging";
import type { MessageChannel } from "@k8slens/messaging"; import type { MessageChannel } from "@k8slens/messaging";
import { persistentStorageIpcChannelPrefixesInjectionToken } from "./channel-prefix";
import { shouldPersistentStorageDisableSyncInIpcListenerInjectionToken } from "./disable-sync";
import { persistStateToConfigInjectionToken } from "./save-to-file"; import { persistStateToConfigInjectionToken } from "./save-to-file";
import type { Migrations } from "./migrations.injectable"; import type { Migrations } from "./migrations.injectable";
import { nextTick } from "process";
export interface PersistentStorage { export interface PersistentStorage {
/** /**
@ -65,10 +62,8 @@ const createPersistentStorageInjectable = getInjectable({
const getConfigurationFileModel = di.inject(getConfigurationFileModelInjectable); const getConfigurationFileModel = di.inject(getConfigurationFileModelInjectable);
const logger = di.inject(loggerInjectionToken); const logger = di.inject(loggerInjectionToken);
const getBasenameOfPath = di.inject(getBasenameOfPathInjectable); const getBasenameOfPath = di.inject(getBasenameOfPathInjectable);
const ipcChannelPrefixes = di.inject(persistentStorageIpcChannelPrefixesInjectionToken);
const persistStateToConfig = di.inject(persistStateToConfigInjectionToken); const persistStateToConfig = di.inject(persistStateToConfigInjectionToken);
const enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken); // const shouldDisableSyncInListener = di.inject(shouldPersistentStorageDisableSyncInIpcListenerInjectionToken);
const shouldDisableSyncInListener = di.inject(shouldPersistentStorageDisableSyncInIpcListenerInjectionToken);
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken); const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
return <T extends object>(rawParams: PersistentStorageParams<T>) => { return <T extends object>(rawParams: PersistentStorageParams<T>) => {
@ -101,47 +96,53 @@ const createPersistentStorageInjectable = getInjectable({
logger.info(`[${displayName}]: LOADED from ${config.path}`); logger.info(`[${displayName}]: LOADED from ${config.path}`);
const syncDisposers = disposer(); const syncDisposers = disposer();
const sendChannel: MessageChannel<T> = {
id: `${ipcChannelPrefixes.remote}:${config.path}`, const targetChannel: MessageChannel<T> = {
}; id: config.path,
const receiveChannel: MessageChannel<T> = {
id: `${ipcChannelPrefixes.local}:${config.path}`,
}; };
const name = getBasenameOfPath(config.path); const name = getBasenameOfPath(config.path);
const disableSync = () => syncDisposers(); // const disableSync = () => syncDisposers();
const enableSync = () => { const enableSync = () => {
const mikkoInjectable = getMessageChannelListenerInjectable({
id: `persistent-storage-sync-${displayName}`,
channel: targetChannel,
getHandler: () => (model) => {
logger.silly(`[${displayName}]: syncing ${name}`, { model });
// if (shouldDisableSyncInListener) {
// disableSync();
// }
// todo: use "resourceVersion" if merge required (to avoid equality checks => better performance)
if (!isEqual(toJSON(), model)) {
fromStore(model);
}
// if (shouldDisableSyncInListener) {
// nextTick(() => {
// enableSync();
// });
// }
},
});
runInAction(() => {
di.register(mikkoInjectable);
});
syncDisposers.push( syncDisposers.push(
reaction( reaction(
() => toJSON(), () => toJSON(),
model => { (model) => {
persistStateToConfig(config, model); persistStateToConfig(config, model);
sendMessageToChannel(sendChannel, model); sendMessageToChannel(targetChannel, model);
}, },
syncOptions, syncOptions,
), ),
enlistMessageChannelListener({
id: "persistent-storage-sync",
channel: receiveChannel,
handler: (model) => {
logger.silly(`[${displayName}]: syncing ${name}`, { model });
if (shouldDisableSyncInListener) { action(() => di.deregister(mikkoInjectable)),
disableSync();
}
// todo: use "resourceVersion" if merge required (to avoid equality checks => better performance)
if (!isEqual(toJSON(), model)) {
fromStore(model);
}
if (shouldDisableSyncInListener) {
nextTick(() => {
enableSync();
});
}
},
}),
); );
}; };

View File

@ -13,7 +13,6 @@ export const messageBroadcasterListenerInjectable = getMessageChannelListenerInj
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken); const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
return ({ targetChannelId, message }, extraData) => { return ({ targetChannelId, message }, extraData) => {
console.log("mikkomikko", { targetChannelId, message });
sendMessageToChannel({ id: targetChannelId }, message, extraData); sendMessageToChannel({ id: targetChannelId }, message, extraData);
}; };
}, },

View File

@ -47,7 +47,7 @@ const overrideMessaging = ({
messageListenersByDi.set(di, messageHandlersByChannel); messageListenersByDi.set(di, messageHandlersByChannel);
di.override(sendMessageToChannelInjectionToken, () => (channel, message) => { di.override(sendMessageToChannelInjectionToken, () => (channel, message) => {
const allOtherDis = [...messageListenersByDi.keys()].filter((x) => x !== di); const allOtherDis = [...messageListenersByDi.keys()]; //.filter((x) => x !== di);
allOtherDis.forEach((otherDi) => { allOtherDis.forEach((otherDi) => {
const listeners = messageListenersByDi.get(otherDi); const listeners = messageListenersByDi.get(otherDi);