feat: CB-1826 add isEqual for cached resources

This commit is contained in:
Wroud
2022-03-29 19:51:20 +03:00
parent 740dedaedc
commit c5d5ccd47d
4 changed files with 27 additions and 15 deletions
@@ -14,6 +14,7 @@ import { NotificationService } from '@cloudbeaver/core-events';
import { CachedResourceIncludeArgs, CachedMapResource, CachedMapResourceGetter, ResourceKey, CachedMapResourceValue, CachedMapResourceKey, CachedMapResourceArguments, CachedMapResourceLoader, ResourceKeyList, CachedMapResourceListGetter, isResourceKeyList } from '@cloudbeaver/core-sdk';
import type { ILoadableState } from './Loader/Loader';
import { useObjectRef } from './useObjectRef';
import { useObservableRef } from './useObservableRef';
interface IActions<
@@ -137,9 +138,19 @@ export function useMapResource<
includes = keyObj.includes;
}
const keyRef = useObjectRef(() => ({
loadedKey: null as TKeyArg | null,
get actual() {
if (this.loadedKey === this.key) {
return true;
}
return resource.isKeyEqual(this.loadedKey, this.key);
},
}), { key });
const refObj = useObservableRef(() => ({
loading: false,
firstRender: true,
prevData: undefined as CachedMapResourceLoader<
TKeyArg,
CachedMapResourceKey<TResource>,
@@ -178,7 +189,7 @@ export function useMapResource<
return;
}
this.firstRender = false;
keyRef.loadedKey = key;
this.loading = true;
const prevent = await actions?.onLoad?.(resource);
@@ -298,7 +309,7 @@ export function useMapResource<
const preloaded = refObj.preloaded; // make mobx subscription
useEffect(() => {
if (!preloaded || (!outdated && !refObj.firstRender) || refObj.key === null) {
if (!preloaded || (!outdated && keyRef.actual) || refObj.key === null) {
return;
}
@@ -392,6 +392,14 @@ export abstract class CachedMapResource<
}, {});
}
isKeyEqual(param: ResourceKey<TKey>, second: ResourceKey<TKey>): boolean {
if (this.isAlias(param) || this.isAlias(second)) {
return true;
}
return this.includes(param, second);
}
protected dataSet(key: TKey, value: TValue): void {
this.data.set(key, value);
}
@@ -400,14 +408,6 @@ export abstract class CachedMapResource<
this.data.delete(key);
}
protected lock(param: ResourceKey<TKey>, second: ResourceKey<TKey>): boolean {
if (this.isAlias(param) || this.isAlias(second)) {
return true;
}
return this.includes(param, second);
}
protected commitIncludes(key: ResourceKey<TKey>, includes: string[]): void {
key = this.transformParam(key);
ResourceKeyUtils.forEach(key, key => {
@@ -63,7 +63,7 @@ export abstract class CachedResource<
constructor(defaultValue: TData, defaultIncludes: string[] = []) {
super();
this.lock = this.lock.bind(this);
this.isKeyEqual = this.isKeyEqual.bind(this);
this.includes = this.includes.bind(this);
this.loadingTask = this.loadingTask.bind(this);
@@ -79,9 +79,9 @@ export abstract class CachedResource<
exception: null,
includes: observable([...this.defaultIncludes]),
}, undefined, { deep: false })));
this.scheduler = new TaskScheduler(this.lock);
this.scheduler = new TaskScheduler(this.isKeyEqual);
this.data = defaultValue;
this.beforeLoad = new Executor(null, this.lock);
this.beforeLoad = new Executor(null, this.isKeyEqual);
this.onDataOutdated = new SyncExecutor<TParam>(null);
this.onDataUpdate = new SyncExecutor<TParam>(null);
this.onDataError = new SyncExecutor<IDataError<TParam>>(null);
@@ -404,7 +404,7 @@ export abstract class CachedResource<
this.onDataOutdated.execute(param);
}
protected lock(param: TParam, second: TParam): boolean {
isKeyEqual(param: TParam, second: TParam): boolean {
if (this.isAlias(param) || this.isAlias(second)) {
return true;
}
@@ -132,6 +132,7 @@ export const SqlEditor = observer<ISqlEditorProps>(function SqlEditor({ state, c
container={sqlEditorModeService.tabsContainer}
state={state}
data={data}
lazy
onChange={tab => state.currentEditorId = tab.tabId}
>
<sql-editor className={className}>