Skip to content

Commit

Permalink
fixed infinite recursion in Lazy({}).watch (see #49)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtao committed Dec 20, 2013
1 parent ecc0f35 commit 4e2e8c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4466,9 +4466,11 @@
index = 0;

Lazy(propertyNames).each(function(propertyName) {
var propertyValue = object[propertyName];

Object.defineProperty(object, propertyName, {
get: function() {
return object[propertyName];
return propertyValue;
},

set: function(value) {
Expand All @@ -4477,6 +4479,7 @@
listeners.splice(i, 1);
}
}
propertyValue = value;
++index;
}
});
Expand Down
19 changes: 19 additions & 0 deletions spec/watch_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ describe("Lazy", function() {
expect(callback.calls[1].args).toEqual([{ property: 'bar', value: 2}, 1]);
});

it("does not exhibit infinite recursion on accesses (!)", function() {
var object = { foo: 1 };
Lazy(object).watch(['foo']).each(Lazy.noop);
expect(function() { var foo = object.foo; }).not.toThrow();
});

it("does not override the original value", function() {
var object = { foo: 1 };
Lazy(object).watch('foo').each(Lazy.noop);
expect(object.foo).toBe(1);
});

it("properly updates the value on sets", function() {
var object = { foo: 1 };
Lazy(object).watch('foo').each(Lazy.noop);
object.foo = 2;
expect(object.foo).toBe(2);
});

it("provides access to map, filter, etc.", function() {
function upcase(str) {
return str.toUpperCase();
Expand Down

0 comments on commit 4e2e8c2

Please sign in to comment.