amatriain/feedbunch

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

Summary

Maintainability
Test Coverage
/**
 * Web Worker to move feeds into or out of folders in a separate thread.
 */

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

// Callback for messages from the main thread
onmessage = function(e){
  // Constants for the different operations the web worker can perform
  var MOVE_INTO_NEW_FOLDER = "move_into_new_folder";
  var MOVE_INTO_EXISTING_FOLDER = "move_into_existing_folder";
  var REMOVE_FROM_FOLDER = "remove_from_folder";

  // Operation to perform: move feed into existing folder, into new folder, or out of its folder
  var operation = e.data.operation;

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

  // ID of the feed to move. Mandatory.
  var feed_id = e.data.feed_id;

  var data = {folder: {feed_id: feed_id}};

  if (operation == MOVE_INTO_NEW_FOLDER){
    var title = e.data.title;
    data.folder.title = title;
    var url = "/api/folders.json";
    do_post(operation, url, token, data, 0);
  }
  else if (operation == MOVE_INTO_EXISTING_FOLDER){
    var folder_id = e.data.folder_id;
    var url = "/api/folders/" + folder_id + ".json";
    do_put(operation, url, token, data, 0);
  }
  else if (operation == REMOVE_FROM_FOLDER){
    var url = "/api/folders/none.json";
    do_put(operation, url, token, data, 0);
  }
}