Store settings in lowdb
This commit is contained in:
@@ -1,21 +1,80 @@
|
||||
import { rendererEvents } from '../../events'
|
||||
import { v4 } from 'uuid'
|
||||
|
||||
import {
|
||||
storageStoreEvent,
|
||||
makeStorageResponseEvent,
|
||||
storageLoadEvent,
|
||||
storageClearEvent,
|
||||
makeStorageAcknoledgementEvent,
|
||||
} from '../../events/StorageEvents'
|
||||
|
||||
export interface StorageIdentifier<Model> {
|
||||
id: string
|
||||
}
|
||||
|
||||
export interface PersistantStorage {
|
||||
store<Model>(identifier: StorageIdentifier<Model>, data: Model): void
|
||||
load<Model>(identifier: StorageIdentifier<Model>): Model | undefined
|
||||
store<Model>(identifier: StorageIdentifier<Model>, data: Model): Promise<void>
|
||||
load<Model>(identifier: StorageIdentifier<Model>): Promise<Model | undefined>
|
||||
clear(): Promise<void>
|
||||
}
|
||||
|
||||
class LocalStorage implements PersistantStorage {
|
||||
public store<Model>(identifier: StorageIdentifier<Model>, data: Model) {
|
||||
localStorage.setItem(identifier.id, JSON.stringify(data))
|
||||
class RemoteStorage implements PersistantStorage {
|
||||
private timeoutCallback(event: any, callback: any, reject: any) {
|
||||
setTimeout(() => {
|
||||
reject('remote storage timeout')
|
||||
rendererEvents.unsubscribe(event, callback)
|
||||
}, 10000)
|
||||
}
|
||||
|
||||
public load<Model>(identifier: StorageIdentifier<Model>): Model | undefined {
|
||||
const data = localStorage.getItem(identifier.id)
|
||||
return data && JSON.parse(data)
|
||||
private expectAck(transactionId: string): Promise<void> {
|
||||
const ack = makeStorageAcknoledgementEvent(transactionId)
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const callback = () => {
|
||||
resolve()
|
||||
rendererEvents.unsubscribe(ack, callback)
|
||||
}
|
||||
rendererEvents.subscribe(ack, callback)
|
||||
this.timeoutCallback(ack, callback, reject)
|
||||
})
|
||||
}
|
||||
|
||||
public store<Model>(identifier: StorageIdentifier<Model>, data: Model): Promise<void> {
|
||||
const transactionId = v4()
|
||||
const expectation = this.expectAck(transactionId)
|
||||
rendererEvents.emit(storageStoreEvent, { data, transactionId, store: identifier.id })
|
||||
return expectation
|
||||
}
|
||||
|
||||
public load<Model>(identifier: StorageIdentifier<Model>): Promise<Model | undefined> {
|
||||
const transactionId = v4()
|
||||
const responseEvent = makeStorageResponseEvent(transactionId)
|
||||
|
||||
const promise = new Promise<Model>((resolve, reject) => {
|
||||
const callback = (msg: any) => {
|
||||
const data = msg.data && JSON.parse(msg.data)
|
||||
resolve(data)
|
||||
rendererEvents.unsubscribe(responseEvent, callback)
|
||||
}
|
||||
rendererEvents.subscribe(responseEvent, callback)
|
||||
this.timeoutCallback(responseEvent, callback, reject)
|
||||
})
|
||||
|
||||
rendererEvents.emit(storageLoadEvent, {
|
||||
transactionId,
|
||||
store: identifier.id,
|
||||
})
|
||||
|
||||
return promise
|
||||
}
|
||||
|
||||
public clear(): Promise<void> {
|
||||
const transactionId = v4()
|
||||
const expectation = this.expectAck(transactionId)
|
||||
|
||||
rendererEvents.emit(storageClearEvent, { transactionId })
|
||||
return expectation
|
||||
}
|
||||
}
|
||||
|
||||
export default new LocalStorage()
|
||||
export default new RemoteStorage()
|
||||
|
||||
@@ -12,9 +12,9 @@ const storedConnectionsIdentifier: StorageIdentifier<{[s: string]: ConnectionOpt
|
||||
id: 'ConnectionManager_connections',
|
||||
}
|
||||
|
||||
export const loadConnectionSettings = () => (dispatch: Dispatch<any>, getState: () => AppState) => {
|
||||
ensureConnectionsHaveBeenInitialized()
|
||||
const connections = persistantStorage.load(storedConnectionsIdentifier)
|
||||
export const loadConnectionSettings = () => async (dispatch: Dispatch<any>, getState: () => AppState) => {
|
||||
await ensureConnectionsHaveBeenInitialized()
|
||||
const connections = await persistantStorage.load(storedConnectionsIdentifier)
|
||||
|
||||
if (!connections) {
|
||||
return
|
||||
@@ -92,9 +92,8 @@ export const deleteConnection = (connectionId: string) => (dispatch: Dispatch<an
|
||||
}
|
||||
}
|
||||
|
||||
function ensureConnectionsHaveBeenInitialized() {
|
||||
const connections = persistantStorage.load(storedConnectionsIdentifier)
|
||||
|
||||
async function ensureConnectionsHaveBeenInitialized() {
|
||||
const connections = await persistantStorage.load(storedConnectionsIdentifier)
|
||||
const requiresInitialization = !connections
|
||||
if (requiresInitialization) {
|
||||
const migratedConnection = loadLegacyConnectionOptions()
|
||||
|
||||
@@ -315,7 +315,6 @@ class ConnectionSettings extends React.Component<Props, State> {
|
||||
|
||||
const mqttOptions = toMqttConnection(this.props.connection)
|
||||
if (mqttOptions) {
|
||||
console.log(mqttOptions)
|
||||
this.props.actions.connect(mqttOptions, this.props.connection.id)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user