Created
February 21, 2014 19:26
-
-
Save brian-gates/9141529 to your computer and use it in GitHub Desktop.
Oboe.js in a worker thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var worker = new Worker("/javascripts/oboe_worker.js"); | |
worker.onmessage = function(event) { | |
var message = JSON.parse(event.data); | |
if(message.path == '!*.user') { | |
console.log(message.data); | |
} | |
} | |
worker.postMessage(JSON.stringify({ | |
url : 'http://localhost:3000/v1/users', | |
method : 'GET', | |
paths : ['!*.user'] | |
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var window = window || {}; // this is required since there isn't a window object in worker context | |
... rest of the file here ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
importScripts('oboe-browser.js'); | |
var oboe = window.oboe; | |
function sendData(data) { | |
postMessage(JSON.stringify(data)); | |
} | |
onmessage = function(event) { | |
var message = JSON.parse(event.data); | |
var req = oboe({ | |
url : message.url, | |
method : message.method, | |
headers : message.headers, | |
}) | |
.fail(function fail(error) { | |
throw error; | |
}); | |
message.paths.forEach(function(path){ | |
req.node(path, function(data) { | |
sendData({ path: path, data: data }) | |
}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment