Sha256: aac4f869560ebd6c31292b7adb578f3b459fd7744c6f63f699a318ab39fa7f9c

Contents?: true

Size: 1.96 KB

Versions: 7

Compression:

Stored size: 1.96 KB

Contents

(function(window){
  'use strict';
  function defineLibrary(){
    var MILLISECONDS_TO_GET_JOBS = 5000;
    var JobNotifier = {};

    JobNotifier.init = function() {
      var body = document.querySelector('body');
      JobNotifier.jobIdentifier = body.dataset.identifier;

      if(!JobNotifier.jobIdentifier) {
        return;
      }

      JobNotifier.rootUrl = body.dataset.rootUrl;
      JobNotifier.findJobs();
    };

    JobNotifier.performRequest = function(method, action, onLoadCallback) {
      var oReq = new XMLHttpRequest();
      oReq.onload = onLoadCallback;
      oReq.onerror = JobNotifier.onError;
      var url = JobNotifier.rootUrl + 'job_notifier/' + JobNotifier.jobIdentifier + '/jobs/' + action + '.json';
      oReq.open(method, url, true);
      oReq.send();
    };

    JobNotifier.findJobs = function() {
      setTimeout(JobNotifier.findPendingJobs, MILLISECONDS_TO_GET_JOBS);
    };

    JobNotifier.findPendingJobs = function() {
      JobNotifier.performRequest('get', 'pending', JobNotifier.onPendingJobsLoad);
    };

    JobNotifier.notifyJobs = function() {
      JobNotifier.performRequest('put', 'notify', JobNotifier.onNotifyJobsLoad);
    };

    JobNotifier.onPendingJobsLoad = function() {
      var data = JSON.parse(this.responseText);

      if(data.length === 0) {
        JobNotifier.findJobs();
        return;
      }

      JobNotifier.onNotify(data);
      JobNotifier.notifyJobs();
    };

    JobNotifier.onNotifyJobsLoad = function() {
      JobNotifier.findJobs();
    };

    JobNotifier.onNotify = function(data) {
      console.info('Override this method with your own logic. Data: ', data);
    };

    JobNotifier.onError = function(err) {
      console.error('Error', err);
    };

    return JobNotifier;
  }

  if(typeof(JobNotifier) === 'undefined'){
    window.JobNotifier = defineLibrary();
  } else{
    console.log('JobNotifier already defined.');
  }
})(window);

document.addEventListener('DOMContentLoaded', JobNotifier.init);

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
job_notifier-1.2.3 app/assets/javascripts/job_notifier/notifier.js
job_notifier-1.2.2 app/assets/javascripts/job_notifier/notifier.js
job_notifier-1.2.1 app/assets/javascripts/job_notifier/notifier.js
job_notifier-1.2.0 app/assets/javascripts/job_notifier/notifier.js
job_notifier-1.1.1 app/assets/javascripts/job_notifier/notifier.js
job_notifier-1.1.0 app/assets/javascripts/job_notifier/notifier.js
job_notifier-1.0.0 app/assets/javascripts/job_notifier/notifier.js