Welcome to mirror list, hosted at ThFree Co, Russian Federation.

job_details_mediator.js « jobs « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e2658f9fc16ea8af2a226c9dd1ff21498747704 (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
/* global Build */

import Visibility from 'visibilityjs';
import Flash from '../flash';
import Poll from '../lib/utils/poll';
import JobStore from './stores/job_store';
import JobService from './services/job_service';
import Job from '../job';
import handleRevealVariables from '../build_variables';

export default class JobMediator {
  constructor(options = {}) {
    this.options = options;

    this.store = new JobStore();
    this.service = new JobService(options.endpoint);

    this.state = {
      isLoading: false,
    };
  }

  initBuildClass() {
    this.build = new Job();
    handleRevealVariables();
  }

  fetchJob() {
    this.poll = new Poll({
      resource: this.service,
      method: 'getJob',
      successCallback: this.successCallback.bind(this),
      errorCallback: this.errorCallback.bind(this),
    });

    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;
    return response.json().then(data => this.store.storeJob(data));
  }

  errorCallback() {
    this.state.isLoading = false;

    return new Flash('An error occurred while fetching the job.');
  }
}