amatriain/feedbunch

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

Summary

Maintainability
Test Coverage
/**
 * Web Worker to update the state of entries in a separate thread.
 */

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 CHANGE_SINGLE_ENTRY = "change_single_entry";
  var CHANGE_MULTIPLE_ENTRIES = "change_multiple_entries";

  // Operation to perform: change the state of a single or multiple entries
  var operation = e.data.operation;

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

  // ID of the entry
  var id = e.data.id;

  // New state for the entry, "read" or "unread"
  var state = e.data.state;

  var data = {entry: {id: id, state: state}};

  if(operation == CHANGE_MULTIPLE_ENTRIES){
    // Is user marking a whole feed as read?
    var whole_feed = e.data.whole_feed;

    // Is user marking a whole folder as read?
    var whole_folder = e.data.whole_folder;

    // Is user marking all entries in all folders as read?
    var all_entries = e.data.all_entries;

    data["entry"]["whole_feed"] = whole_feed;
    data["entry"]["whole_folder"] = whole_folder;
    data["entry"]["all_entries"] = all_entries;
  }

  do_put(operation, "/api/entries/update.json", token, data, 0);
}