Skip to content

Commit

Permalink
getset emits event
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Feb 15, 2012
1 parent 8d0172c commit d8ec7dc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/eventvat.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@
EventVat.prototype.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;
};

Expand Down
22 changes: 22 additions & 0 deletions test/events-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions test/methods-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d8ec7dc

Please sign in to comment.