Skip to content

Commit ec54a6f

Browse files
authored
Merge pull request #17 from Code-Hex/fix/workers-kv-singleton
fixed singleton behaviour for WorkersKVStoreSingle
2 parents 0f98722 + 7228c47 commit ec54a6f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,23 @@ export class Auth extends BaseAuth {
3535
}
3636

3737
export class WorkersKVStoreSingle extends WorkersKVStore {
38-
private static instance?: WorkersKVStoreSingle;
38+
private static instance?: Map<string, WorkersKVStoreSingle>;
3939

4040
private constructor(cacheKey: string, cfKVNamespace: KVNamespace) {
4141
super(cacheKey, cfKVNamespace);
4242
}
4343

4444
static getOrInitialize(cacheKey: string, cfKVNamespace: KVNamespace): WorkersKVStoreSingle {
4545
if (!WorkersKVStoreSingle.instance) {
46-
WorkersKVStoreSingle.instance = new WorkersKVStoreSingle(cacheKey, cfKVNamespace);
46+
WorkersKVStoreSingle.instance = new Map<string, WorkersKVStoreSingle>();
4747
}
48-
return WorkersKVStoreSingle.instance;
48+
const instance = WorkersKVStoreSingle.instance.get(cacheKey);
49+
if (instance) {
50+
return instance;
51+
}
52+
const newInstance = new WorkersKVStoreSingle(cacheKey, cfKVNamespace);
53+
WorkersKVStoreSingle.instance.set(cacheKey, newInstance);
54+
return newInstance;
4955
}
5056
}
5157

0 commit comments

Comments
 (0)