Skip to content

Commit

Permalink
rearrange redis calls so they are executed in the right order
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jan 18, 2010
1 parent c69ea49 commit de2824e
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ sys.inherits(WheresWaldo, process.EventEmitter)

WheresWaldo.prototype.track = function(user, location) {
this.emit('track', user, location)
return promises.group(
this.cleanupOldUserLocation(user),
this.setUserLocation(user, location),
this.addLocationUser(user, location))
var waldo = this
return this.cleanupOldUserLocation(user, location).addCallback(function() {
waldo.setUserLocation(user, location)
waldo.addLocationUser(user, location)
})
}

WheresWaldo.prototype.locate = function(user) {
Expand Down Expand Up @@ -60,21 +61,27 @@ WheresWaldo.prototype.key = function() {
return args.join(":")
}

WheresWaldo.prototype.cleanupOldUserLocation = function(user) {
WheresWaldo.prototype.cleanupOldUserLocation = function(user, location) {
var waldo = this,
key = this.key(user)
return this.redis.get(key).addCallback(function(oldLocation) {
if(oldLocation)
if(oldLocation && oldLocation != location)
waldo.redis.del(waldo.key(oldLocation, user))
})
}

WheresWaldo.prototype.setUserLocation = function(user, location) {
var key = this.key(user)
return promises.group(this.redis.set(key, location), this.redis.expire(key, this.age))
var key = this.key(user),
waldo = this
return this.redis.set(key, location).addCallback(function() {
waldo.redis.expire(key, waldo.age)
})
}

WheresWaldo.prototype.addLocationUser = function(user, location) {
var key = this.key(location, user)
return promises.group(this.redis.set(key, 1), this.redis.expire(key, this.age))
var key = this.key(location, user),
waldo = this
return this.redis.set(key, 1).addCallback(function() {
waldo.redis.expire(key, waldo.age)
})
}

0 comments on commit de2824e

Please sign in to comment.