Permalink
Browse files

removed references to Promise, replaced with callback parameters

  • Loading branch information...
1 parent 79178cb commit 337a9bdadcc2543f4510d710b6db676c17b33bce @banksean committed Mar 2, 2010
Showing with 37 additions and 29 deletions.
  1. +8 −7 lib/atom.js
  2. +29 −22 lib/pshb-client.js
View
@@ -76,21 +76,23 @@ var AtomParser = function(strict) {
exports.AtomParser = AtomParser;
exports.parse = parse;
-function parse(data) {
+function parse(data, callback, errback) {
var parser = new AtomParser(false);
- return parser.parse(data);
+ return parser.parse(data, callback, errback);
}
-AtomParser.prototype.parse = function(data) {
+AtomParser.prototype.parse = function(data, callback, errback) {
+ sys.puts('callback: ' + callback);
var parser = sax.parser(this.strict);
- var promise = new events.Promise();
var feed = new AtomFeed();
var currentEntry = null;
var currentNodeName = "";
var skippingActivity = false;
parser.onerror = function (e) {
- promise.emitError(e);
+ if (errback) {
+ errback(e);
+ }
};
parser.ontext = function (t) {
@@ -152,10 +154,9 @@ AtomParser.prototype.parse = function(data) {
};
parser.onend = function () {
- promise.emitSuccess(feed);
+ callback(feed);
};
parser.write(data).close();
- return promise;
};
View
@@ -29,7 +29,7 @@ Subscriber.prototype.createCallbackUri = function() {
return "http://localhost:" + this.callbackPort + "/";
};
-Subscriber.prototype.subscribe = function(topicUri, hubUri, callbackUri) {
+Subscriber.prototype.subscribe = function(topicUri, hubUri, callbackUri, callbackFn, errbackFn) {
var callback = callbackUri.href,
mode = "subscribe",
topic = topicUri.href,
@@ -50,8 +50,6 @@ Subscriber.prototype.subscribe = function(topicUri, hubUri, callbackUri) {
hubUri = url.parse(hubUri.href);
- var promise = new process.Promise();
-
var port = hubUri.port;
if (!port) {
port = 80;
@@ -89,21 +87,19 @@ Subscriber.prototype.subscribe = function(topicUri, hubUri, callbackUri) {
if (response.statusCode == 202) {
// Accepted means success for an async request, but we need
// to wait for async verification request now.
- promise.emitSuccess();
+ callbackFn();
} else if (response.statusCode == 204) {
// No-Content means success for a sync sub.
- promise.emitSuccess();
+ callbackFn();
} else {
// Any other response code is an error.
- promise.emitError(body)
+ errbackFn(body);
}
})
});
hubRequest.write(requestBody);
hubRequest.close();
-
- return promise;
}
Subscriber.prototype.registerEventEmitter = function(topicUri, topicEvents) {
@@ -137,20 +133,21 @@ Subscriber.prototype.listen = function(topicUri) {
});
response.addListener('end', function() {
- var atomPromise = atom.parse(body);
- atomPromise.addCallback(function(feed) {
+ var callback =
+ function(feed) {
var hubUri = feed.getLinksByRel('hub')[0];
-
var callbackUri = url.parse(subscriber.createCallbackUri());
- var subPromise = subscriber.subscribe(topicUri, hubUri, callbackUri);
- subPromise.addCallback(function() {
- topicEvents.emit('subscribed', topicUri.href);
- subscriber.registerEventEmitter(topicUri, topicEvents);
- });
- subPromise.addErrback(function(error) {
- topicEvents.emit('error', error);
- })
- })
+ subscriber.subscribe(topicUri, hubUri, callbackUri,
+ function() {
+ topicEvents.emit('subscribed', topicUri.href);
+ subscriber.registerEventEmitter(topicUri, topicEvents);
+ },
+ function(error) {
+ topicEvents.emit('error', error);
+ });
+ };
+ sys.puts('callback: ' + callback);
+ atom.parse(body, callback);
});
});
topicRequest.close();
@@ -203,8 +200,7 @@ Subscriber.prototype.startCallbackServer = function() {
request.addListener('end', function() {
response.sendHeader(200);
response.close();
- var atomPromise = atom.parse(body);
- atomPromise.addCallback(function(feed) {
+ atom.parse(body, function(feed) {
var topicId = trim(feed.id);
var events = subscriber.topicEventEmitters[topicId];
events.emit('update', feed);
@@ -214,6 +210,17 @@ Subscriber.prototype.startCallbackServer = function() {
}).listen(this.callbackPort);
}
+var Publisher = function() {
+ // TODO: Write this :)
+};
+
+exports.Publisher = Publisher;
+
+Publisher.prototype.publish = function(atomFeed) {
+ // get the hub link from the atomFeed links
+ // POST to it
+}
+
function trim(str) {
return str.replace(/^\s+|\s+$/g,"");
}

0 comments on commit 337a9bd

Please sign in to comment.