Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #13 from fent/master

Emitted events make more sense and are more consistent
  • Loading branch information...
commit 8bda28632629f4b223c2c91914de0bc3294696e8 2 parents afdc7a5 + 7a7e5ce
Paolo Fragomeni authored
Showing with 173 additions and 70 deletions.
  1. +37 −29 lib/eventvat.js
  2. +121 −41 test/events-test.js
  3. +15 −0 test/methods-test.js
View
66 lib/eventvat.js
@@ -139,7 +139,7 @@
}
}
- this.emit('keys', keys, regex);
+ this.emit('keys', regex, keys);
return keys;
};
@@ -291,9 +291,10 @@
}
var newValue = this.hash[key].value += value;
- this.emit('append ' + key, value, newValue);
- this.emit('append', key, value, newValue);
- return newValue.length;
+ var len = newValue.length;
+ this.emit('append ' + key, value, len);
+ this.emit('append', key, value, len);
+ return len;
};
//
@@ -376,8 +377,8 @@
p.getrange = function(key, start, end) {
if (this.type(key) === 'string') {
var value = this.hash[key].value.slice(start, end);
- this.emit('getrange ' + key, value);
- this.emit('getrange', key, value);
+ this.emit('getrange ' + key, start, end, value);
+ this.emit('getrange', key, start, end, value);
return value;
} else {
return false;
@@ -390,6 +391,8 @@
p.getset = function(key, value) {
var old = this.get(key);
this.set(key, value);
+ this.emit('getset ' + key, value, old);
+ this.emit('getset', key, value, old);
return old;
};
@@ -428,11 +431,16 @@
// Get the values of all the given keys
//
p.mget = function(key /* ... */) {
+ var keys = Array.prototype.slice.call(arguments);
var values = [];
- for(var i=0, l=arguments.length; i < l; i++) {
- values.push(this.get(arguments[i]));
+
+ for(var i=0, l=keys.length; i < l; i++) {
+ values.push(this.get(keys[i]));
}
- this.emit('mget', values);
+
+ keys.unshift('mget');
+ keys.push(values);
+ this.emit.apply(this, keys);
return values;
};
@@ -572,8 +580,8 @@
this.persist(key);
this.hash.value = newValue;
- this.emit('setrange ' + key, newValue);
- this.emit('setrange', key, newValue);
+ this.emit('setrange ' + key, offset, value, l);
+ this.emit('setrange', key, offset, value, l);
return l;
} else {
return false;
@@ -837,8 +845,8 @@
}
this.hash[key].value[field] = value;
- this.emit('hset ' + key, field, value);
- this.emit('hset', key, field, value);
+ this.emit('hset ' + key, field, value, update);
+ this.emit('hset', key, field, value, update);
return update;
};
@@ -970,12 +978,12 @@
for (var i = 1, l = arguments.length; i < l; i++) {
var val = arguments[i];
- list.unshift(val);
- this.emit('lpush ' + key, val);
- this.emit('lpush', key, val);
+ var len = list.unshift(val);
+ this.emit('lpush ' + key, val, len);
+ this.emit('lpush', key, val, len);
}
- return list.length;
+ return len;
};
//
@@ -984,8 +992,8 @@
p.lpushx = function(key, value) {
if (this.type(key) === 'list') {
var len = this.hash[key].value.unshift(value);
- this.emit('lpushx ' + key, value);
- this.emit('lpushx', key, value);
+ this.emit('lpushx ' + key, value, len);
+ this.emit('lpushx', key, value, len);
return len;
} else {
return 0;
@@ -1017,7 +1025,7 @@
// if count is positive, list will be traversed from 0 to list.length
if (ncount >= 0) {
i = 0;
- check = function() { return i < list.lengthl; };
+ check = function() { return i < list.length; };
incr = function() { i++; };
cancel = function() { i--; };
@@ -1129,13 +1137,13 @@
this.persist(source);
var value = this.hash[source].value.pop();
- dest.unshift(value);
+ var len = dest.unshift(value);
this.emit('rpoplpush ' + source, destination, value);
this.emit('rpoplpush', source, destination, value);
this.emit('rpop ' + source, value);
this.emit('rpop', source, value);
- this.emit('lpush ' + destination, value);
- this.emit('lpush', destination, value);
+ this.emit('lpush ' + destination, value, len);
+ this.emit('lpush', destination, value, len);
return value;
} else {
return null;
@@ -1160,12 +1168,12 @@
for (var i = 1, l = arguments.length; i < l; i++) {
var val = arguments[i];
- list.push(val);
- this.emit('rpush ' + key, val);
- this.emit('rpush', key, val);
+ var len = list.push(val);
+ this.emit('rpush ' + key, val, len);
+ this.emit('rpush', key, val, len);
}
- return list.length;
+ return len;
};
//
@@ -1174,8 +1182,8 @@
p.rpushx = function(key, value) {
if (this.type(key) === 'list') {
var len = this.hash[key].value.push(value);
- this.emit('rpushx ' + key, value);
- this.emit('rpushx', key, value);
+ this.emit('rpushx ' + key, value, len);
+ this.emit('rpushx', key, value, len);
return len;
} else {
return 0;
View
162 test/events-test.js
@@ -88,6 +88,28 @@ module.exports = simpleEvents({
test.done();
},
+ 'Raise event on `getset` method invokation': function (test) {
+
+ var vat = EventVat();
+
+ vat.on('getset', function(key, value, old) {
+ test.equal(key, 'foo');
+ test.equal(value, 2);
+ test.equal(old, 1);
+ });
+
+ vat.on('getset foo', function(value, old) {
+ test.equal(value, 2);
+ test.equal(old, 1);
+ });
+
+ vat.set('foo', 1);
+ vat.getset('foo', 2);
+
+ test.expect(5);
+ vat.die();
+ test.done();
+ },
'Raise event on `rename` method invokation': function (test) {
var vat = EventVat();
@@ -290,15 +312,15 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.on('append', function(key, value, newValue) {
+ vat.on('append', function(key, value, len) {
test.equal(key, 'foo');
test.equal(value, 'bar');
- test.equal(newValue, 'foobar');
+ test.equal(len, 6);
});
- vat.on('append foo', function(value, newValue) {
+ vat.on('append foo', function(value, len) {
test.equal(value, 'bar');
- test.equal(newValue, 'foobar');
+ test.equal(len, 6);
});
vat.set('foo', 'foo');
@@ -378,9 +400,9 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.on('keys', function(keys, regex) {
- test.deepEqual(keys, ['one', 'two']);
+ vat.on('keys', function(regex, keys) {
test.equal(regex, r);
+ test.deepEqual(keys, ['one', 'two']);
});
vat.set('foo', 'bar');
@@ -440,19 +462,23 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.on('getrange', function(key, value) {
+ vat.on('getrange', function(key, start, end, value) {
test.equal(key, 'foo');
+ test.equal(start, 6);
+ test.equal(end, 11);
test.equal(value, 'world');
});
- vat.on('getrange foo', function(value) {
+ vat.on('getrange foo', function(start, end, value) {
+ test.equal(start, 6);
+ test.equal(end, 11);
test.equal(value, 'world');
});
vat.set('foo', 'hello world!');
vat.getrange('foo', 6, 11);
- test.expect(3);
+ test.expect(7);
vat.die();
test.done();
@@ -461,7 +487,9 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.on('mget', function(values) {
+ vat.on('mget', function(key1, key2, values) {
+ test.equal(key1, 'foo');
+ test.equal(key2, 'bar');
test.deepEqual(values, ['hello world!', 42]);
});
@@ -469,7 +497,7 @@ module.exports = simpleEvents({
vat.set('bar', 42);
vat.mget('foo', 'bar');
- test.expect(1);
+ test.expect(3);
vat.die();
test.done();
@@ -565,24 +593,28 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.on('setrange', function(key, value) {
+ vat.on('setrange', function(key, offset, value, len) {
test.equal(key, 'foo');
- test.equal(value, 'hello redis!');
+ test.equal(offset, 6);
+ test.equal(value, 'redis');
+ test.equal(len, 12);
});
- vat.on('setrange foo', function(value) {
- test.equal(value, 'hello redis!');
+ vat.on('setrange foo', function(offset, value, len) {
+ test.equal(offset, 6);
+ test.equal(value, 'redis');
+ test.equal(len, 12);
});
vat.set('foo', 'hello world!');
vat.setrange('foo', 6, 'redis');
- test.expect(3);
+ test.expect(7);
vat.die();
test.done();
},
- 'Raise event on `hset` method invokation': function(test) {
+ 'Raise event on `hget` method invokation': function(test) {
var vat = EventVat();
@@ -592,7 +624,7 @@ module.exports = simpleEvents({
test.equal(value, 1);
});
- vat.on('hset hash', function(field, value) {
+ vat.on('hget hash', function(field, value) {
test.equal(field, 'a');
test.equal(value, 1);
});
@@ -605,25 +637,55 @@ module.exports = simpleEvents({
test.done();
},
- 'Raise event on `hset` method invokation': function(test) {
+ 'Raise event on `hset` method invokation for non-existing key': function(test) {
var vat = EventVat();
- vat.on('hget', function(key, field, value) {
+ vat.on('hset', function(key, field, value, update) {
test.equal(key, 'hash');
test.equal(field, 'a');
test.equal(value, 1);
+ test.equal(update, false);
});
- vat.on('hget hash', function(field, value) {
+ vat.on('hset hash', function(field, value, update) {
test.equal(field, 'a');
test.equal(value, 1);
+ test.equal(update, false);
});
vat.hset('hash', 'a', 1);
vat.hget('hash', 'a');
- test.expect(5);
+ test.expect(7);
+ vat.die();
+ test.done();
+
+ },
+ 'Raise event on `hset` method invokation for pre-existing key': function(test) {
+
+ var vat = EventVat();
+
+ vat.on('hset', function(key, field, value, update) {
+ if (value !== 1) return;
+ test.equal(key, 'hash');
+ test.equal(field, 'a');
+ test.equal(value, 1);
+ test.equal(update, true);
+ });
+
+ vat.on('hset hash', function(field, value, update) {
+ if (value !== 1) return;
+ test.equal(field, 'a');
+ test.equal(value, 1);
+ test.equal(update, true);
+ });
+
+ vat.hset('hash', 'a', 'hi');
+ vat.hset('hash', 'a', 1);
+ vat.hget('hash', 'a');
+
+ test.expect(7);
vat.die();
test.done();
@@ -945,26 +1007,32 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.once('lpush', function(key, value) {
+ vat.once('lpush', function(key, value, len) {
test.equal(key, 'mylist');
test.equal(value, 'one');
- vat.once('lpush', function(key, value) {
+ test.equal(len, 1);
+
+ vat.once('lpush', function(key, value, len) {
test.equal(key, 'mylist');
test.equal(value, 'two');
+ test.equal(len, 2);
});
});
- vat.once('lpush mylist', function(value) {
+ vat.once('lpush mylist', function(value, len) {
test.equal(value, 'one');
- vat.once('lpush mylist', function(value) {
+ test.equal(len, 1);
+
+ vat.once('lpush mylist', function(value, len) {
test.equal(value, 'two');
+ test.equal(len, 2);
});
});
vat.lpush('mylist', 'one');
vat.lpush('mylist', 'two');
- test.expect(6);
+ test.expect(10);
vat.die();
test.done();
},
@@ -972,26 +1040,32 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.once('rpush', function(key, value) {
+ vat.once('rpush', function(key, value, len) {
test.equal(key, 'mylist');
test.equal(value, 'one');
- vat.once('rpush', function(key, value) {
+ test.equal(len, 1);
+
+ vat.once('rpush', function(key, value, len) {
test.equal(key, 'mylist');
test.equal(value, 'two');
+ test.equal(len, 2);
});
});
- vat.once('rpush mylist', function(value) {
+ vat.once('rpush mylist', function(value, len) {
test.equal(value, 'one');
- vat.once('rpush mylist', function(value) {
+ test.equal(len, 1);
+
+ vat.once('rpush mylist', function(value, len) {
test.equal(value, 'two');
+ test.equal(len, 2);
});
});
vat.rpush('mylist', 'one');
vat.rpush('mylist', 'two');
- test.expect(6);
+ test.expect(10);
vat.die();
test.done();
},
@@ -1068,20 +1142,22 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.on('lpushx', function(key, value) {
+ vat.on('lpushx', function(key, value, len) {
test.equal(key, 'mylist');
test.equal(value, 'two');
+ test.equal(len, 2);
});
- vat.on('lpushx mylist', function(value) {
+ vat.on('lpushx mylist', function(value, len) {
test.equal(value, 'two');
+ test.equal(len, 2);
});
vat.lpush('mylist', 'one');
vat.lpushx('mylist', 'two');
vat.lpushx('myotherlist', 'three');
- test.expect(3);
+ test.expect(5);
vat.die();
test.done();
@@ -1090,20 +1166,22 @@ module.exports = simpleEvents({
var vat = EventVat();
- vat.on('rpushx', function(key, value) {
+ vat.on('rpushx', function(key, value, len) {
test.equal(key, 'mylist');
test.equal(value, 'two');
+ test.equal(len, 2);
});
- vat.on('rpushx mylist', function(value) {
+ vat.on('rpushx mylist', function(value, len) {
test.equal(value, 'two');
+ test.equal(len, 2);
});
vat.rpush('mylist', 'one');
vat.rpushx('mylist', 'two');
vat.rpushx('myotherlist', 'three');
- test.expect(3);
+ test.expect(5);
vat.die();
test.done();
@@ -1174,13 +1252,15 @@ module.exports = simpleEvents({
test.equal(value, 'two');
});
- vat.on('lpush', function(destination, value) {
+ vat.on('lpush', function(destination, value, len) {
test.equal(destination, 'mylist2');
test.equal(value, 'two');
+ test.equal(len, 2);
});
- vat.on('lpush mylist2', function(value) {
+ vat.on('lpush mylist2', function(value, len) {
test.equal(value, 'two');
+ test.equal(len, 2);
});
vat.rpush('mylist', 'one');
@@ -1188,7 +1268,7 @@ module.exports = simpleEvents({
vat.rpush('mylist2', 'three');
vat.rpoplpush('mylist', 'mylist2');
- test.expect(11);
+ test.expect(13);
vat.die();
test.done();
View
15 test/methods-test.js
@@ -74,6 +74,21 @@ this.methodSuite = {
test.done();
},
+ 'Invoke `getset` method and report returned value and stored value': function (test) {
+
+ var vat = EventVat();
+
+ test.equal(vat.get('foo'), null);
+ vat.set('foo', 1);
+ test.equal(vat.get('foo'), 1);
+
+ test.equal(vat.getset('foo', 2), 1);
+ test.equal(vat.get('foo'), 2);
+
+ vat.die();
+ test.done();
+
+ },
'Invoke `rename` method and get the value of the new key': function (test) {
var vat = EventVat();
Please sign in to comment.
Something went wrong with that request. Please try again.