Update code formatting

This commit is contained in:
Thomas Nordquist
2019-06-15 14:56:57 +02:00
parent 6176859c7c
commit 92e045297e
115 changed files with 2988 additions and 1042 deletions

View File

@@ -8,8 +8,8 @@ import {
makeStorageResponseEvent,
storageClearEvent,
storageLoadEvent,
storageStoreEvent
} from '../../events/StorageEvents'
storageStoreEvent,
} from '../../events/StorageEvents'
export default class ConfigStorage {
private file: string
@@ -32,31 +32,43 @@ export default class ConfigStorage {
}
public async init() {
backendEvents.subscribe(storageStoreEvent, async (event) => {
backendEvents.subscribe(storageStoreEvent, async event => {
const ack = makeStorageAcknowledgementEvent(event.transactionId)
try {
const db = await this.getDb()
await db.set(event.store, event.data).write()
backendEvents.emit(ack, undefined)
} catch (error) {
backendEvents.emit(ack, { error, transactionId: event.transactionId, store: event.store })
backendEvents.emit(ack, {
error,
transactionId: event.transactionId,
store: event.store,
})
throw error
}
})
backendEvents.subscribe(storageLoadEvent, async (event) => {
backendEvents.subscribe(storageLoadEvent, async event => {
const responseEvent = makeStorageResponseEvent(event.transactionId)
try {
const db = await this.getDb()
const data = await db.get(event.store).value()
backendEvents.emit(responseEvent, { data, transactionId: event.transactionId, store: event.store })
backendEvents.emit(responseEvent, {
data,
transactionId: event.transactionId,
store: event.store,
})
} catch (error) {
backendEvents.emit(responseEvent, { error, transactionId: event.transactionId, store: event.store })
backendEvents.emit(responseEvent, {
error,
transactionId: event.transactionId,
store: event.store,
})
throw error
}
})
backendEvents.subscribe(storageClearEvent, async (event) => {
backendEvents.subscribe(storageClearEvent, async event => {
try {
const db = await this.getDb()
const keys = await db.keys().value()
@@ -65,7 +77,10 @@ export default class ConfigStorage {
}
backendEvents.emit(makeStorageAcknowledgementEvent(event.transactionId), undefined)
} catch (error) {
backendEvents.emit(makeStorageAcknowledgementEvent(event.transactionId), { error, transactionId: event.transactionId })
backendEvents.emit(makeStorageAcknowledgementEvent(event.transactionId), {
error,
transactionId: event.transactionId,
})
throw error
}
})

View File

@@ -68,7 +68,7 @@ export class MqttSource implements DataSource<MqttOptions> {
client.on('connect', () => {
this.stateMachine.setConnected(true)
options.subscriptions.forEach((subscription) => {
options.subscriptions.forEach(subscription => {
client.subscribe(subscription, (err: Error) => {
if (err) {
this.stateMachine.setError(err)
@@ -86,10 +86,10 @@ export class MqttSource implements DataSource<MqttOptions> {
public publish(msg: MqttMessage) {
if (this.client) {
this.client.publish(
msg.topic,
msg.payload ? Base64Message.toUnicodeString(msg.payload) : '',
{ qos: msg.qos, retain: msg.retain })
this.client.publish(msg.topic, msg.payload ? Base64Message.toUnicodeString(msg.payload) : '', {
qos: msg.qos,
retain: msg.retain,
})
}
}

View File

@@ -59,16 +59,20 @@ interface JsonAstArray {
function jsonToPropertyPaths(ast: JsonAst, previousPath: Array<string> = []): Array<JsonPropertyLocation> {
let children: Array<Array<JsonPropertyLocation>> = []
if (ast.type === 'Literal') {
return [{
value: ast.value,
path: previousPath.join('.'),
line: ast.loc.start.line,
column: ast.loc.start.column,
}]
return [
{
value: ast.value,
path: previousPath.join('.'),
line: ast.loc.start.line,
column: ast.loc.start.column,
},
]
} else if (ast.type === 'Array') {
children = ast.children.map((value, idx) => jsonToPropertyPaths(value, previousPath.slice().concat([String(idx)])))
} else if (ast.type === 'Object') {
children = ast.children.map(property => jsonToPropertyPaths(property.value, previousPath.slice().concat([property.key.value])))
children = ast.children.map(property =>
jsonToPropertyPaths(property.value, previousPath.slice().concat([property.key.value]))
)
}
return children.reduce((a, b) => a.concat(b), [])

View File

@@ -26,7 +26,7 @@ export class RingBuffer<T extends Lengthwise> {
if (remainingSize < 0) {
this.freeSomeSpace(Math.abs(remainingSize))
}
while ((this.end - this.start) >= this.maxItems) {
while (this.end - this.start >= this.maxItems) {
this.dropFirst()
}
}

View File

@@ -1,11 +1,6 @@
import { ChangeBuffer } from './ChangeBuffer'
import { Destroyable } from './Destroyable'
import {
EventBusInterface,
EventDispatcher,
makeConnectionMessageEvent,
MqttMessage
} from '../../../events'
import { EventBusInterface, EventDispatcher, makeConnectionMessageEvent, MqttMessage } from '../../../events'
import { TreeNode } from './'
import { TreeNodeFactory } from './TreeNodeFactory'
@@ -35,7 +30,11 @@ export class Tree<ViewModel extends Destroyable> extends TreeNode<ViewModel> {
this.didReceive.removeAllListeners()
}
public updateWithConnection(emitter: EventBusInterface, connectionId: string, nodeFilter?: (node: TreeNode<ViewModel>) => boolean) {
public updateWithConnection(
emitter: EventBusInterface,
connectionId: string,
nodeFilter?: (node: TreeNode<ViewModel>) => boolean
) {
this.updateSource = emitter
this.connectionId = connectionId
this.nodeFilter = nodeFilter
@@ -49,7 +48,7 @@ export class Tree<ViewModel extends Destroyable> extends TreeNode<ViewModel> {
}
public applyUnmergedChanges() {
this.unmergedMessages.popAll().forEach((msg) => {
this.unmergedMessages.popAll().forEach(msg => {
const edges = msg.topic.split('/')
const node = TreeNodeFactory.fromEdgesAndValue<ViewModel>(edges, msg.payload)
node.mqttMessage = msg

View File

@@ -8,7 +8,7 @@ export class TreeNode<ViewModel extends Destroyable> {
public mqttMessage?: MqttMessage
public messageHistory: MessageHistory = new RingBuffer<Message>(20000, 100)
public viewModel?: ViewModel
public edges: {[s: string]: Edge<ViewModel>} = {}
public edges: { [s: string]: Edge<ViewModel> } = {}
public edgeArray: Array<Edge<ViewModel>> = []
public collapsed = false
public messages: number = 0
@@ -52,7 +52,7 @@ export class TreeNode<ViewModel extends Destroyable> {
}
private isTopicEmptyLeaf() {
const hasNoMessage = (!this.message || !this.message.value || this.message.value.length === 0)
const hasNoMessage = !this.message || !this.message.value || this.message.value.length === 0
return hasNoMessage && this.isLeaf()
}
@@ -135,7 +135,7 @@ export class TreeNode<ViewModel extends Destroyable> {
}
public hash(): string {
return `N${(this.sourceEdge ? this.sourceEdge.hash() : '')}`
return `N${this.sourceEdge ? this.sourceEdge.hash() : ''}`
}
public firstNode(): TreeNode<ViewModel> {
@@ -145,7 +145,7 @@ export class TreeNode<ViewModel extends Destroyable> {
public path(): string {
if (!this.cachedPath) {
return this.branch()
.map(node => (node.sourceEdge && node.sourceEdge.name))
.map(node => node.sourceEdge && node.sourceEdge.name)
.filter(name => name !== undefined)
.join('/')
}
@@ -199,9 +199,8 @@ export class TreeNode<ViewModel extends Destroyable> {
public leafMessageCount(): number {
if (this.cachedLeafMessageCount === undefined) {
this.cachedLeafMessageCount = this.edgeArray
.map(edge => edge.target.leafMessageCount())
.reduce((a, b) => a + b, 0) + this.messages
this.cachedLeafMessageCount =
this.edgeArray.map(edge => edge.target.leafMessageCount()).reduce((a, b) => a + b, 0) + this.messages
}
return this.cachedLeafMessageCount as number

View File

@@ -4,7 +4,10 @@ import { Edge, Tree, TreeNode } from './'
export abstract class TreeNodeFactory {
private static messageCounter = 0
public static insertNodeAtPosition<ViewModel extends Destroyable>(edgeNames: Array<string>, node: TreeNode<ViewModel>) {
public static insertNodeAtPosition<ViewModel extends Destroyable>(
edgeNames: Array<string>,
node: TreeNode<ViewModel>
) {
let currentNode: TreeNode<ViewModel> = new Tree()
let edge
for (const edgeName of edgeNames) {
@@ -17,7 +20,10 @@ export abstract class TreeNodeFactory {
node.sourceEdge!.target = node
}
public static fromEdgesAndValue<ViewModel extends Destroyable>(edgeNames: Array<string>, value?: Base64Message | null): TreeNode<ViewModel> {
public static fromEdgesAndValue<ViewModel extends Destroyable>(
edgeNames: Array<string>,
value?: Base64Message | null
): TreeNode<ViewModel> {
const node = new TreeNode<ViewModel>()
node.setMessage({
value: value || undefined,

View File

@@ -42,7 +42,7 @@ describe('Edge', () => {
const topics2 = 'foo/foo/baz'.split('/')
const bazEdge2 = TreeNodeFactory.fromEdgesAndValue(topics2, undefined).sourceEdge
if (!bazEdge1 || !bazEdge2) {
if (!bazEdge1 || !bazEdge2) {
throw Error('should not happen')
}

View File

@@ -8,8 +8,8 @@ describe('EventDispatcher', async () => {
this.timeout(300)
setTimeout(() => dispatcher.dispatch('hello'), 5)
const response = await new Promise((resolve) => {
dispatcher.subscribe((msg) => {
const response = await new Promise(resolve => {
dispatcher.subscribe(msg => {
resolve(msg)
})
})

View File

@@ -1,12 +1,12 @@
import 'mocha'
import { TreeNodeFactory } from '../'
import { TreeNodeFactory } from '../'
import { expect } from 'chai'
import { Base64Message } from '../Base64Message';
import { Base64Message } from '../Base64Message'
describe('TreeNode', () => {
const number3 = Base64Message.fromString("3")
const number5 = Base64Message.fromString("5")
const number3 = Base64Message.fromString('3')
const number5 = Base64Message.fromString('5')
it('firstNode should retrieve first node', () => {
const topics = 'foo/bar'.split('/')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, undefined)

View File

@@ -2,7 +2,7 @@ import 'mocha'
import { TreeNodeFactory } from '../'
import { expect } from 'chai'
import { Base64Message } from '../Base64Message';
import { Base64Message } from '../Base64Message'
describe('TreeNodeFactory', () => {
it('root node must not have a sourceEdge', () => {
@@ -45,7 +45,7 @@ describe('TreeNodeFactory', () => {
expect(node.sourceEdge.name).to.eq('baz')
const barNode = node.sourceEdge.source
if (!barNode || !barNode.sourceEdge) {
if (!barNode || !barNode.sourceEdge) {
expect.fail('should not fail')
return
}

View File

@@ -12,7 +12,7 @@ import {
} from '../../events'
export class ConnectionManager {
private connections: {[s: string]: DataSource<any>} = {}
private connections: { [s: string]: DataSource<any> } = {}
private handleConnectionRequest = (event: AddMqttConnection) => {
const connectionId = event.id
@@ -27,7 +27,7 @@ export class ConnectionManager {
this.connections[connectionId] = connection
const connectionStateEvent = makeConnectionStateEvent(connectionId)
connection.stateMachine.onUpdate.subscribe((state) => {
connection.stateMachine.onUpdate.subscribe(state => {
backendEvents.emit(connectionStateEvent, state)
})
@@ -46,7 +46,12 @@ export class ConnectionManager {
buffer = buffer.slice(0, 20000)
}
backendEvents.emit(messageEvent, { topic, payload: Base64Message.fromBuffer(buffer), qos: packet.qos, retain: packet.retain })
backendEvents.emit(messageEvent, {
topic,
payload: Base64Message.fromBuffer(buffer),
qos: packet.qos,
retain: packet.retain,
})
})
}
@@ -68,7 +73,6 @@ export class ConnectionManager {
}
public closeAllConnections() {
Object.keys(this.connections)
.forEach(conenctionId => this.removeConnection(conenctionId))
Object.keys(this.connections).forEach(conenctionId => this.removeConnection(conenctionId))
}
}

View File

@@ -26,16 +26,11 @@ describe('access JSON values via dot property paths', () => {
expect(result[0].path).to.eq('foo.bar')
expect(result[0].line).to.eq(3)
expect(dotProp.get(data, result[0].path)).to.eq(4)
})
it('array path', () => {
const data = {
foo: [
1,
2,
3,
],
foo: [1, 2, 3],
}
const result = parseJson(JSON.stringify(data, undefined, 2))
@@ -48,7 +43,7 @@ describe('access JSON values via dot property paths', () => {
it('should fail parsing invalid json', () => {
expect(() => {
const result = parseJson("BLE2MQTT-8C48")
const result = parseJson('BLE2MQTT-8C48')
}).to.throw()
})
})