Skip to content

Commit

Permalink
CB-5658 chore: refactor repeated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Wroud committed Sep 18, 2024
1 parent 979dc0c commit d5783af
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
15 changes: 3 additions & 12 deletions webapp/packages/core-authentication/src/UsersResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import { runInAction } from 'mobx';

import { injectable } from '@cloudbeaver/core-di';
import {
CACHED_RESOURCE_DEFAULT_PAGE_LIMIT,
CACHED_RESOURCE_DEFAULT_PAGE_OFFSET,
CachedMapAllKey,
CachedMapResource,
CachedResourceOffsetPageKey,
CachedResourceOffsetPageListKey,
getOffsetPageKeyInfo,
isResourceAlias,
type ResourceKey,
resourceKeyList,
Expand Down Expand Up @@ -260,19 +259,11 @@ export class UsersResource extends CachedMapResource<string, AdminUser, UserReso

usersList.push(user);
} else {
const pageListKey = this.aliases.isAlias(originalKey, CachedResourceOffsetPageListKey);
const pageKey = this.aliases.isAlias(originalKey, CachedResourceOffsetPageKey) || pageListKey;
const { isPageListKey, offset, limit } = getOffsetPageKeyInfo(this, originalKey);
const filterKey = this.aliases.isAlias(originalKey, UsersResourceFilterKey);
let offset = CACHED_RESOURCE_DEFAULT_PAGE_OFFSET;
let limit = CACHED_RESOURCE_DEFAULT_PAGE_LIMIT;
let userIdMask: string | undefined;
let enabledState: boolean | undefined;

if (pageKey) {
offset = pageKey.options.offset;
limit = pageKey.options.limit;
}

if (filterKey) {
userIdMask = filterKey.options.userId;
enabledState = filterKey.options.enabledState;
Expand All @@ -294,7 +285,7 @@ export class UsersResource extends CachedMapResource<string, AdminUser, UserReso
usersList.push(...users);

pages.push([
pageListKey
isPageListKey
? CachedResourceOffsetPageListKey(offset, users.length).setParent(filterKey)
: CachedResourceOffsetPageKey(offset, users.length).setParent(filterKey),
users.map(user => user.userId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
CachedResourceOffsetPageKey,
CachedResourceOffsetPageListKey,
CachedResourceOffsetPageTargetKey,
getOffsetPageKeyInfo,
isResourceAlias,
type ResourceKey,
resourceKeyList,
Expand Down Expand Up @@ -96,16 +97,8 @@ export class DBObjectResource extends CachedMapResource<string, DBObject> {
}

protected async loader(originalKey: ResourceKey<string>): Promise<Map<string, DBObject>> {
let limit = this.navTreeResource.childrenLimit;
let offset = CACHED_RESOURCE_DEFAULT_PAGE_OFFSET;
const parentKey = this.aliases.isAlias(originalKey, DBObjectParentKey);
const pageListKey = this.aliases.isAlias(originalKey, CachedResourceOffsetPageListKey);
const pageKey = this.aliases.isAlias(originalKey, CachedResourceOffsetPageKey) || pageListKey;

if (pageKey) {
limit = pageKey.options.limit;
offset = pageKey.options.offset;
}
const { isPageListKey, offset, limit } = getOffsetPageKeyInfo(this, originalKey, undefined, this.navTreeResource.childrenLimit);

if (parentKey) {
const nodeId = parentKey.options.parentId;
Expand All @@ -116,7 +109,7 @@ export class DBObjectResource extends CachedMapResource<string, DBObject> {
this.set(resourceKeyList(keys), dbObjects);

this.offsetPagination.setPage(
pageListKey
isPageListKey
? CachedResourceOffsetPageListKey(offset, limit).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId))
: CachedResourceOffsetPageKey(offset, limit).setParent(parentKey || CachedResourceOffsetPageTargetKey(nodeId)),
keys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { injectable } from '@cloudbeaver/core-di';
import { Executor, ExecutorInterrupter, IExecutionContext, IExecutor } from '@cloudbeaver/core-executor';
import { ProjectInfoResource } from '@cloudbeaver/core-projects';
import {
CACHED_RESOURCE_DEFAULT_PAGE_OFFSET,
CachedMapAllKey,
CachedMapResource,
CachedResourceOffsetPageKey,
CachedResourceOffsetPageListKey,
CachedResourceOffsetPageTargetKey,
getOffsetPageKeyInfo,
type ICachedResourceMetadata,
isResourceAlias,
isResourceKeyList,
Expand Down Expand Up @@ -462,27 +462,23 @@ export class NavTreeResource extends CachedMapResource<string, string[], Record<
}

protected async loader(originalKey: ResourceKey<string>): Promise<Map<string, string[]>> {
const pageListKey = this.aliases.isAlias(originalKey, CachedResourceOffsetPageListKey);
const pageKey = this.aliases.isAlias(originalKey, CachedResourceOffsetPageKey) || pageListKey;
const { isPageListKey, pageTargetKey, offset, limit } = getOffsetPageKeyInfo(this, originalKey, undefined, this.childrenLimit);
const allKey = this.aliases.isAlias(originalKey, CachedMapAllKey);
const pageTarget = this.aliases.isAlias(originalKey, CachedResourceOffsetPageTargetKey);

if (allKey) {
throw new Error('Loading all nodes is prohibited');
}

const offset = pageKey?.options.offset ?? CACHED_RESOURCE_DEFAULT_PAGE_OFFSET;
const limit = pageKey?.options.limit ?? this.childrenLimit;
const values: NavNodeChildrenQuery[] = [];
const pages: Parameters<typeof this.offsetPagination.setPage>[] = [];

await ResourceKeyUtils.forEachAsync(originalKey, async key => {
const nodeId = pageTarget?.options?.target ?? key;
const nodeId = pageTargetKey ?? key;
const navNodeChildren = await this.loadNodeChildren(nodeId, offset, limit);
values.push(navNodeChildren);

pages.push([
pageListKey
isPageListKey
? CachedResourceOffsetPageListKey(offset, navNodeChildren.navNodeChildren.length).setParent(CachedResourceOffsetPageTargetKey(nodeId))
: CachedResourceOffsetPageKey(offset, navNodeChildren.navNodeChildren.length).setParent(CachedResourceOffsetPageTargetKey(nodeId)),
navNodeChildren.navNodeChildren.map(node => node.id),
Expand Down
46 changes: 46 additions & 0 deletions webapp/packages/core-resource/src/Resource/getOffsetPageKeyInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { CachedResource } from './CachedResource';
import {
CACHED_RESOURCE_DEFAULT_PAGE_LIMIT,
CACHED_RESOURCE_DEFAULT_PAGE_OFFSET,
CachedResourceOffsetPageKey,
CachedResourceOffsetPageListKey,
CachedResourceOffsetPageTargetKey,
} from './CachedResourceOffsetPageKeys';
import { CachedResourceKey } from './IResource';

interface IOffsetPageKeyInfo {
limit: number;
offset: number;
isPageListKey: boolean;
pageTargetKey?: any;
}

export function getOffsetPageKeyInfo(
resource: CachedResource<any, any, any, any>,
originalKey: CachedResourceKey<any>,
offset = CACHED_RESOURCE_DEFAULT_PAGE_OFFSET,
limit = CACHED_RESOURCE_DEFAULT_PAGE_LIMIT,
): IOffsetPageKeyInfo {
const pageListKey = resource.aliases.isAlias(originalKey, CachedResourceOffsetPageListKey);
const pageKey = resource.aliases.isAlias(originalKey, CachedResourceOffsetPageKey) || pageListKey;
const pageTargetKey = resource.aliases.isAlias(originalKey, CachedResourceOffsetPageTargetKey);

if (pageKey) {
limit = pageKey.options.limit;
offset = pageKey.options.offset;
}

return {
limit,
offset,
isPageListKey: !!pageListKey,
pageTargetKey: pageTargetKey?.options.target,
};
}
1 change: 1 addition & 0 deletions webapp/packages/core-resource/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {
getNextPageOffset,
type ICachedResourceOffsetPageOptions,
} from './Resource/CachedResourceOffsetPageKeys';
export * from './Resource/getOffsetPageKeyInfo';
export * from './Resource/CachedTreeResource/CachedTreeResource';
export * from './Resource/CachedTreeResource/ICachedTreeMoveData';
export * from './Resource/ICachedResourceMetadata';
Expand Down

0 comments on commit d5783af

Please sign in to comment.