2017-06-12 05:20:19 -04:00
|
|
|
/* global Build */
|
|
|
|
|
|
|
|
import Visibility from 'visibilityjs';
|
2017-10-02 08:32:53 -04:00
|
|
|
import Flash from '../flash';
|
2017-06-12 05:20:19 -04:00
|
|
|
import Poll from '../lib/utils/poll';
|
|
|
|
import JobStore from './stores/job_store';
|
|
|
|
import JobService from './services/job_service';
|
2017-10-10 07:06:42 -04:00
|
|
|
import Job from '../job';
|
|
|
|
import handleRevealVariables from '../build_variables';
|
2017-06-12 05:20:19 -04:00
|
|
|
|
|
|
|
export default class JobMediator {
|
|
|
|
constructor(options = {}) {
|
|
|
|
this.options = options;
|
|
|
|
|
|
|
|
this.store = new JobStore();
|
|
|
|
this.service = new JobService(options.endpoint);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isLoading: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
initBuildClass() {
|
2017-10-10 07:06:42 -04:00
|
|
|
this.build = new Job();
|
|
|
|
handleRevealVariables();
|
2017-06-12 05:20:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchJob() {
|
|
|
|
this.poll = new Poll({
|
|
|
|
resource: this.service,
|
|
|
|
method: 'getJob',
|
2017-11-20 04:57:08 -05:00
|
|
|
successCallback: response => this.successCallback(response),
|
|
|
|
errorCallback: () => this.errorCallback(),
|
2017-06-12 05:20:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
this.state.isLoading = true;
|
|
|
|
this.poll.makeRequest();
|
|
|
|
} else {
|
|
|
|
this.getJob();
|
|
|
|
}
|
|
|
|
|
|
|
|
Visibility.change(() => {
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
this.poll.restart();
|
|
|
|
} else {
|
|
|
|
this.poll.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getJob() {
|
|
|
|
return this.service.getJob()
|
|
|
|
.then(response => this.successCallback(response))
|
|
|
|
.catch(() => this.errorCallback());
|
|
|
|
}
|
|
|
|
|
|
|
|
successCallback(response) {
|
|
|
|
this.state.isLoading = false;
|
2017-11-20 04:57:08 -05:00
|
|
|
return this.store.storeJob(response.data);
|
2017-06-12 05:20:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
errorCallback() {
|
|
|
|
this.state.isLoading = false;
|
|
|
|
|
|
|
|
return new Flash('An error occurred while fetching the job.');
|
|
|
|
}
|
|
|
|
}
|