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

switch to detecting ==null in the setter

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-03-31 12:50:22 -04:00
parent c6ba0a1f78
commit f4ce27dc0d
6 changed files with 10 additions and 21 deletions

View File

@ -5,7 +5,7 @@ import { Namespace, namespacesApi } from "../../api/endpoints/namespaces.api";
import { createPageParam } from "../../navigation"; import { createPageParam } from "../../navigation";
import { apiManager } from "../../api/api-manager"; import { apiManager } from "../../api/api-manager";
const selectedNamespaces = createStorage<string[]>("selected_namespaces"); const selectedNamespaces = createStorage<string[]>("selected_namespaces", []);
export const namespaceUrlParam = createPageParam<string[]>({ export const namespaceUrlParam = createPageParam<string[]>({
name: "namespaces", name: "namespaces",

View File

@ -81,6 +81,6 @@ export class DockTabStore<T> {
reset() { reset() {
this.data.clear(); this.data.clear();
this.storage?.clear(); this.storage?.reset();
} }
} }

View File

@ -2,7 +2,7 @@ import { useState } from "react";
import { createStorage } from "../utils"; import { createStorage } from "../utils";
import { CreateObservableOptions } from "mobx/lib/api/observable"; import { CreateObservableOptions } from "mobx/lib/api/observable";
export function useStorage<T>(key: string, initialValue?: T, options?: CreateObservableOptions) { export function useStorage<T>(key: string, initialValue: T, options?: CreateObservableOptions) {
const storage = createStorage(key, initialValue, options); const storage = createStorage(key, initialValue, options);
const [storageValue, setStorageValue] = useState(storage.get()); const [storageValue, setStorageValue] = useState(storage.get());
const setValue = (value: T) => { const setValue = (value: T) => {

View File

@ -142,17 +142,6 @@ describe("renderer/utils/StorageHelper", () => {
})); }));
expect(storageHelper.get()).toEqual({ ...storageHelperDefaultValue, message: "updated3" }); expect(storageHelper.get()).toEqual({ ...storageHelperDefaultValue, message: "updated3" });
}); });
it("clears data in storage", () => {
storageHelper.init();
expect(storageHelper.get()).toBeTruthy();
storageHelper.clear();
expect((storageHelper as any).data.get()).toBeFalsy();
expect(storageMock[storageKey]).toBeUndefined();
expect(storageAdapter.removeItem).toHaveBeenCalledWith(storageHelper.key);
});
}); });
describe("data in storage-helper is observable (mobx)", () => { describe("data in storage-helper is observable (mobx)", () => {

View File

@ -14,7 +14,7 @@ let initialized = false;
const loaded = observable.box(false); const loaded = observable.box(false);
const storage = observable.map<string/* key */, any /* serializable */>(); const storage = observable.map<string/* key */, any /* serializable */>();
export function createStorage<T>(key: string, defaultValue?: T, observableOptions?: CreateObservableOptions) { export function createStorage<T>(key: string, defaultValue: T, observableOptions?: CreateObservableOptions) {
const clusterId = getHostedClusterId(); const clusterId = getHostedClusterId();
const savingFolder = path.resolve((app || remote.app).getPath("userData"), "lens-local-storage"); const savingFolder = path.resolve((app || remote.app).getPath("userData"), "lens-local-storage");
const jsonFilePath = path.resolve(savingFolder, `${clusterId ?? "app"}.json`); const jsonFilePath = path.resolve(savingFolder, `${clusterId ?? "app"}.json`);

View File

@ -129,21 +129,21 @@ export class StorageHelper<T> {
} }
get(): T { get(): T {
return this.data.get() ?? this.defaultValue; return this.data.get();
} }
set(value: T) { set(value: T) {
this.data.set(value); if (value == null) {
this.reset();
} else {
this.data.set(value);
}
} }
reset() { reset() {
this.set(this.defaultValue); this.set(this.defaultValue);
} }
clear() {
this.data.set(null);
}
merge(value: Partial<T> | ((draft: Draft<T>) => Partial<T> | void)) { merge(value: Partial<T> | ((draft: Draft<T>) => Partial<T> | void)) {
const nextValue = produce(this.get(), (state: Draft<T>) => { const nextValue = produce(this.get(), (state: Draft<T>) => {
const newValue = isFunction(value) ? value(state) : value; const newValue = isFunction(value) ? value(state) : value;