amatriain/feedbunch

View on GitHub
FeedBunch-app/app/assets/javascripts/workers/export_state_worker.js.erb

Summary

Maintainability
Test Coverage
/**
 * Web Worker to load and hide hide the state of an OPML export job in a separate thread.
 */

importScripts('<%= asset_path 'workers/common/do_get' %>');
importScripts('<%= asset_path 'workers/common/do_put' %>');

// Callback for messages from the main thread
onmessage = function(e){
  // Constants for the different operations the web worker can perform
  var LOAD_JOB_STATE = "load_job_state";
  var HIDE_JOB_ALERT = "hide_job_alert";

  // Operation to perform: load or hide
  var operation = e.data.operation;

  // CSRF token
  var token = e.data.token;

  if (operation == LOAD_JOB_STATE){
    // GET job state
    do_get(operation, "/api/opml_exports.json", token, {}, 0);
  }
  else if (operation == HIDE_JOB_ALERT) {
    // Hide the alert showing the job state
    var data = {opml_export: {show_alert: 'false'}};
    do_put(operation, "/api/opml_exports.json", token, data, 0);
  }

}