-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: enhance folder listing and permission checks (#4539)
- Loading branch information
Showing
13 changed files
with
337 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Folder } from "~/folder/folder.types"; | ||
import { ListCache } from "~/utils/ListCache"; | ||
|
||
export class FoldersCacheFactory { | ||
private cache: Map<string, ListCache<Folder>> = new Map(); | ||
|
||
hasCache(namespace: string) { | ||
const cacheKey = this.getCacheKey(namespace); | ||
return this.cache.has(cacheKey); | ||
} | ||
|
||
getCache(namespace: string) { | ||
const cacheKey = this.getCacheKey(namespace); | ||
|
||
if (!this.cache.has(cacheKey)) { | ||
this.cache.set(cacheKey, new ListCache<Folder>()); | ||
} | ||
|
||
return this.cache.get(cacheKey) as ListCache<Folder>; | ||
} | ||
|
||
deleteCache() { | ||
this.cache.clear(); | ||
} | ||
|
||
private getCacheKey(namespace: string) { | ||
return namespace; | ||
} | ||
} | ||
|
||
export const folderCacheFactory = new FoldersCacheFactory(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import cloneDeep from "lodash/cloneDeep"; | ||
|
||
export type Constructor<T> = new (...args: any[]) => T; | ||
|
||
export interface IListCachePredicate<T> { | ||
(item: T): boolean; | ||
} | ||
|
||
export interface IListCacheItemUpdater<T> { | ||
(item: T): T; | ||
} | ||
|
||
export interface IListCache<T> { | ||
clear(): void; | ||
hasItems(): boolean; | ||
getItems(): T[]; | ||
addItems(items: T[]): void; | ||
updateItems(updater: IListCacheItemUpdater<T>): void; | ||
} | ||
|
||
export class ListCache<T> implements IListCache<T> { | ||
private state: T[]; | ||
|
||
constructor() { | ||
this.state = []; | ||
} | ||
|
||
clear(): void { | ||
this.state = []; | ||
} | ||
|
||
hasItems(): boolean { | ||
return this.state.length > 0; | ||
} | ||
|
||
getItems(): T[] { | ||
return cloneDeep(this.state); | ||
} | ||
|
||
addItems(items: T[]): void { | ||
this.state.push(...items); | ||
} | ||
|
||
updateItems(updater: IListCacheItemUpdater<T>): void { | ||
this.state = this.state.map(item => updater(item)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { folderCacheFactory } from "~/utils/FoldersCacheFactory"; | ||
import { Folder, ListFoldersParams } from "~/folder/folder.types"; | ||
import { ListMeta } from "~/types"; | ||
|
||
export interface ListFoldersRepositoryParams { | ||
gateway: (params: ListFoldersParams) => Promise<[Folder[], ListMeta]>; | ||
} | ||
|
||
export class ListFoldersRepository { | ||
private readonly gateway: (params: ListFoldersParams) => Promise<[Folder[], ListMeta]>; | ||
|
||
constructor(params: ListFoldersRepositoryParams) { | ||
this.gateway = params.gateway; | ||
} | ||
|
||
public async execute(folderType: string): Promise<Folder[]> { | ||
if (folderCacheFactory.hasCache(folderType)) { | ||
return folderCacheFactory.getCache(folderType).getItems(); | ||
} | ||
|
||
let hasMoreItems: ListMeta["hasMoreItems"] = true; | ||
let cursor: ListMeta["cursor"] = null; | ||
|
||
while (hasMoreItems) { | ||
const response: [Folder[], ListMeta] = await this.gateway({ | ||
where: { type: folderType }, | ||
after: cursor | ||
}); | ||
|
||
const [folders, meta] = response; | ||
|
||
folderCacheFactory.getCache(folderType).addItems(folders); | ||
hasMoreItems = meta.hasMoreItems; | ||
cursor = meta.cursor; | ||
} | ||
|
||
return folderCacheFactory.getCache(folderType).getItems(); | ||
} | ||
} |
Oops, something went wrong.