Skip to content

Commit

Permalink
fixed singleton behaviour for WorkersKVStoreSingle
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Feb 24, 2024
1 parent 0f98722 commit 7228c47
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@ export class Auth extends BaseAuth {
}

export class WorkersKVStoreSingle extends WorkersKVStore {
private static instance?: WorkersKVStoreSingle;
private static instance?: Map<string, WorkersKVStoreSingle>;

private constructor(cacheKey: string, cfKVNamespace: KVNamespace) {
super(cacheKey, cfKVNamespace);
}

static getOrInitialize(cacheKey: string, cfKVNamespace: KVNamespace): WorkersKVStoreSingle {
if (!WorkersKVStoreSingle.instance) {
WorkersKVStoreSingle.instance = new WorkersKVStoreSingle(cacheKey, cfKVNamespace);
WorkersKVStoreSingle.instance = new Map<string, WorkersKVStoreSingle>();
}
return WorkersKVStoreSingle.instance;
const instance = WorkersKVStoreSingle.instance.get(cacheKey);
if (instance) {
return instance;
}
const newInstance = new WorkersKVStoreSingle(cacheKey, cfKVNamespace);
WorkersKVStoreSingle.instance.set(cacheKey, newInstance);
return newInstance;
}
}

Expand Down

0 comments on commit 7228c47

Please sign in to comment.