I’m attempting to create a BuiltIn Array wrapper in order to make a Circular Buffer, I basically want a class that acts just like an array… although when you Add it checks the maximum length and Shifts the first item if the length is exceeded.
It’s a slow day, thought I’d write you one. You can use it from JS but I wrote it in C# as I wanted to create a generic version. Hence you should put this in a CS file and place it in a Plugins folder to access it from JS.
public class CircularBuffer<T> : List<T>
{
public int Capacity = 1;
public CircularBuffer(int capacity)
{
Capacity = capacity;
}
public CircularBuffer()
{
}
public void Add (T item)
{
base.Add(item);
if(Count > Capacity)
RemoveAt(0);
}
public void Insert (int index, T item)
{
base.Insert(index,item);
if(Count > Capacity)
RemoveAt(0);
}
}
To use in Javascript:
var buffer = new CircularBuffer.<object>(10); //Use your base class
buffer.Add("2");
If you choose to use insert, be aware that inserting at 0 will cause it to be immediately deleted if the list is at capacity.