Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shared instance fields #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
73 changes: 64 additions & 9 deletions src/__tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import assert from 'assert';
import autobind, {boundMethod, boundClass} from '..';

describe('autobind method decorator: @boundMethod', () => {
class A {
constructor() {
this.value = 42;
}
let A;

@boundMethod
getValue() {
return this.value;
}
}
beforeEach(() => {
A = class {
constructor(value = 42) {
this.value = value;
}

@boundMethod
getValue() {
return this.value;
}
};
});

test('binds methods to an instance', () => {
const a = new A();
Expand Down Expand Up @@ -68,6 +72,57 @@ describe('autobind method decorator: @boundMethod', () => {
assert(value === 50);
});

test('does not change prototype method when set on instance', () => {
const a = new A();

a.getValue = () => 'oops';

const b = new A();
assert.equal(b.getValue(), 42);
});

test('does not share instance fields between instances', () => {
const a = new A();

a.getValue = {
foo: 'bar'
};

assert.strictEqual(a.getValue, a.getValue);

const b = new A();
assert.notStrictEqual(a.getValue, b.getValue);
});

describe('set new value on prototype', () => {
let A;

beforeEach(() => {
A = class {
@boundMethod
noop() {
return 100;
}
}
})

test('allows changing of underlying method', () => {
A.prototype.noop = () => 200;

const a = new A();

assert.strictEqual(a.noop(), 200);
});

test('does binding on first access', () => {
const a = new A();

A.prototype.noop = () => 300;

assert.strictEqual(a.noop(), 300);
});
});

describe('set new value', () => {
class A {
constructor() {
Expand Down
67 changes: 48 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const OBJECT_INSTANCE = {};

/**
* Return a descriptor removing the value and returning a getter
* The getter will return a .bind version of the function
Expand All @@ -15,32 +17,59 @@ export function boundMethod(target, key, descriptor) {
// recursion and an "Out of stack space" error.
let definingProperty = false;

function reDefineProperty(instance, propertyValue, callGetter) {
let boundFn = null;
definingProperty = true;
let underlyingFn = propertyValue;
// Dummy object which is always strictly not equal to any other value
let currentlyBoundFn = OBJECT_INSTANCE;
const getter = function () {
if (currentlyBoundFn !== underlyingFn) {
if (typeof underlyingFn === 'function') {
boundFn = underlyingFn.bind(this);
} else {
boundFn = underlyingFn;
}
currentlyBoundFn = underlyingFn;
}
return boundFn;
};
Object.defineProperty(instance, key, {
configurable: true,
get: getter,
set(value) {
underlyingFn = value;
}
});
definingProperty = false;
if (callGetter) {
return getter.call(instance);
}
}

return {
configurable: true,
get() {
// eslint-disable-next-line no-prototype-builtins
if (definingProperty || this === target.prototype || this.hasOwnProperty(key) ||
typeof fn !== 'function') {
// If getter is called
// * definingProperty: in IE while defining property
// * this === target: on prototype itself Clazz.prototype[key]
// * Object.getPrototypeOf(this) !== target: getter is called on super
if (definingProperty || this === target || typeof fn !== 'function' || Object.getPrototypeOf(this) !== target) {
return fn;
}

const boundFn = fn.bind(this);
definingProperty = true;
Object.defineProperty(this, key, {
configurable: true,
get() {
return boundFn;
},
set(value) {
fn = value;
delete this[key];
}
});
definingProperty = false;
return boundFn;
return reDefineProperty(this, fn, true);
},
set(value) {
fn = value;
// accessing prototype
if (this === target) {
fn = value;
} else {
// If setter is called on instance we should create instance bound
// property here. We won't get into getter anymore
// setter on super behaves like on instance
reDefineProperty(this, value, false);
}
}
};
}
Expand Down Expand Up @@ -72,7 +101,7 @@ export function boundClass(target) {

// Only methods need binding
if (typeof descriptor.value === 'function') {
Object.defineProperty(target.prototype, key, boundMethod(target, key, descriptor));
Object.defineProperty(target.prototype, key, boundMethod(target.prototype, key, descriptor));
}
});
return target;
Expand Down