Refactor connection reducer & tree

This commit is contained in:
Thomas Nordquist
2019-01-20 22:34:15 +01:00
parent ecd3a23311
commit ba4efbe30d
10 changed files with 163 additions and 107 deletions

View File

@@ -1,7 +1,29 @@
import { Edge, TreeNode } from './'
import { EventBusInterface, makeConnectionMessageEvent, MqttMessage } from '../../../events'
import { TreeNodeFactory } from './TreeNodeFactory'
export class Tree extends TreeNode {
private connectionId?: string
private updateSource?: EventBusInterface
constructor() {
super(undefined, undefined)
}
public updateWithConnection(emitter: EventBusInterface, connectionId: string) {
this.updateSource = emitter
this.updateSource.subscribe(makeConnectionMessageEvent(connectionId), this.handleNewData)
}
private handleNewData = (msg: MqttMessage) => {
const edges = msg.topic.split('/')
const node = TreeNodeFactory.fromEdgesAndValue(edges, msg.payload)
node.mqttMessage = msg
this.updateWithNode(node.firstNode())
}
public stopUpdating() {
if (this.updateSource && this.connectionId) {
this.updateSource.unsubscribeAll(makeConnectionMessageEvent(this.connectionId))
}
}
}