summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_pipelines_index/store.js.es6
blob: 3ec627d24d02e97e273df243b8a4f165972d385b (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
96
97
98
99
100
/* global gl, Flash */
/* eslint-disable no-param-reassign */

((gl) => {
  const REALTIME = false;
  const PAGINATION_LIMIT = 31;
  const SLICE_LIMIT = 29;

  class RealtimePaginationUpdater {
    constructor(pageData) {
      this.pageData = pageData;
    }

    updatePageDiff(apiResponse) {
      const diffData = this.pageData.slice(0);
      apiResponse.pipelines.forEach((newPipe, i) => {
        if (newPipe.commit) {
          diffData.unshift(newPipe);
        } else {
          const newMerge = Object.assign({}, diffData[i], newPipe);
          diffData[i] = newMerge;
        }
      });
      if (diffData.length < PAGINATION_LIMIT) return diffData;
      return diffData.slice(0, SLICE_LIMIT);
    }
  }

  gl.PipelineStore = class {
    fetchDataLoop(Vue, pageNum, url) {
      const setVueResources = () => { Vue.activeResources = 1; };
      const resetVueResources = () => { Vue.activeResources = 0; };
      const addToVueResources = () => { Vue.activeResources += 1; };
      const subtractFromVueResources = () => { Vue.activeResources -= 1; };

      resetVueResources(); // set Vue.resources to 0

      const updatePipelineNums = (count) => {
        const { all } = count;
        // cannot define non camel case, so not using destructuring for running
        const running = count.running_or_pending;
        document.querySelector('.js-totalbuilds-count').innerHTML = all;
        document.querySelector('.js-running-count').innerHTML = running;
      };

      const resourceChecker = () => {
        if (Vue.activeResources === 0) {
          setVueResources();
        } else {
          addToVueResources();
        }
      };

      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);
            this.pageRequest = false;
            subtractFromVueResources();
          }, () => 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 RealtimePaginationUpdater(this.pipelines);
            Vue.set(this, 'updatedAt', res.updated_at);
            Vue.set(this, 'pipelines', p.updatePageDiff(res));
            Vue.set(this, 'count', res.count);
            updatePipelineNums(this.count);
            subtractFromVueResources();
          }, () => new Flash(
            'Something went wrong on our end.'
          ));

      resourceChecker();
      goFetch();

      if (REALTIME) {
        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 = {}));