Permalink
Browse files

[lib] Fixed the ability to reconstitute a vat from a JSON object

  • Loading branch information...
2 parents 6c61f26 + 8bda286 commit cb55595f882c772f54232cdd87605b8f04bb5406 Paolo Fragomeni committed Mar 22, 2012
Showing with 205 additions and 97 deletions.
  1. +69 −56 lib/eventvat.js
  2. +121 −41 test/events-test.js
  3. +15 −0 test/methods-test.js
View
@@ -21,7 +21,7 @@
//
function has(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
- };
+ }
EventVat = module.exports = function EventVat(conf) {
@@ -51,10 +51,10 @@
p.unsubscribe = EventEmitter2.prototype.removeListener;
p.die = function(key) {
- for (var key in this.hash) {
- if (has(this.hash, key)) {
- if (this.hash[key].tid) {
- clearTimeout(this.hash[key].tid);
+ for (var k in this.hash) {
+ if (has(this.hash, k)) {
+ if (this.hash[k].tid) {
+ clearTimeout(this.hash[k].tid);
}
}
}
@@ -147,7 +147,7 @@
}
}
- this.emit('keys', keys, regex);
+ this.emit('keys', regex, keys);
return keys;
};
@@ -164,7 +164,7 @@
return true;
}
return false;
- }
+ };
//
// Inspect the internals of EventVat objects
@@ -299,9 +299,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;
};
//
@@ -338,32 +339,37 @@
//
p.get = function(key) {
- var newValue;
+ var newValue = null;
if(has(this.hash, key)) {
- var newValue = this.hash[key].value;
+ newValue = this.hash[key].value;
this.emit('get ' + key, newValue);
this.emit('get', key, newValue);
- return newValue;
- }
- else {
- return null;
}
+
+ return newValue;
+
+ };
+
+ p.getDel = function (key) {
+ var newValue = p.get(key);
+ p.del(key);
+ this.emit('getdel ' + key, newValue);
+ this.emit('getdel', key, newValue);
};
p.gettime = function() {
- var newValue;
+ var newValue = null;
if(has(this.hash, key)) {
- var newValue = this.hash[key].time;
+ newValue = this.hash[key].time;
this.emit('gettime ' + key, newValue);
this.emit('gettime', key, newValue);
- return newValue;
- }
- else {
- return null;
}
+
+ return newValue;
+
};
//
@@ -379,8 +385,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;
@@ -393,6 +399,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;
};
@@ -431,11 +439,16 @@
// Get the values of all the given keys
//
p.mget = function() {
+ 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;
};
@@ -459,15 +472,15 @@
}
// set events must be emitted after keys are updated
- for(var i=0, l=arguments.length; i < l; i += 2) {
+ for(i=0, l=arguments.length; i < l; i += 2) {
key = arguments[i];
value = arguments[i + 1];
this.emit('set ' + key, value);
this.emit('set', key, value);
}
- var args = Array.prototype.slice.call(arguments)
+ var args = Array.prototype.slice.call(arguments);
args.unshift('mset');
this.emit.apply(this, args);
return true;
@@ -485,7 +498,7 @@
}
}
- for(var i=0, l=arguments.length; i < l; i += 2) {
+ for(i=0, l=arguments.length; i < l; i += 2) {
key = arguments[i];
value = arguments[i + 1];
@@ -494,7 +507,7 @@
}
}
- for(var i=0, l=arguments.length; i < l; i += 2) {
+ for(i=0, l=arguments.length; i < l; i += 2) {
key = arguments[i];
value = arguments[i + 1];
@@ -504,7 +517,7 @@
this.emit('setnx', key, value);
}
- var args = Array.prototype.slice.call(arguments)
+ var args = Array.prototype.slice.call(arguments);
args.unshift('msetnx');
this.emit.apply(this, args);
return true;
@@ -575,8 +588,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;
@@ -809,15 +822,15 @@
// set events must be emitted after keys are updated
var field, value;
- for(var i=1, l=arguments.length; i < l; i += 2) {
+ for(i=1, l=arguments.length; i < l; i += 2) {
field = arguments[i];
value = arguments[i + 1];
this.emit('hset ' + key, field, value);
this.emit('hset', key, field, value);
}
- var args = Array.prototype.slice.call(arguments)
+ var args = Array.prototype.slice.call(arguments);
this.emit.apply(this, ['hmset ' + key].concat(args.slice(1)));
this.emit.apply(this, ['hmset'].concat(args));
return true;
@@ -840,8 +853,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;
};
@@ -973,12 +986,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;
};
//
@@ -987,8 +1000,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;
@@ -1000,7 +1013,7 @@
//
p.lrange = function(key, start, stop) {
var range = this.type(key) === 'list'
- ? this.hash[key].value.slice(start, stop) : []
+ ? this.hash[key].value.slice(start, stop) : [];
this.emit('lrange ' + key, start, stop, range);
this.emit('lrange', key, start, stop, range);
@@ -1020,7 +1033,7 @@
// if count is positive, list will be traversed from 0 to list.length
if (ncount >= 0) {
i = 0;
- check = function() { return i < list.length };
+ check = function() { return i < list.length; };
incr = function() { i++; };
cancel = function() { i--; };
@@ -1132,13 +1145,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;
@@ -1163,12 +1176,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;
};
//
@@ -1177,8 +1190,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;
@@ -1240,7 +1253,7 @@
//
p.findin = function(key, value) {
if(has(this.hash, key)) {
- var index = this.hash[key].value.indexOf(value)
+ var index = this.hash[key].value.indexOf(value);
this.emit('findin ' + key, value, index);
this.emit('findin', key, value, index);
return index;
Oops, something went wrong.

0 comments on commit cb55595

Please sign in to comment.