summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_merge_request_widget/stores/artifacts_list/actions.js
blob: c8d969e3adffddc25652f9b5eb94dc154475ff2b (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
import Visibility from 'visibilityjs';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
import Poll from '~/lib/utils/poll';

import * as types from './mutation_types';

export const setEndpoint = ({ commit }, endpoint) => commit(types.SET_ENDPOINT, endpoint);

export const requestArtifacts = ({ commit }) => commit(types.REQUEST_ARTIFACTS);

let eTagPoll;

export const clearEtagPoll = () => {
  eTagPoll = null;
};

export const stopPolling = () => {
  if (eTagPoll) eTagPoll.stop();
};

export const restartPolling = () => {
  if (eTagPoll) eTagPoll.restart();
};

export const fetchArtifacts = ({ state, dispatch }) => {
  dispatch('requestArtifacts');

  eTagPoll = new Poll({
    resource: {
      getArtifacts(endpoint) {
        return axios.get(endpoint);
      },
    },
    data: state.endpoint,
    method: 'getArtifacts',
    successCallback: ({ data, status }) => {
      dispatch('receiveArtifactsSuccess', {
        data,
        status,
      });
    },
    errorCallback: () => dispatch('receiveArtifactsError'),
  });

  if (!Visibility.hidden()) {
    eTagPoll.makeRequest();
  } else {
    axios
      .get(state.endpoint)
      .then(({ data, status }) => dispatch('receiveArtifactsSuccess', { data, status }))
      .catch(() => dispatch('receiveArtifactsError'));
  }

  Visibility.change(() => {
    if (!Visibility.hidden()) {
      dispatch('restartPolling');
    } else {
      dispatch('stopPolling');
    }
  });
};

export const receiveArtifactsSuccess = ({ commit }, response) => {
  // With 204 we keep polling and don't update the state
  if (response.status === HTTP_STATUS_OK) {
    commit(types.RECEIVE_ARTIFACTS_SUCCESS, response.data);
  }
};

export const receiveArtifactsError = ({ commit }) => commit(types.RECEIVE_ARTIFACTS_ERROR);