debian-mirror-gitlab/app/assets/javascripts/pipelines/pipeline_details_mediator.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import Visibility from 'visibilityjs';
2018-03-17 18:26:18 +05:30
import Flash from '../flash';
2017-09-10 17:25:29 +05:30
import Poll from '../lib/utils/poll';
2018-03-27 19:54:05 +05:30
import { __ } from '../locale';
2017-09-10 17:25:29 +05:30
import PipelineStore from './stores/pipeline_store';
import PipelineService from './services/pipeline_service';
export default class pipelinesMediator {
constructor(options = {}) {
this.options = options;
this.store = new PipelineStore();
this.service = new PipelineService(options.endpoint);
this.state = {};
this.state.isLoading = false;
}
fetchPipeline() {
this.poll = new Poll({
resource: this.service,
method: 'getPipeline',
successCallback: this.successCallback.bind(this),
errorCallback: this.errorCallback.bind(this),
});
if (!Visibility.hidden()) {
this.state.isLoading = true;
this.poll.makeRequest();
} else {
this.refreshPipeline();
}
Visibility.change(() => {
if (!Visibility.hidden()) {
this.poll.restart();
} else {
this.poll.stop();
}
});
}
successCallback(response) {
2018-10-15 14:42:47 +05:30
this.state.isLoading = false;
this.store.storePipeline(response.data);
2017-09-10 17:25:29 +05:30
}
errorCallback() {
this.state.isLoading = false;
2018-03-27 19:54:05 +05:30
Flash(__('An error occurred while fetching the pipeline.'));
2017-09-10 17:25:29 +05:30
}
refreshPipeline() {
2018-05-09 12:01:36 +05:30
this.poll.stop();
2018-11-08 19:23:39 +05:30
return this.service
.getPipeline()
2017-09-10 17:25:29 +05:30
.then(response => this.successCallback(response))
2018-05-09 12:01:36 +05:30
.catch(() => this.errorCallback())
.finally(() => this.poll.restart());
2017-09-10 17:25:29 +05:30
}
}