summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_pipelines_index/store.js.es6
blob: c1c153b06046c380bb230cb844e3d674e8d1d791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* global gl, Flash */
/* eslint-disable no-param-reassign */

((gl) => {
  class PipelineUpdater {
    constructor(pipelines) {
      this.pipelines = pipelines;
      this.updateClone = (update, newPipe) => {
        update.forEach((pipe) => {
          if (pipe.id === newPipe.id) pipe = Object.assign(pipe, newPipe);
        });
      };
    }

    currentPageSlicer(update) {
      const length = update.length;
      if (this.pipelines.length === update.length) return update;
      if (update.length <= 30) return update;
      return update.slice(0, (length - 1));
    }

    updatePipelines(apiResponse) {
      const update = this.pipelines.map(e => e);
      apiResponse.pipelines.forEach((newPipe) => {
        if (newPipe.commit) {
          update.unshift(newPipe);
        } else {
          this.updateClone(update, newPipe);
        }
      });
      return this.currentPageSlicer(update);
    }
  }

  gl.PipelineStore = class {
    fetchDataLoop(Vue, pageNum, url) {
      Vue.activeResources = 0;
      const updatePipelineNums = (total, running) => {
        document.querySelector('.js-totalbuilds-count').innerHTML = total;
        document.querySelector('.js-running-count').innerHTML = running;
      };

      const resourceChecker = () => {
        if (Vue.activeResources === 0) {
          Vue.activeResources = 1;
        } else {
          Vue.activeResources += 1;
        }
      };

      const goFetch = () =>
        this.$http.get(`${url}?page=${pageNum}`)
          .then((response) => {
            const res = JSON.parse(response.body);
            Vue.set(this, 'updatedAt', res.updated_at);
            Vue.set(this, 'pipelines', res.pipelines);
            Vue.set(this, 'count', res.count);
            updatePipelineNums(this.count.all, this.count.running_or_pending);
            this.pageRequest = false;
            Vue.activeResources -= 1;
          }, () => new Flash(
            'Something went wrong on our end.'
          ));

      const goUpdate = () =>
        this.$http.get(`${url}?page=${pageNum}&updated_at=${this.updatedAt}`)
          .then((response) => {
            const res = JSON.parse(response.body);
            const p = new PipelineUpdater(this.pipelines);
            Vue.set(this, 'updatedAt', res.updated_at);
            Vue.set(this, 'pipelines', p.updatePipelines(res));
            Vue.set(this, 'count', res.count);
            updatePipelineNums(this.count.all, this.count.running_or_pending);
            Vue.activeResources -= 1;
          }, () => new Flash(
            'Something went wrong on our end.'
          ));

      resourceChecker();
      goFetch();

      this.intervalId = setInterval(() => {
        if (this.updatedAt) {
          resourceChecker();
          if (Vue.activeResources > 1) return;
          goUpdate();
        }
      }, 3000);

      window.onbeforeunload = function removePipelineInterval() {
        clearInterval(this.intervalId);
      };
    }
  };
})(window.gl || (window.gl = {}));