diff options
Diffstat (limited to 'app/assets/javascripts/vue_merge_request_widget/stores/artifacts_list/actions.js')
-rw-r--r-- | app/assets/javascripts/vue_merge_request_widget/stores/artifacts_list/actions.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/app/assets/javascripts/vue_merge_request_widget/stores/artifacts_list/actions.js b/app/assets/javascripts/vue_merge_request_widget/stores/artifacts_list/actions.js new file mode 100644 index 00000000000..3648db795f5 --- /dev/null +++ b/app/assets/javascripts/vue_merge_request_widget/stores/artifacts_list/actions.js @@ -0,0 +1,74 @@ +import Visibility from 'visibilityjs'; +import axios from '~/lib/utils/axios_utils'; +import Poll from '~/lib/utils/poll'; +import httpStatusCodes from '~/lib/utils/http_status'; + +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 === httpStatusCodes.OK) { + commit(types.RECEIVE_ARTIFACTS_SUCCESS, response.data); + } +}; + +export const receiveArtifactsError = ({ commit }) => commit(types.RECEIVE_ARTIFACTS_ERROR); + +// prevent babel-plugin-rewire from generating an invalid default during karma tests +export default () => {}; |