Skip to content

Commit

Permalink
Removed all promises.
Browse files Browse the repository at this point in the history
  • Loading branch information
strange committed Mar 10, 2010
1 parent 28e524f commit 60b9c21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 9 additions & 8 deletions examples/lookup2.js
@@ -1,14 +1,15 @@
var sys = require('sys'),
geoip = require('../lib/geoip');

con = new geoip.Connection('/usr/local/share/GeoIP/GeoLiteCity.dat');

var dbpath = '/usr/local/share/GeoIP/GeoLiteCity.dat';
var ip = '216.236.135.152';

sys.puts('Looking up ip: ' + ip + '...\n');
con.query(ip).addCallback(function (result) {
for (var attr in result) {
sys.puts(attr + ' : ' + result[attr]);
}
var con = new geoip.Connection(dbpath, function(con) {
con.query(ip, function(result) {
for (var attr in result) {
sys.puts(attr + ' : ' + result[attr]);
}
con.close();
});
});

con.close();
16 changes: 8 additions & 8 deletions lib/geoip.js
@@ -1,11 +1,12 @@
var sys = require('sys'),
ceoip = require('./ceoip');

function Connection(dbpath) {
function Connection(dbpath, callback) {
this.con = new ceoip.Connection();
this.queue = [];
this.connected = false;
this.currentQuery = null;
this.callback = callback;

var self = this;

Expand All @@ -16,11 +17,12 @@ function Connection(dbpath) {

this.con.addListener('connected', function() {
self.connected = true;
self.callback(self);
self.processQueue();
});

this.con.addListener('result', function(result) {
self.currentQuery[0].emitSuccess(result);
self.currentQuery[0](result);
self.currentQuery = null;
self.processQueue();
});
Expand All @@ -30,15 +32,13 @@ function Connection(dbpath) {

sys.inherits(Connection, process.EventEmitter);

Connection.prototype.addJob = function(promise, ipAddress) {
this.queue.push([promise, ipAddress]);
Connection.prototype.addJob = function(callback, ipAddress) {
this.queue.push([callback, ipAddress]);
this.processQueue();
};

Connection.prototype.query = function(ipAddress) {
var promise = new process.Promise;
this.addJob(promise, ipAddress);
return promise;
Connection.prototype.query = function(ipAddress, callback) {
this.addJob(callback, ipAddress);
};

Connection.prototype.processQueue = function () {
Expand Down

0 comments on commit 60b9c21

Please sign in to comment.