Created
October 3, 2012 01:50
-
-
Save ishiduca/3824467 to your computer and use it in GitHub Desktop.
BaoBab - a Simple FIFO JobQueue
This file contains hidden or 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
function BaoBab () { this.queue = []; } | |
var BBP = { | |
push: function (f) { | |
(typeof f === 'function') | |
&& this.queue.push(f); | |
return this; | |
} | |
, release: function () { | |
var f = this.queue.shift(); | |
return (typeof f !== 'function') | |
? undefined | |
: f.apply(null, arguments); | |
} | |
, clear: function () { | |
this.queue = []; | |
return this; | |
} | |
}; | |
BaoBab.prototype = BBP; | |
function createBaoBab () { | |
return (typeof Object.create === 'function') | |
? Object.create( BBP, { queue: { value: [], writable: true } }) | |
: new BaoBab | |
; | |
} | |
if (typeof window === 'object') { | |
window.createBaoBab = createBaoBab; | |
} else if (typeof module === 'object' && typeof module.exports === 'object') { | |
module.exports = createBaoBab; | |
} |
This file contains hidden or 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
<!doctype html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>BaoBab - this is a simple job queue.</title> | |
</head> | |
<body> | |
<h1>baobab</h1> | |
<div></div> | |
</body> | |
<script src="/baobab.js"></script> | |
<script> | |
function request (url, next_url) { | |
var req = new XMLHttpRequest; | |
var q = this; | |
req.onreadystatechange = function () { | |
if (req.readyState === 4 && req.status === 200) { | |
console.log(JSON.parse(req.responseText)); | |
var node = document.createElement('div'); | |
node.innerHTML = req.responseText + '<br />'; | |
document.getElementsByTagName('div')[0].appendChild( node ); | |
next_url && q.release(next_url); | |
} | |
}; | |
req.open('GET',url, true); | |
req.send(null); | |
} | |
window.onload = function () { | |
var base = 'http://localhost:3021/'; | |
var q = createBaoBab(); | |
q.push(function (url, next_url) { | |
request.apply(q, [ url, next_url ]); | |
}); | |
q.push(function (url) { | |
request.apply(q, [ url, base + 'tea' ]); | |
}); | |
q.push(function (url) { | |
request.apply(q, [ url, base + 'chai?reason=colorful' ]); | |
}); | |
q.push(function (url) { | |
request.apply(q, [ url ]); | |
}); | |
q.release(base + 'favorite/drink?reason', base + 'coffee'); | |
}; | |
</script> |
This file contains hidden or 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 createBaobab = require('./baobab'); | |
var jobQueue = createBaobab(); | |
var hoge = {}; | |
hoge.isNum = function (n) { return typeof n === 'number'; }; | |
hoge.inc = function (n) { return this.isNum(n) ? n + 1 : 1; }; | |
hoge.callback = function (f, n) { | |
n = f(n); | |
setTimeout(function () { | |
jobQueue.release(f, n); | |
}, 1000); | |
console.log(n); | |
}; | |
jobQueue.push( hoge.callback.bind(hoge) ) | |
.push( hoge.callback.bind(hoge) ) | |
.push( hoge.callback.bind(hoge) ) | |
.release(hoge.inc.bind(hoge), 10); |
This file contains hidden or 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
// $ node test_server.js | |
var http = require('http'); | |
var url = require('url'); | |
var util = require('util'); | |
var fs = require('fs'); | |
var port = process.argv[2] || 3021; | |
var index_html = __dirname + '/index.html'; | |
var html = fs.readFileSync(index_html, 'utf8'); | |
var byteLength = Buffer.byteLength(html); | |
var baobab_js = __dirname + '/baobab.js'; | |
var js = fs.readFileSync(baobab_js, 'utf8'); | |
var byteLen = Buffer.byteLength(js); | |
function index (res) { | |
res.setHeader('Content-Length', byteLength); | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
res.end(html); | |
util.log('INDEX'); | |
} | |
function script (res) { | |
res.setHeader('Content-Length', byteLen); | |
res.writeHead(200, { 'Content-Type': 'application/javascript' }); | |
res.end(js); | |
util.log('JS'); | |
} | |
var echo_server = http.createServer(function createServer (req, res) { | |
var parsedURL = url.parse(req.url, true); | |
if (parsedURL.pathname === '/') return index(res); | |
if (parsedURL.pathname === '/baobab.js') return script(res); | |
var responseJSON = JSON.stringify({ | |
query: parsedURL.query | |
, pathname: parsedURL.pathname | |
}); | |
res.setHeader('Content-Length', Buffer.byteLength(responseJSON)); | |
res.writeHead(200, { 'Content-Type': 'application/json' }); | |
res.end(responseJSON); | |
util.log(responseJSON); | |
}); | |
echo_server.listen(port); | |
util.log("server start to listen on port '" + port + '"'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment