-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a28dadd
commit f69b2f1
Showing
3 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
export type MemoryStorage = { | ||
has: (key: string) => boolean; | ||
getItem: <T>(key: string) => T | null; | ||
setItem: (key: string, value: unknown) => void; | ||
removeItem: (key: string) => void; | ||
clear: () => void; | ||
size: number; | ||
}; | ||
|
||
class MemoryStorageImpl implements MemoryStorage { | ||
private readonly storage: Map<string, unknown>; | ||
|
||
constructor() { | ||
this.storage = new Map(); | ||
} | ||
|
||
has(key: string): boolean { | ||
return this.storage.has(key); | ||
} | ||
|
||
getItem<T>(key: string): T | null { | ||
return this.storage.has(key) ? this.storage.get(key) as T : null; | ||
} | ||
|
||
setItem(key: string, value: unknown): void { | ||
this.storage.set(key, value); | ||
} | ||
|
||
removeItem(key: string): void { | ||
this.storage.delete(key); | ||
} | ||
|
||
clear(): void { | ||
this.storage.clear(); | ||
} | ||
|
||
get size(): number { | ||
return this.storage.size; | ||
} | ||
} | ||
|
||
export function createMemoryStorage(): MemoryStorage { | ||
return new MemoryStorageImpl(); | ||
} | ||
|
||
export const defaultMemoryStorage: MemoryStorage = createMemoryStorage(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters