-
Notifications
You must be signed in to change notification settings - Fork 1
/
Repository.ts
40 lines (32 loc) · 1.01 KB
/
Repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Document } from './Storage'
import { debounce, DebounceFn } from './utils'
export interface DB {
getKeys: () => Promise<string[]>
getDocument: (key: string) => Promise<Document>
getAllDocuments: () => Promise<Document[]>
setDocument: (document: Document) => Promise<void>
setDocuments: (documents: Document[]) => Promise<void>
}
class Repository {
db: DB
debounces: Map<string, DebounceFn>
constructor(db: DB) {
this.db = db
this.debounces = new Map<string, DebounceFn>()
}
async getKeys(): Promise<string[]> {
return await this.db.getKeys()
}
async getDocument(key: string): Promise<Document> {
return await this.db.getDocument(key)
}
async getAllDocuments(): Promise<Document[]> {
return await this.db.getAllDocuments()
}
setDocument(document: Document) {
const currentDebounce = this.debounces.get(document.key) ?? debounce(this.db.setDocument)
this.debounces.set(document.key, currentDebounce)
currentDebounce(document)
}
}
export default Repository