|
|
|
@ -1,10 +1,10 @@ |
|
|
|
|
import { sleep } from '../utils/eventLoop'; |
|
|
|
|
import { StringifiableValue, createSerializer } from '../utils/serializer'; |
|
|
|
|
import { ClearableKeyValueStorage } from './types'; |
|
|
|
|
import { type StringifiableValue, createSerializer } from '../utils/serializer'; |
|
|
|
|
import type { ClearableKeyValueStorage } from './types'; |
|
|
|
|
|
|
|
|
|
export const createKeyValueStorage = <TValue extends StringifiableValue>( |
|
|
|
|
latencyMs: number, |
|
|
|
|
) => { |
|
|
|
|
): ClearableKeyValueStorage<string, TValue> => { |
|
|
|
|
const withSimulatedLatency = async <TResult>( |
|
|
|
|
f: () => Promise<TResult>, |
|
|
|
|
): Promise<TResult> => { |
|
|
|
@ -17,7 +17,7 @@ export const createKeyValueStorage = <TValue extends StringifiableValue>( |
|
|
|
|
const serializer = createSerializer<TValue>(); |
|
|
|
|
const storage = new Map<string, string>(); |
|
|
|
|
return { |
|
|
|
|
get: (key: string) => |
|
|
|
|
get: (key) => |
|
|
|
|
withSimulatedLatency(() => |
|
|
|
|
Promise.resolve( |
|
|
|
|
storage.has(key) |
|
|
|
@ -32,7 +32,7 @@ export const createKeyValueStorage = <TValue extends StringifiableValue>( |
|
|
|
|
}, |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
set: (key: string, value: TValue) => |
|
|
|
|
set: (key, value) => |
|
|
|
|
withSimulatedLatency(() => |
|
|
|
|
Promise.resolve( |
|
|
|
|
void storage.set(key, serializer.stringify(value)), |
|
|
|
@ -41,5 +41,5 @@ export const createKeyValueStorage = <TValue extends StringifiableValue>( |
|
|
|
|
clear: () => |
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
|
|
|
|
|
withSimulatedLatency(() => Promise.resolve(storage.clear())), |
|
|
|
|
} as ClearableKeyValueStorage<string, TValue>; |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|