Skip to content

Instantly share code, notes, and snippets.

@primalmotion
Created January 20, 2012 10:32
Show Gist options
  • Save primalmotion/1646616 to your computer and use it in GitHub Desktop.
Save primalmotion/1646616 to your computer and use it in GitHub Desktop.
var requests = {};
self.onmessage = function (e)
{
var action = e.data.action,
data = e.data;
switch (action)
{
case "send":
req = new WorkerRequest(data.rid, data.service, data.body);
requests[data.rid] = req;
req.perform();
break;
case "abort":
req = requests[data.rid];
req.abort();
delete requests[data.rid];
break;
}
}
WorkerRequest = function(rid, service, body)
{
this.rid = rid;
this.service = service;
this.body = body;
this._xhr = new XMLHttpRequest();
}
WorkerRequest.prototype = {
perform: function()
{
this._xhr.addEventListener("readystatechange", this, false);
this._xhr.open("POST", this.service);
this._xhr.send(this.body);
//self.postMessage({"rid": this.rid, "readyState": this._xhr.readyState, "status": this._xhr.status, "responseText": this._xhr.responseText});
},
abort: function()
{
this._xhr.removeEventListener("readystatechange", this, false);
this._xhr.abort();
},
handleEvent: function(e)
{
self.postMessage({"rid": this.rid, "readyState": e.target.readyState, "status": e.target.status, "responseText": e.target.responseText});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment