Skip to content
This repository was archived by the owner on Sep 29, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/decorate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ function handleDescriptor(target, key, descriptor, [decorator, ...args]) {
get() {
const fn = isGetter ? originalGet.call(this) : originalValue;
const value = decorator.call(this, fn, ...args);
const desc = {
configurable,
enumerable,
value,
writable
};

if (isGetter) {
return value;
} else {
const desc = {
configurable,
enumerable
};

desc.value = value;
desc.writable = writable;
desc.writable = true;
}

Object.defineProperty(this, key, desc);
Object.defineProperty(this, key, desc);

return value;
}
return value;
},
set: isGetter ? originalSet : createDefaultSetter()
};
Expand Down
38 changes: 38 additions & 0 deletions test/unit/decorate.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import autobind from '../../lib/autobind';
import decorate from '../../lib/decorate';
import { memoize } from 'lodash';

Expand Down Expand Up @@ -38,6 +39,13 @@ describe('@decorate', function () {
callCount++;
return this;
}

@decorate(memoize)
@autobind
getFooAutobound() {
callCount++;
return this;
}
}
});

Expand Down Expand Up @@ -66,9 +74,39 @@ describe('@decorate', function () {
foo1Result.should.not.equal(foo2Result);
foo1Result.should.equal(foo1);
foo2Result.should.equal(foo2);
});

it('is applied in a way that allows memoization', function () {
const foo1 = new Foo();
const foo2 = new Foo();

const foo1Result = foo1.getFoo();
const foo2Result = foo2.getFoo();

foo1.getFoo();

callCount.should.equal(2);

foo1.getFoo(1);
foo1.getFoo(2);

callCount.should.equal(4);
});

it('is applied in a way that allows memoization when autobound', function () {
const foo1 = new Foo();
const foo2 = new Foo();

const foo1Result = foo1.getFooAutobound();
const foo2Result = foo2.getFooAutobound();

foo1.getFooAutobound();

callCount.should.equal(2);

foo1.getFooAutobound(1);
foo1.getFooAutobound(2);

callCount.should.equal(4);
});
});