Introduction
The Problem:
I had to deal with massive console output from a server that is delivered at realtime with signalR. There were over 20k lines in 2 minutes. So every browser had some problems to deal with that amount of data. The solution was simple, truncate the data on client side and dispose lines that are too old, but the implementation was not. I searched for some premade modules that I could use, and I found some but neither some written in typescript nor were they perfect for my environment. So I wrote it myself.
Now, I would like to share this solution on CodeProject.
Using the Code
export module System.Collections
{
export class RingBuffer<T> extends Array<T>
{
constructor(size: number)
{
super();
this._size = size;
}
private _size: number;
public get Size(): number
{
return this._size;
}
private shiftBack(length: number)
{
var overwrite = (this.length + length) - this.Size;
if (overwrite > 0)
{
super.splice(0, overwrite);
}
}
private shiftFor(length: number)
{
var overwrite = (this.length + length) - this.Size;
if (overwrite > 0)
{
var startAt = this.length - overwrite;
super.splice(startAt, overwrite);
}
}
push(...items: T[]): number
{
this.shiftBack(items.length);
return super.push(...items);
}
concat<U extends T[]>(...items: U[]): T[];
concat(...items: T[]): T[]
{
this.shiftBack(items.length);
return super.concat(items);
}
splice(start: number, deleteCount?: number, ...items: T[]): T[]
{
var removed = super.splice(start, deleteCount);
this.push(...items);
return removed;
}
unshift(...items: T[]): number
{
this.shiftFor(items.length);
return super.unshift(...items);
}
}
A nice guy.
And WPF Developer.
And asp.net.
like everything I can get my hand on in .net.
But never java.