FeedBunch-app/app/assets/javascripts/workers/subscriptions_worker.js.erb
/**
* Web Worker to add or remove subscriptions in a separate thread.
*/
importScripts('<%= asset_path 'workers/common/do_post' %>');
importScripts('<%= asset_path 'workers/common/do_delete' %>');
// Callback for messages from the main thread
onmessage = function(e){
// Constants for the different operations the web worker can perform
var SUBSCRIBE = "subscribe";
var UNSUBSCRIBE = "unsubscribe";
// Operation to perform: subscribe or unsubscribe
var operation = e.data.operation;
// CSRF token
var token = e.data.token;
// Add a subscription
if (operation == SUBSCRIBE){
// URL of the feed to subscribe
var feed_url = e.data.feed_url;
var url = "/api/feeds.json";
var data = {feed: {url: feed_url}};
do_post(operation, url, token, data, 0);
}
// Remove a subscription
else if (operation == UNSUBSCRIBE){
// ID of the feed to unsubscribe
var feed_id = e.data.feed_id;
var data = {feed: {id: feed_id}};
var url = "/api/feeds/" + feed_id + ".json";
do_delete(operation, url, token, data, 0);
}
}