// An array with a max length.
//
// When max length is surpassed, items are removed from
// the front of the array.

// Using Classes to extend Array is unsupported in Babel so this less
// ideal approach is taken: https://babeljs.io/docs/en/caveats#classes
export default function RollingArray(maxLength, ...items) {
  const array = new Array(...items);
  array.maxLength = maxLength;

  // Capture the originals of each array method, but
  // associate them with the array to prevent closures.
  array._push = array.push;
  array._splice = array.splice;
  array._unshift = array.unshift;

  // All mutable array methods build on top of insertAt
  array._insertAt = array.insertAt;

  // Bring the length back down to maxLength by removing from the front
  array._limit = function() {
    const surplus = this.length - this.maxLength;
    if (surplus > 0) {
      this.splice(0, surplus);
    }
  };

  array.push = function(...items) {
    this._push(...items);
    this._limit();
    return this.length;
  };

  array.splice = function(...args) {
    const returnValue = this._splice(...args);
    this._limit();
    return returnValue;
  };

  array.insertAt = function(...args) {
    const returnValue = this._insertAt(...args);
    this._limit();
    this.arrayContentDidChange();
    return returnValue;
  };

  array.unshift = function() {
    throw new Error('Cannot unshift onto a RollingArray');
  };

  return array;
}

@@ -111,16 +111,153 @@
 array.%0A%0A
+// Native array methods%0Alet %7B push, splice %7D = Array.prototype;%0A%0A// Ember array prototype extension%0Alet %7B insertAt %7D = Array.prototype;%0A%0A
 // Using
@@ -522,305 +522,8 @@
 h;%0A%0A
-  // Capture the originals of each array method, but%0A  // associate them with the array to prevent closures.%0A  array._push = array.push;%0A  array._splice = array.splice;%0A  array._unshift = array.unshift;%0A%0A  // All mutable array methods build on top of insertAt%0A  array._insertAt = array.insertAt;%0A%0A
   //
@@ -780,22 +780,25 @@
     
-this._push(...
+push.apply(this, 
 item
@@ -915,24 +915,27 @@
 e = 
-this._splice(...
+splice.apply(this, 
 args
@@ -978,32 +978,88 @@
 urnValue;%0A  %7D;%0A%0A
+  // All mutable array methods build on top of insertAt%0A
   array.insertAt
@@ -1109,22 +1109,16 @@
 e = 
-this._
 insertAt
 (...
@@ -1113,20 +1113,29 @@
 insertAt
-(...
+.apply(this, 
 args);%0A 

Use the prototype instead of "private" property backups