Skip to content

Commit

Permalink
refactor: add loading namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
leopuleo committed Oct 21, 2024
1 parent 0c5b524 commit 603aa5e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { folderCacheFactory } from "../cache";
export class CreateFolder {
public static instance(type: string, gateway: ICreateFolderGateway): ICreateFolderUseCase {
const foldersCache = folderCacheFactory.getCache(type);
const loadingRepository = loadingRepositoryFactory.getRepository();
const loadingRepository = loadingRepositoryFactory.getRepository(type);
const repository = new CreateFolderRepository(foldersCache, gateway, type);
const useCase = new CreateFolderUseCase(repository);
return new CreateFolderUseCaseWithLoading(loadingRepository, useCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { folderCacheFactory } from "../cache";
export class DeleteFolder {
public static instance(type: string, gateway: IDeleteFolderGateway): IDeleteFolderUseCase {
const foldersCache = folderCacheFactory.getCache(type);
const loadingRepository = loadingRepositoryFactory.getRepository();
const loadingRepository = loadingRepositoryFactory.getRepository(type);
const repository = new DeleteFolderRepository(foldersCache, gateway);
const useCase = new DeleteFolderUseCase(repository);
return new DeleteFolderUseCaseWithLoading(loadingRepository, useCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { folderCacheFactory } from "../cache";
export class GetFolder {
public static instance(type: string, gateway: IGetFolderGateway): IGetFolderUseCase {
const foldersCache = folderCacheFactory.getCache(type);
const loadingRepository = loadingRepositoryFactory.getRepository();
const loadingRepository = loadingRepositoryFactory.getRepository(type);
const repository = new GetFolderRepository(foldersCache, gateway);
const useCase = new GetFolderUseCase(repository);
return new GetFolderUseCaseWithLoading(loadingRepository, useCase);
Expand Down
38 changes: 8 additions & 30 deletions packages/app-aco/src/features/folder/listFolders/ListFolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,17 @@ interface IListFoldersInstance {
}

export class ListFolders {
static useCaseCache: Map<string, IListFoldersUseCase> = new Map();
static foldersCache: Map<string, FoldersCache> = new Map();
static loadingCache: Map<string, LoadingRepository> = new Map();

public static instance(type: string, gateway: IListFoldersGateway): IListFoldersInstance {
if (!this.foldersCache.has(type)) {
this.foldersCache.set(type, folderCacheFactory.getCache(type));
}

if (!this.loadingCache.has(type)) {
this.loadingCache.set(type, loadingRepositoryFactory.getRepository());
}

if (!this.useCaseCache.has(type)) {
// Create a new instance if not cached
const foldersCache = this.foldersCache.get(type)!;
const loadingRepository = this.loadingCache.get(type)!;
const repository = new ListFoldersRepository(foldersCache, gateway, type);
const useCase = new ListFoldersUseCase(repository);
const useCaseWithLoading = new ListFoldersUseCaseWithLoading(
loadingRepository,
useCase
);

// Store in cache
this.useCaseCache.set(type, useCaseWithLoading);
}
const foldersCache = folderCacheFactory.getCache(type);
const loadingRepository = loadingRepositoryFactory.getRepository(type);
const repository = new ListFoldersRepository(foldersCache, gateway, type);
const useCase = new ListFoldersUseCase(repository);
const useCaseWithLoading = new ListFoldersUseCaseWithLoading(loadingRepository, useCase);

// Return the cached instance
return {
useCase: this.useCaseCache.get(type)!,
folders: this.foldersCache.get(type)!,
loading: this.loadingCache.get(type)!
useCase: useCaseWithLoading,
folders: foldersCache,
loading: loadingRepository
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { folderCacheFactory } from "~/features/folder";
export class UpdateFolder {
public static instance(type: string, gateway: IUpdateFolderGateway): IUpdateFolderUseCase {
const foldersCache = folderCacheFactory.getCache(type);
const loadingRepository = loadingRepositoryFactory.getRepository();
const loadingRepository = loadingRepositoryFactory.getRepository(type);
const repository = new UpdateFolderRepository(foldersCache, gateway);
const useCase = new UpdateFolderUseCase(repository);
return new UpdateFolderUseCaseWithLoading(loadingRepository, useCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { LoadingRepository } from "./LoadingRepository";
export class LoadingRepositoryFactory {
private cache: Map<string, LoadingRepository> = new Map();

getRepository() {
const cacheKey = this.getCacheKey();
getRepository(namespace?: string) {
const cacheKey = this.getCacheKey(namespace);

if (!this.cache.has(cacheKey)) {
this.cache.set(cacheKey, new LoadingRepository());
Expand All @@ -13,8 +13,8 @@ export class LoadingRepositoryFactory {
return this.cache.get(cacheKey) as LoadingRepository;
}

private getCacheKey() {
return Date.now().toString();
private getCacheKey(namespace?: string) {
return namespace ?? Date.now().toString();
}
}

Expand Down

0 comments on commit 603aa5e

Please sign in to comment.