Skip to content

Commit

Permalink
v1.1.3 addAsync fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
taburetkin committed Dec 18, 2019
1 parent c59b477 commit 21c9868
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
36 changes: 36 additions & 0 deletions test/unit/utils.spec.js
Expand Up @@ -477,6 +477,42 @@ describe('# utils:', function() {
});
});

describe('context of wrapped method', function() {
let context;
let instance;
let spy1;
let spy2;
beforeEach(() => {
spy1 = sinon.spy();
spy2 = sinon.spy();
context = {
method: spy1,
};
const Cls = function() {};
Cls.prototype = {
method: spy2,
}
addAsync(context, 'method');
instance = new Cls();
addAsync(instance, 'method');
});
it('should use correct `this` when context is a plain object', async () => {
expect(typeof context.methodAsync, 'has async method').to.be.equal('function');
let res = await context.methodAsync();
expect(spy1, 'called once').to.be.calledOnce;
expect(res, 'instanceof AsyncResult').to.be.instanceOf(AsyncResult);
let call = spy1.getCall(0);
expect(call, 'correct context').calledOn(context);
});
it('should use correct `this` when context is an instance of a class', async () => {
expect(typeof instance.methodAsync, 'has async method').to.be.equal('function');
let res = await instance.methodAsync();
expect(spy2, 'called once').to.be.calledOnce;
expect(res, 'instanceof AsyncResult').to.be.instanceOf(AsyncResult);
let call = spy2.getCall(0);
expect(call, 'correct context').calledOn(instance);
});
});

});
});
13 changes: 10 additions & 3 deletions utils.js
Expand Up @@ -46,15 +46,20 @@ function toAsyncResult(promise, OwnAsyncResult) {
* @param {object} [options] - You may specify context and AsyncResult for new method through options
* @return {function} - wrapped method
*/
function wrapMethod(method, { context, AsyncResult } = {}) {
function wrapMethod(method, { context, AsyncResult, prototypeMethod, methodName } = {}) {
if (typeof method !== 'function') {
throw new Error('first argument should be a function');
throw new Error('first argument should be a function or context should be provided');
}
!AsyncResult && (AsyncResult = config.AsyncResult);

let asyncMethod = function() {
try {
let result = method.apply(this, arguments);
let result;
if (methodName) {
result = this[methodName].apply(this, arguments);
} else {
result = method.apply(this, arguments);
}
return toAsyncResult(result, AsyncResult);
} catch (error) {
return toAsyncResult(error, AsyncResult);
Expand All @@ -81,8 +86,10 @@ function addAsync(context, methodName, options = {}) {
}
} else {
let isCtor = typeof context === 'function';
options.methodName = methodName;
if (isCtor && !options.static) {
context = context.prototype
options.prototypeMethod = true;
}
if (!isCtor && options.context === void 0) {
options.context = context;
Expand Down

0 comments on commit 21c9868

Please sign in to comment.