Fix emitter leaks, style, tree swaps

This commit is contained in:
Thomas Nordquist
2019-01-22 12:17:59 +01:00
parent 221abd8c48
commit d4ce58a8ec
22 changed files with 411 additions and 164 deletions

View File

@@ -18,7 +18,14 @@ export class Edge implements Hashable {
public hash(): string {
if (!this.cachedHash) {
const previousHash = (this.source && this.source.sourceEdge) ? this.source.sourceEdge.hash() : ''
let previousHash
if (this.source && this.source.sourceEdge) {
previousHash = this.source.sourceEdge.hash()
} else {
// Use the tree hash to distinguish between different trees
previousHash = this.source && this.source.isTree ? (this.source as any).hash() : ''
}
this.cachedHash = `H${sha1(previousHash + this.name)}`
}

View File

@@ -3,27 +3,45 @@ import { EventBusInterface, makeConnectionMessageEvent, MqttMessage } from '../.
import { TreeNodeFactory } from './TreeNodeFactory'
export class Tree extends TreeNode {
private connectionId?: string
private updateSource?: EventBusInterface
public connectionId?: string
public updateSource?: EventBusInterface
public nodeFilter?: (node: TreeNode) => boolean
private subscriptionEvent?: any
public isTree = true
private cachedHash = `${Math.random()}`
constructor() {
super(undefined, undefined)
}
public updateWithConnection(emitter: EventBusInterface, connectionId: string) {
public updateWithConnection(emitter: EventBusInterface, connectionId: string, nodeFilter?:(node: TreeNode) => boolean) {
this.updateSource = emitter
this.updateSource.subscribe(makeConnectionMessageEvent(connectionId), this.handleNewData)
this.connectionId = connectionId
this.nodeFilter = nodeFilter
this.subscriptionEvent = makeConnectionMessageEvent(connectionId)
this.updateSource.subscribe(this.subscriptionEvent, this.handleNewData)
}
public hash() {
return this.cachedHash
}
private handleNewData = (msg: MqttMessage) => {
const edges = msg.topic.split('/')
const node = TreeNodeFactory.fromEdgesAndValue(edges, msg.payload)
node.mqttMessage = msg
if (this.nodeFilter && !this.nodeFilter(node)) {
return
}
this.updateWithNode(node.firstNode())
}
public stopUpdating() {
if (this.updateSource && this.connectionId) {
this.updateSource.unsubscribeAll(makeConnectionMessageEvent(this.connectionId))
if (this.subscriptionEvent && this.updateSource) {
console.log(this.updateSource.ipc)
this.updateSource.unsubscribe(this.subscriptionEvent, this.handleNewData)
}
}
}

View File

@@ -13,6 +13,7 @@ export class TreeNode {
public onMerge = new EventDispatcher<void, TreeNode>(this)
public onEdgesChange = new EventDispatcher<void, TreeNode>(this)
public onMessage = new EventDispatcher<Message, TreeNode>(this)
public isTree = false
private cachedLeafes?: TreeNode[]
private cachedLeafMessageCount?: number