-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eslint: fix errors in contexts/storage.ts
Signed-off-by: Dennis Francis <dennisfrancis.in@gmail.com>
- Loading branch information
1 parent
68b9a29
commit c70aaee
Showing
1 changed file
with
73 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,105 @@ | ||
/* eslint-disable */ | ||
import React from 'react' | ||
import React from 'react'; | ||
import { Item } from '../types/item'; | ||
import { ShoppingDatabase } from '../storage/storageDefs'; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
type ListenerType = (x: Item[]) => void; | ||
|
||
export class StorageType { | ||
private db: ShoppingDatabase | undefined = undefined; | ||
private listeners = new Set<((x: Item[]) => void)>(); | ||
constructor(db?: ShoppingDatabase) { | ||
this.db = db; | ||
this.invokeListeners = this.invokeListeners.bind(this); | ||
} | ||
private db: ShoppingDatabase | undefined = undefined; | ||
|
||
public setDB(db: ShoppingDatabase) { | ||
this.db = db; | ||
} | ||
private listeners = new Set<ListenerType>(); | ||
|
||
public hasDB(): boolean { | ||
return !!this.db; | ||
} | ||
constructor(db?: ShoppingDatabase) { | ||
this.db = db; | ||
this.invokeListeners = this.invokeListeners.bind(this); | ||
} | ||
|
||
private invokeListeners(items: Item[]) { | ||
// console.log('Invoking ' + this.masterListeners.size + ' master listeners.'); | ||
this.listeners.forEach((listener) => { | ||
listener(items); | ||
}); | ||
} | ||
public setDB(db: ShoppingDatabase): void { | ||
this.db = db; | ||
} | ||
|
||
public fetch() { | ||
if (!this.db) | ||
return; | ||
public hasDB(): boolean { | ||
return !!this.db; | ||
} | ||
|
||
this.db.getAllItems().then(this.invokeListeners); | ||
} | ||
private invokeListeners(items: Item[]): void { | ||
// console.log('Invoking ' + this.masterListeners.size + ' master listeners.'); | ||
this.listeners.forEach((listener) => { | ||
listener(items); | ||
}); | ||
} | ||
|
||
public addListener(listener: (x: Item[]) => void) { | ||
this.listeners.add(listener); | ||
public fetch(): void { | ||
if (!this.db) { | ||
return; | ||
} | ||
|
||
public removeListener(listener: (x: Item[]) => void) { | ||
this.listeners.delete(listener); | ||
} | ||
this.db.getAllItems().then(this.invokeListeners); | ||
} | ||
|
||
public addUpdate(item: Item) { | ||
if (!this.db) | ||
return; | ||
public addListener(listener: ListenerType): void { | ||
this.listeners.add(listener); | ||
} | ||
|
||
this.db.addUpdateItem(item); | ||
public removeListener(listener: ListenerType): void { | ||
this.listeners.delete(listener); | ||
} | ||
|
||
public addUpdate(item: Item): void { | ||
if (!this.db) { | ||
return; | ||
} | ||
|
||
public delete(item: Item) { | ||
if (!this.db) | ||
return; | ||
this.db.addUpdateItem(item); | ||
} | ||
|
||
this.db.deleteItem(item); | ||
public delete(item: Item): void { | ||
if (!this.db) { | ||
return; | ||
} | ||
|
||
public clearUnsaved() { | ||
if (!this.db) | ||
return; | ||
this.db.deleteItem(item); | ||
} | ||
|
||
return this.db.clearUnsaved(); | ||
public clearUnsaved(): Promise<undefined> { | ||
if (!this.db) { | ||
return new Promise<undefined>(() => undefined); | ||
} | ||
|
||
public saveUnsaved(saveDate: Date) { | ||
if (!this.db) | ||
return; | ||
return this.db.clearUnsaved(); | ||
} | ||
|
||
return this.db.saveUnsaved(saveDate); | ||
public saveUnsaved(saveDate: Date): Promise<undefined> { | ||
if (!this.db) { | ||
return new Promise<undefined>(() => undefined); | ||
} | ||
|
||
public async exportToJSONText() { | ||
let obj; | ||
try { | ||
obj = await this.db?.exportToJSON(); | ||
} catch (e) { | ||
return ''; | ||
} | ||
return this.db.saveUnsaved(saveDate); | ||
} | ||
|
||
return JSON.stringify(obj); | ||
public async exportToJSONText(): Promise<string> { | ||
let obj; | ||
try { | ||
obj = await this.db?.exportToJSON(); | ||
} catch (e) { | ||
return ''; | ||
} | ||
|
||
public async importFromJSONText(jsonText: string) { | ||
let res = false; | ||
try { | ||
res = !! await this.db?.importFromJSON(jsonText); | ||
} catch (e) { | ||
return false; | ||
} | ||
return JSON.stringify(obj); | ||
} | ||
|
||
return res; | ||
public async importFromJSONText(jsonText: string): Promise<boolean> { | ||
let res = false; | ||
try { | ||
res = !!await this.db?.importFromJSON(jsonText); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
return res; | ||
} | ||
} | ||
|
||
export const StorageContext = React.createContext({} as StorageType); | ||
export const StorageProvider = StorageContext.Provider; | ||
export const StorageProvider = StorageContext.Provider; |