|
1 | 1 | // Wrap level.get and level.put to parse and stringify JSON values. |
2 | 2 | export default class Store { |
3 | | - private readonly db: Level |
4 | | - private readonly key: string |
| 3 | + private readonly db: Level; |
| 4 | + private readonly key: string; |
5 | 5 | constructor(db: Level, key: string) { |
6 | | - this.db = db |
7 | | - this.key = key |
| 6 | + this.db = db; |
| 7 | + this.key = key; |
8 | 8 | } |
9 | 9 |
|
10 | | - async getDB () { |
11 | | - let dbState: { [tcrAddress: string]: {[itemID: string]: number} } = {} |
| 10 | + async getDB() { |
| 11 | + let dbState: { [tcrAddress: string]: { [itemID: string]: number } } = {}; |
12 | 12 | try { |
13 | | - dbState = JSON.parse(await this.db.get(this.key)) |
| 13 | + dbState = JSON.parse(await this.db.get(this.key)); |
14 | 14 | } catch (err) { |
15 | | - if (err.type !== 'NotFoundError') throw new Error(err) |
| 15 | + if (err.type !== "NotFoundError") throw new Error(err); |
16 | 16 | // No-op, we just return an empty object which will be |
17 | 17 | // saved on put. |
18 | 18 | } |
19 | | - return dbState |
| 19 | + return dbState; |
20 | 20 | } |
21 | 21 |
|
22 | | - async addToWatchlist (tcrAddress: string, itemID: string, challengePeriodEnd: number) { |
23 | | - const tcrState = await this.getTCR(tcrAddress) |
24 | | - tcrState[itemID] = challengePeriodEnd |
25 | | - await this.putTCR(tcrAddress, tcrState) |
| 22 | + async addToWatchlist( |
| 23 | + tcrAddress: string, |
| 24 | + itemID: string, |
| 25 | + challengePeriodEnd: number |
| 26 | + ) { |
| 27 | + const tcrState = await this.getTCR(tcrAddress); |
| 28 | + tcrState[itemID] = challengePeriodEnd; |
| 29 | + await this.putTCR(tcrAddress, tcrState); |
26 | 30 | } |
27 | 31 |
|
28 | | - async removeFromWatchlist (tcrAddress: string, itemID: string) { |
29 | | - const tcrState = await this.getTCR(tcrAddress) |
30 | | - delete tcrState[itemID] |
31 | | - await this.putTCR(tcrAddress, tcrState) |
| 32 | + async removeFromWatchlist(tcrAddress: string, itemID: string) { |
| 33 | + const tcrState = await this.getTCR(tcrAddress); |
| 34 | + delete tcrState[itemID]; |
| 35 | + await this.putTCR(tcrAddress, tcrState); |
32 | 36 | } |
33 | 37 |
|
34 | | - private async putDB (dbState: object) { this.db.put(this.key, JSON.stringify(dbState)) } |
35 | | - |
36 | | - private async getTCR (tcrAddress: string){ |
37 | | - const dbState: { [tcrAddress: string]: { [itemID: string]: number} } = await this.getDB() |
38 | | - return dbState[tcrAddress] || {} |
| 38 | + private async putDB(dbState: object) { |
| 39 | + this.db.put(this.key, JSON.stringify(dbState)); |
39 | 40 | } |
40 | 41 |
|
41 | | - private async putTCR (tcrAddress: string, tcrState: {[tcrAddress: string]: number}) { |
42 | | - const dbState = await this.getDB() |
43 | | - dbState[tcrAddress] = tcrState |
44 | | - await this.putDB(dbState) |
| 42 | + private async getTCR(tcrAddress: string) { |
| 43 | + const dbState: { [tcrAddress: string]: { [itemID: string]: number } } = |
| 44 | + await this.getDB(); |
| 45 | + return dbState[tcrAddress] || {}; |
45 | 46 | } |
46 | 47 |
|
| 48 | + private async putTCR( |
| 49 | + tcrAddress: string, |
| 50 | + tcrState: { [tcrAddress: string]: number } |
| 51 | + ) { |
| 52 | + const dbState = await this.getDB(); |
| 53 | + dbState[tcrAddress] = tcrState; |
| 54 | + await this.putDB(dbState); |
| 55 | + } |
47 | 56 | } |
0 commit comments