Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / Typescript
Tip/Trick

RingBuffer in Typescript

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 May 2016CPOL 9.1K   2  
RingBuffer in Typescript

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

JavaScript
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);
		}
 
		/**
		  * Combines two or more arrays.
		  * @param items Additional items to add to the end of array1.
		  */
		concat<U extends T[]>(...items: U[]): T[];
		/**
		  * Combines two or more arrays.
		  * @param items Additional items to add to the end of array1.
		  */
		concat(...items: T[]): T[]
		{
			this.shiftBack(items.length);
			return super.concat(items);
		}
		
		/**
		  * Removes elements from an array and, if necessary, 
		  * inserts new elements in their place, returning the deleted elements.
		  * @param start The zero-based location in the array from which 
                  * to start removing elements.
		  * @param deleteCount The number of elements to remove.
		  * @param items Elements to insert into the array in place of the deleted elements.
		  */
		splice(start: number, deleteCount?: number, ...items: T[]): T[]
		{
			var removed = super.splice(start, deleteCount);
			this.push(...items);
			return removed;
		}
 
		/**
		  * Inserts new elements at the start of an array.
		  * @param items  Elements to insert at the start of the Array.
		  */
		unshift(...items: T[]): number
		{
			this.shiftFor(items.length);
			return super.unshift(...items);
		}	
	}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Freelancer
Germany Germany
A nice guy.
And WPF Developer.
And asp.net.

like everything I can get my hand on in .net.
But never java.

Comments and Discussions

 
-- There are no messages in this forum --