import Ember from 'ember';
import Config from 'travis/config/environment';

export default Ember.Service.extend({
  records: [],
  allowFinishedBuilds: false,

  init() {
    Visibility.every(Config.intervals.updateTimes, this.updateTimes.bind(this));
    setInterval(this.resetAllowFinishedBuilds.bind(this), 60000);

    return this._super(...arguments);
  },

  resetAllowFinishedBuilds() {
    this.set('allowFinishedBuilds', true);
  },

  updateTimes() {
    let records = this.get('records');

    records.filter((record) => {
      return this.get('allowFinishedBuilds') || !record.get('isFinished');
    }).forEach((record) => {
      record.updateTimes();
    });

    this.set('records', []);

    if(this.get('allowFinishedBuilds')) {
      this.set('allowFinishedBuilds', false);
    }
  },

  pushObject(record) {
    let records = this.get('records');

    if(!records.contains(record)) {
      records.pushObject(record);
    }
  },

  push(model) {
    if(!model) { return; }

    if(model.forEach) {
      model.forEach( (element) => {
        this.pushObject(element);
      });
    } else {
      this.pushObject(model);
    }
  }
});

@@ -167,16 +167,41 @@
 () %7B%0A   
+ this.set('visibilityId',
  Visibil
@@ -268,21 +268,45 @@
 d(this))
+)
 ;%0A   
+ this.set('intervalId',
  setInte
@@ -358,16 +358,17 @@
 , 60000)
+)
 ;%0A%0A    r
@@ -406,16 +406,161 @@
 ;%0A  %7D,%0A%0A
+  willDestroy() %7B%0A    Visibility.stop(this.get('visibilityId'));%0A    clearInterval(this.get('intervalId'));%0A    this._super(...arguments);%0A  %7D,%0A%0A
   resetA

Clear timers in update-times service