Extract ChangeBuffer to own file

This commit is contained in:
Thomas Nordquist
2019-04-07 19:32:13 +02:00
parent ab3e35520b
commit c7e20c26cb
2 changed files with 45 additions and 38 deletions

View File

@@ -0,0 +1,38 @@
import { MqttMessage } from '../../../events'
export class ChangeBuffer {
private buffer: MqttMessage[] = []
private size = 0
private maxSize = 100_000_000 // ~100MB
public length = 0
public estimatedMessageOverhead = 24
public push(val: MqttMessage) {
if (!this.isFull()) {
this.buffer.push(val)
this.size += this.estimatedMessageOverhead + (val.payload ? val.payload.length : 0)
this.length += 1
}
}
public getSize() {
return this.size
}
public isFull() {
return this.size >= this.maxSize
}
public fillState() {
return this.size / this.maxSize
}
public popAll(): MqttMessage[] {
const tmpBuffer = this.buffer
this.buffer = []
this.size = 0
this.length = 0
return tmpBuffer
}
}

View File

@@ -1,44 +1,13 @@
import { ChangeBuffer } from './ChangeBuffer'
import {
EventBusInterface,
EventDispatcher,
makeConnectionMessageEvent,
MqttMessage
} from '../../../events'
import { TreeNode } from './' import { TreeNode } from './'
import { EventBusInterface, makeConnectionMessageEvent, MqttMessage, EventDispatcher } from '../../../events'
import { TreeNodeFactory } from './TreeNodeFactory' import { TreeNodeFactory } from './TreeNodeFactory'
class ChangeBuffer {
private buffer: MqttMessage[] = []
private size = 0
private maxSize = 100_000_000 // ~100MB
public length = 0
public estimatedMessageOverhead = 24
public push(val: MqttMessage) {
if (!this.isFull()) {
this.buffer.push(val)
this.size += this.estimatedMessageOverhead + (val.payload ? val.payload.length : 0)
this.length += 1
}
}
public getSize() {
return this.size
}
public isFull() {
return this.size >= this.maxSize
}
public fillState() {
return this.size / this.maxSize
}
public popAll(): MqttMessage[] {
const tmpBuffer = this.buffer
this.buffer = []
this.size = 0
this.length = 0
return tmpBuffer
}
}
export class Tree<ViewModel> extends TreeNode<ViewModel> { export class Tree<ViewModel> extends TreeNode<ViewModel> {
public connectionId?: string public connectionId?: string
public updateSource?: EventBusInterface public updateSource?: EventBusInterface