Destroy view-models when destroying trees
This commit is contained in:
3
backend/src/Model/Destroyable.ts
Normal file
3
backend/src/Model/Destroyable.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface Destroyable {
|
||||
destroy(): void
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Destroyable } from './Destroyable'
|
||||
import { Hashable, TreeNode } from './'
|
||||
const sha1 = require('sha1')
|
||||
|
||||
export class Edge<ViewModel> implements Hashable {
|
||||
export class Edge<ViewModel extends Destroyable> implements Hashable {
|
||||
public name: string
|
||||
|
||||
public target!: TreeNode<ViewModel>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ChangeBuffer } from './ChangeBuffer'
|
||||
import { Destroyable } from './Destroyable'
|
||||
import {
|
||||
EventBusInterface,
|
||||
EventDispatcher,
|
||||
@@ -8,7 +9,7 @@ import {
|
||||
import { TreeNode } from './'
|
||||
import { TreeNodeFactory } from './TreeNodeFactory'
|
||||
|
||||
export class Tree<ViewModel> extends TreeNode<ViewModel> {
|
||||
export class Tree<ViewModel extends Destroyable> extends TreeNode<ViewModel> {
|
||||
public connectionId?: string
|
||||
public updateSource?: EventBusInterface
|
||||
public nodeFilter?: (node: TreeNode<ViewModel>) => boolean
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Destroyable } from './Destroyable'
|
||||
import { Edge, Message, RingBuffer } from './'
|
||||
import { EventDispatcher, MqttMessage } from '../../../events'
|
||||
|
||||
export class TreeNode<ViewModel> {
|
||||
export class TreeNode<ViewModel extends Destroyable> {
|
||||
public sourceEdge?: Edge<ViewModel>
|
||||
public message?: Message
|
||||
public mqttMessage?: MqttMessage
|
||||
@@ -103,6 +104,8 @@ export class TreeNode<ViewModel> {
|
||||
for (const edge of this.edgeArray) {
|
||||
edge.target.destroy()
|
||||
}
|
||||
this.viewModel && this.viewModel.destroy()
|
||||
this.viewModel = undefined
|
||||
this.edgeArray = []
|
||||
this.edges = {}
|
||||
this.cachedChildTopics = []
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { Base64Message } from './Base64Message'
|
||||
import { Destroyable } from './Destroyable'
|
||||
import { Edge, Tree, TreeNode } from './'
|
||||
|
||||
export abstract class TreeNodeFactory {
|
||||
private static messageCounter = 0
|
||||
public static insertNodeAtPosition<ViewModel>(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) {
|
||||
@@ -16,7 +17,7 @@ export abstract class TreeNodeFactory {
|
||||
node.sourceEdge!.target = node
|
||||
}
|
||||
|
||||
public static fromEdgesAndValue<ViewModel>(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,
|
||||
|
||||
Reference in New Issue
Block a user