Refactor
This commit is contained in:
@@ -7,7 +7,7 @@ export interface DataSourceState {
|
||||
}
|
||||
|
||||
export class DataSourceStateMachine {
|
||||
public onUpdate = new EventDispatcher<DataSourceState, DataSourceStateMachine>()
|
||||
public onUpdate = new EventDispatcher<DataSourceState>()
|
||||
private state: DataSourceState = {
|
||||
error: undefined,
|
||||
connected: false,
|
||||
|
||||
@@ -12,7 +12,7 @@ export class Tree<ViewModel extends Destroyable> extends TreeNode<ViewModel> {
|
||||
public isTree = true
|
||||
private cachedHash = `${Math.random()}`
|
||||
private unmergedMessages: ChangeBuffer = new ChangeBuffer()
|
||||
public didReceive = new EventDispatcher<void, Tree<ViewModel>>()
|
||||
public didReceive = new EventDispatcher<void>()
|
||||
|
||||
constructor() {
|
||||
super(undefined, undefined)
|
||||
|
||||
@@ -13,9 +13,10 @@ export class TreeNode<ViewModel extends Destroyable> {
|
||||
public collapsed = false
|
||||
public messages: number = 0
|
||||
public lastUpdate: number = Date.now()
|
||||
public onMerge = new EventDispatcher<void, TreeNode<ViewModel>>()
|
||||
public onEdgesChange = new EventDispatcher<void, TreeNode<ViewModel>>()
|
||||
public onMessage = new EventDispatcher<Message, TreeNode<ViewModel>>()
|
||||
public onMerge = new EventDispatcher<void>()
|
||||
public onEdgesChange = new EventDispatcher<void>()
|
||||
public onMessage = new EventDispatcher<Message>()
|
||||
public onDestroy = new EventDispatcher<TreeNode<ViewModel>>()
|
||||
public isTree = false
|
||||
|
||||
private cachedPath?: string
|
||||
@@ -65,7 +66,11 @@ export class TreeNode<ViewModel extends Destroyable> {
|
||||
if (!previous || !this.sourceEdge) {
|
||||
return
|
||||
}
|
||||
this.lastUpdate = Date.now()
|
||||
previous.removeEdge(this.sourceEdge)
|
||||
if (!this.isTree) {
|
||||
this.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
private findChild(edges: Array<string>): TreeNode<ViewModel> | undefined {
|
||||
@@ -101,6 +106,9 @@ export class TreeNode<ViewModel extends Destroyable> {
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
this.onDestroy.dispatch(this)
|
||||
this.onDestroy.removeAllListeners()
|
||||
|
||||
for (const edge of this.edgeArray) {
|
||||
edge.target.destroy()
|
||||
}
|
||||
@@ -158,6 +166,8 @@ export class TreeNode<ViewModel extends Destroyable> {
|
||||
this.edgeArray.push(edge)
|
||||
edge.source = this
|
||||
|
||||
edge.target && edge.target.removeFromTreeIfEmpty()
|
||||
|
||||
if (emitUpdate) {
|
||||
this.onEdgesChange.dispatch()
|
||||
}
|
||||
@@ -175,8 +185,12 @@ export class TreeNode<ViewModel extends Destroyable> {
|
||||
public removeEdge(edge: Edge<any>) {
|
||||
delete this.edges[edge.name]
|
||||
this.edgeArray = Object.values(this.edges)
|
||||
this.onMerge.dispatch()
|
||||
|
||||
this.removeFromTreeIfEmpty()
|
||||
this.onMerge.dispatch()
|
||||
}
|
||||
|
||||
public removeFromTreeIfEmpty() {
|
||||
if (this.isTopicEmptyLeaf()) {
|
||||
this.removeFromParent()
|
||||
}
|
||||
@@ -189,10 +203,7 @@ export class TreeNode<ViewModel extends Destroyable> {
|
||||
this.mqttMessage = node.mqttMessage
|
||||
}
|
||||
|
||||
if (this.isTopicEmptyLeaf()) {
|
||||
this.removeFromParent()
|
||||
}
|
||||
|
||||
this.removeFromTreeIfEmpty()
|
||||
this.mergeEdges(node)
|
||||
this.onMerge.dispatch()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import 'mocha'
|
||||
|
||||
describe('EventDispatcher', async () => {
|
||||
it('should dispatch', async function() {
|
||||
const dispatcher = new EventDispatcher<string, string>()
|
||||
const dispatcher = new EventDispatcher<string>()
|
||||
this.timeout(300)
|
||||
|
||||
setTimeout(() => dispatcher.dispatch('hello'), 5)
|
||||
@@ -18,7 +18,7 @@ describe('EventDispatcher', async () => {
|
||||
})
|
||||
|
||||
it('should unsubscribe', async function() {
|
||||
const dispatcher = new EventDispatcher<string, string>()
|
||||
const dispatcher = new EventDispatcher<string>()
|
||||
this.timeout(300)
|
||||
let callbackCounter = 0
|
||||
const callback = (msg: any) => {
|
||||
|
||||
@@ -45,7 +45,6 @@ export class ConnectionManager {
|
||||
if (buffer.length > 20000) {
|
||||
buffer = buffer.slice(0, 20000)
|
||||
}
|
||||
|
||||
backendEvents.emit(messageEvent, {
|
||||
topic,
|
||||
payload: Base64Message.fromBuffer(buffer),
|
||||
|
||||
Reference in New Issue
Block a user