Skip to content

Commit

Permalink
Merge pull request #17 from Code-Hex/fix/workers-kv-singleton
Browse files Browse the repository at this point in the history
fixed singleton behaviour for WorkersKVStoreSingle
  • Loading branch information
Code-Hex authored Feb 24, 2024
2 parents 0f98722 + 7228c47 commit ec54a6f
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 ec54a6f

Please sign in to comment.