Skip to content

Commit

Permalink
feat: path-scoped singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jan 4, 2022
1 parent 887bb68 commit de46db4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/shared/localShadowRepo.ts
Expand Up @@ -45,6 +45,8 @@ interface CommitRequest {
}

export class ShadowRepo {
private static instanceMap = new Map<string, ShadowRepo>();

public gitDir: string;
public projectPath: string;

Expand All @@ -59,11 +61,14 @@ export class ShadowRepo {
this.packageDirs = options.packageDirs;
}

// this is NOT a singleton but it was at one point, so its public method still sounds like it would be
// think of singleton behavior but unique to the projectPath
public static async getInstance(options: ShadowRepoOptions): Promise<ShadowRepo> {
const instance = new ShadowRepo(options);
await instance.init();
return instance;
if (!ShadowRepo.instanceMap.has(options.projectPath)) {
const newInstance = new ShadowRepo(options);
await newInstance.init();
ShadowRepo.instanceMap.set(options.projectPath, newInstance);
}
return ShadowRepo.instanceMap.get(options.projectPath) as ShadowRepo;
}

public async init(): Promise<void> {
Expand Down

0 comments on commit de46db4

Please sign in to comment.