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

responding to comments, dock-tab.store.ts refactoring

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-03-17 14:25:42 +02:00
parent cd1749f331
commit c48135e0f3
7 changed files with 45 additions and 31 deletions

View File

@ -6,7 +6,7 @@ import { dockStore, IDockTab, TabKind } from "./dock.store";
export class CreateResourceStore extends DockTabStore<string> {
constructor() {
super({
storageName: "create_resource"
storageKey: "create_resource"
});
}
}

View File

@ -2,29 +2,39 @@ import { autorun, observable, reaction } from "mobx";
import { autobind, createStorage, StorageHelper } from "../../utils";
import { dockStore, TabId } from "./dock.store";
interface Options<T = any> {
storageName?: string; // persistence key
storageSerializer?: (data: T) => Partial<T>; // allow to customize data before saving
export interface DockTabStoreOptions {
autoInit?: boolean; // load data from storage when `storageKey` is provided and bind events, default: true
storageKey?: string; // save data to persistent storage under the key
}
export type DockTabStorageState<T> = Record<TabId, T>;
@autobind()
export class DockTabStore<T = any> {
private storage?: StorageHelper<Record<TabId, T>>;
export class DockTabStore<T> {
private storage?: StorageHelper<DockTabStorageState<T>>;
protected data = observable.map<TabId, T>();
constructor(protected options: Options<T> = {}) {
this.init();
constructor(protected options: DockTabStoreOptions = {}) {
this.options = {
autoInit: true,
...this.options,
};
if (this.options.autoInit) {
this.init();
}
}
protected async init() {
const { storageName: storageKey } = this.options;
protected init() {
const { storageKey } = this.options;
// auto-save to local-storage
if (storageKey) {
this.storage = createStorage(storageKey, {});
await this.storage.whenReady;
this.data.replace(this.storage.get());
reaction(() => this.serializeData(), (data: Record<TabId, T>) => this.storage.set(data));
this.storage.whenReady.then(() => {
this.data.replace(this.storage.get());
reaction(() => this.getStorableData(), data => this.storage.set(data));
});
}
// clear data for closed tabs
@ -39,21 +49,20 @@ export class DockTabStore<T = any> {
});
}
protected serializeData(): Record<TabId, Partial<T>> {
const data = this.data.toJSON();
const { storageSerializer } = this.options;
if (storageSerializer) {
return Object.entries(data).reduce((data, [tabId, tabData]) => {
data[tabId] = storageSerializer(tabData) as T;
return data;
}, data);
}
protected serializeBeforeSave(data: T): T {
return data;
}
protected getStorableData(): DockTabStorageState<T> {
const allTabsData = this.data.toJSON();
return Object.entries(allTabsData).reduce((data, [tabId, tabData]) => {
data[tabId] = this.serializeBeforeSave(tabData);
return data;
}, allTabsData);
}
getData(tabId: TabId) {
return this.data.get(tabId);
}

View File

@ -16,8 +16,7 @@ export class EditResourceStore extends DockTabStore<KubeEditResource> {
constructor() {
super({
storageName: "edit_resource_store",
storageSerializer: ({ draft, ...data }) => data, // skip saving draft in local-storage
storageKey: "edit_resource_store",
});
autorun(() => {
@ -47,6 +46,10 @@ export class EditResourceStore extends DockTabStore<KubeEditResource> {
});
});
}
protected serializeBeforeSave({ draft, ...data }: KubeEditResource) {
return data; // skip saving draft to local-storage
}
getTabByResource(object: KubeObject): IDockTab {
const [tabId] = Array.from(this.data).find(([, { resource }]) => {

View File

@ -22,7 +22,7 @@ export class InstallChartStore extends DockTabStore<IChartInstallData> {
constructor() {
super({
storageName: "install_charts"
storageKey: "install_charts"
});
autorun(() => {
const { selectedTab, isOpen } = dockStore;

View File

@ -27,7 +27,7 @@ interface WorkloadLogsTabData {
export class LogTabStore extends DockTabStore<LogTabData> {
constructor() {
super({
storageName: "pod_logs"
storageKey: "pod_logs"
});
reaction(() => podsStore.items.length, () => {

View File

@ -16,7 +16,7 @@ export class UpgradeChartStore extends DockTabStore<IChartUpgradeData> {
constructor() {
super({
storageName: "chart_releases"
storageKey: "chart_releases"
});
autorun(() => {

View File

@ -32,7 +32,9 @@ export function createStorage<T>(key: string, defaultValue?: T, observableOption
reaction(() => storage.toJSON(), saveFile, { delay: 250 });
// remove json-file when cluster deleted
when(() => clusterStore.removedClusters.has(clusterId)).then(removeFile);
if (clusterId !== undefined) {
when(() => clusterStore.removedClusters.has(clusterId)).then(removeFile);
}
}
async function saveFile(json = {}) {