Skip to content

Commit

Permalink
fix: use better-performing means of setting up chainable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Dec 18, 2020
1 parent 56d2795 commit 9381d2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
24 changes: 10 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,24 @@ module.exports = (chai, utils) => {
if (isChainableMethod(getterName)) {
Object.defineProperty(WaitFor.prototype, getterName, {
get() {
const obj = new WaitFor(this.options, () => {
const gotten = new WaitFor(this.options, () => {
const assertion = this.buildAssertion()
return assertion[getterName]
})
function chainable() {
function chainableMethodWrapper() {
return new WaitFor(this.options, () => {
const assertion = this.buildAssertion()
return assertion[getterName].apply(assertion, arguments)
})
}
for (const methodName of methodNames) {
chainable[methodName] = obj[methodName].bind(obj)
}
for (const getterName of getterNames) {
Object.defineProperty(chainable, getterName, {
get() {
return obj[getterName]
},
configurable: true,
})
}
return chainable
// Inherit all properties from the object by replacing the `Function` prototype
var prototype = Object.create(gotten)
// Restore the `call` and `apply` methods from `Function`
prototype.call = Function.prototype.call
prototype.apply = Function.prototype.apply
Object.setPrototypeOf(chainableMethodWrapper, prototype)

return chainableMethodWrapper
},
configurable: true,
})
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('waitFor', function () {
'2',
'3'
),
clock.tickAsync(501),
clock.tickAsync(1001),
])
expect(i).to.equal(4)
})
Expand Down

0 comments on commit 9381d2c

Please sign in to comment.