|
| 1 | +import { AsyncStorage, StorageKeys, StorageModel } from "../src"; |
| 2 | +import { ExampleExtension, MyExampleExtension } from "./ExampleExtension"; |
| 3 | + |
| 4 | +type MyModel = StorageModel<{ |
| 5 | + age: number; |
| 6 | + name: string; |
| 7 | + likes: boolean[]; |
| 8 | +}>; |
| 9 | + |
| 10 | +// @ts-ignore |
| 11 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 12 | +class ExampleStorage implements AsyncStorage<MyModel, MyExampleExtension> { |
| 13 | + private storage: MyModel = { |
| 14 | + age: null, |
| 15 | + name: null, |
| 16 | + likes: null, |
| 17 | + }; |
| 18 | + |
| 19 | + getItem = async <K extends keyof MyModel>(key: K): Promise<MyModel[K]> => { |
| 20 | + return this.storage[key]; |
| 21 | + }; |
| 22 | + |
| 23 | + setItem = async <K extends StorageKeys<MyModel>>( |
| 24 | + key: K, |
| 25 | + value: MyModel[K] |
| 26 | + ): Promise<void> => { |
| 27 | + this.storage[key] = value; |
| 28 | + }; |
| 29 | + |
| 30 | + removeItem = async <K extends StorageKeys<MyModel>>( |
| 31 | + key: K |
| 32 | + ): Promise<void> => { |
| 33 | + this.storage[key] = null; |
| 34 | + }; |
| 35 | + |
| 36 | + getMany = async <K extends StorageKeys<MyModel>>( |
| 37 | + keys: K[] |
| 38 | + ): Promise<{ [k in K]: MyModel[k] }> => { |
| 39 | + return keys.reduce((entries, key) => { |
| 40 | + return { |
| 41 | + ...entries, |
| 42 | + [key]: this.storage[key] ?? null, |
| 43 | + }; |
| 44 | + }, {} as { [k in K]: MyModel[k] }); |
| 45 | + }; |
| 46 | + |
| 47 | + setMany = async <K extends StorageKeys<MyModel>>(entries: { |
| 48 | + [k in K]: MyModel[k]; |
| 49 | + }): Promise<void> => { |
| 50 | + Object.entries(entries).forEach((entry) => { |
| 51 | + const key = entry[0] as K; |
| 52 | + this.storage[key] = entry[1] as MyModel[K]; |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + removeMany = async <K extends StorageKeys<MyModel>>( |
| 57 | + keys: K[] |
| 58 | + ): Promise<void> => { |
| 59 | + keys.forEach((k) => { |
| 60 | + this.storage[k] = null; |
| 61 | + }); |
| 62 | + }; |
| 63 | + |
| 64 | + clear = async (): Promise<void> => { |
| 65 | + this.storage.age = null; |
| 66 | + this.storage.name = null; |
| 67 | + this.storage.likes = null; |
| 68 | + }; |
| 69 | + |
| 70 | + ext: MyExampleExtension = new ExampleExtension(); |
| 71 | +} |
0 commit comments