commit 154a89c6dc185b516d9d1c67843fbe5431fbae14
parent e48bec8216ca3d19565366a7e1935a3b275cb252
Author: Ryan Wolf <rwolf@borderstylo.com>
Date: Wed, 15 Dec 2010 11:02:39 -0800
can handle more than one request at a time
Diffstat:
4 files changed, 51 insertions(+), 26 deletions(-)
diff --git a/addon/chrome/content/content.js b/addon/chrome/content/content.js
@@ -1,5 +1,8 @@
Components.utils.import('resource://antimony/common.js');
-require.path.push('resource://antimony');
+require.path = [
+ 'resource://antimony',
+ 'file:///home/ryan/repos/borderstylo/curatron/lib'
+];
var $ = require('windex');
diff --git a/addon/chrome/modules/windex.js b/addon/chrome/modules/windex.js
@@ -18,10 +18,6 @@ var Windex = exports = function(selector, context) {
throw new Error("$(" + selector + ", " + context + "): selector is not a Node or a String");
}
- if (!Windex.defaultContext) {
- throw new Error('You must define Windex.defaultContext() before calling Windex without a context.');
- }
-
// TODO: return documents, windows
if (selector == "body") return Windex(defaultContext());
if (selector == "!document") {
@@ -49,7 +45,7 @@ var Windex = exports = function(selector, context) {
return new WindexNodes([context], selector, context).find(selector);
};
-Windex.defaultContext = null;
+Windex.prototype.defaultContext = null;
// See: http://api.jquery.com/jQuery.extend/
Windex.extend = function () {
@@ -124,6 +120,8 @@ WindexNodes.prototype.find = function (selector) {
return new WindexNodes(nodes, selector, context);
};
+WindexNodes.prototype.toArray = function () { return [].concat(this); }
+
WindexNodes.prototype.each = function (f) { return Windex.each(this, f); };
WindexNodes.prototype.filter = function (f) {
diff --git a/example/twitter_comments.js b/example/twitter_comments.js
@@ -0,0 +1,18 @@
+var Client = require('../client');
+
+var client = new Client('localhost:1235');
+
+
+for (var i = 0; i < 100; i++) {
+ client.runOnPage(
+ 'http://www.twitter.com/writeonglass',
+ function (callback) {
+ var twitterComments = require('comments').twitterComments;
+ var c = twitterComments(document, $);
+ callback(c);
+ },
+ function (data) {
+ console.log(data);
+ }
+ );
+}
diff --git a/server.js b/server.js
@@ -1,38 +1,44 @@
var http = require('http'),
router = require('./router');
-var timestamp = function () { return (new Date()).getTime(); };
+var id = 0;
+var pushRes = null;
+var clientMessages = [];
+var clientResponses = {};
-var pushResponses = [];
+var forwardNextMessage = function () {
+ if (!pushRes) { return; }
+ if (clientMessages.length === 0) { return; }
+ var message = clientMessages.shift();
+ var res = clientResponses[message.id];
+ var json = message.json;
+ console.log('\tsending to push ' + json);
+ pushRes.writeHead(200, {'Content-Type': 'application/json'});
+ pushRes.end(json);
+ pushRes = null;
+};
-var push = http.createServer(function (req, res) {
+http.createServer(function (req, res) {
console.log('push connected');
- pushResponses.push(res);
-});
-push.listen(1234);
-
-var clientResponses = {};
+ pushRes = res;
+ forwardNextMessage();
+}).listen(1234);
router.post('client', function (req, res) {
- var id = timestamp();
- console.log('post from client (' + id + ')');
var json = '';
req.on('data', function (data) { json += data; });
req.on('end', function (data) {
if (data) { json += data; }
- console.log('\tsaving response');
- clientResponses[id] = res;
-
+ id++;
var data = JSON.parse(json);
data.id = id;
json = JSON.stringify(data);
- console.log('\tsending to push: ' + json);
- pushRes = pushResponses.pop();
- pushResponses = [];
- pushRes.writeHead(200, {'Content-Type': 'application/json'});
- pushRes.end(json);
+ console.log('\tsaving message (' + id + ')');
+ clientMessages.push({ id: id, json: json});
+ clientResponses[id] = res;
+ forwardNextMessage();
});
});
@@ -49,8 +55,8 @@ router.post('browser/:id', function (req, res, id) {
delete clientResponses[id];
clientRes.writeHead(200);
clientRes.end(json);
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end('OREN FOR PRESIDENT');
+ res.writeHead(204);
+ res.end();
});
});