2017-05-26 06:56:54 -04:00
|
|
|
import Visibility from 'visibilityjs';
|
2017-10-02 08:32:53 -04:00
|
|
|
import Flash from '../flash';
|
2017-05-26 06:56:54 -04:00
|
|
|
import Poll from '../lib/utils/poll';
|
|
|
|
import PipelineStore from './stores/pipeline_store';
|
|
|
|
import PipelineService from './services/pipeline_service';
|
|
|
|
|
|
|
|
export default class pipelinesMediator {
|
2017-05-26 10:05:43 -04:00
|
|
|
constructor(options = {}) {
|
|
|
|
this.options = options;
|
2017-05-26 06:56:54 -04:00
|
|
|
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();
|
2017-06-02 09:24:42 -04:00
|
|
|
} else {
|
|
|
|
this.refreshPipeline();
|
2017-05-26 06:56:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Visibility.change(() => {
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
this.poll.restart();
|
|
|
|
} else {
|
|
|
|
this.poll.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
successCallback(response) {
|
2017-07-12 10:47:09 -04:00
|
|
|
return response.json().then((data) => {
|
|
|
|
this.state.isLoading = false;
|
|
|
|
this.store.storePipeline(data);
|
|
|
|
});
|
2017-05-26 06:56:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
errorCallback() {
|
|
|
|
this.state.isLoading = false;
|
|
|
|
return new Flash('An error occurred while fetching the pipeline.');
|
|
|
|
}
|
2017-06-02 09:24:42 -04:00
|
|
|
|
|
|
|
refreshPipeline() {
|
|
|
|
this.service.getPipeline()
|
|
|
|
.then(response => this.successCallback(response))
|
|
|
|
.catch(() => this.errorCallback());
|
|
|
|
}
|
2017-05-26 06:56:54 -04:00
|
|
|
}
|