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

Support Promise-returning method and hooks #179

Merged
merged 1 commit into from
Feb 13, 2015
Merged
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
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"undef": true,
"laxcomma" : true,
"globals" : {
"Promise": true,
"it": false,
"describe": false,
"before": false,
Expand Down
5 changes: 4 additions & 1 deletion lib/remote-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ RemoteObjects.prototype.execHooks = function(when, method, scope, ctx, next) {

if (cur) {
try {
cur.call(scope, ctx, execStack, method);
var result = cur.call(scope, ctx, execStack, method);
if (result && typeof result.then === 'function') {
result.then(function() { next(); }, next);
}
} catch (err) {
next(err);
}
Expand Down
16 changes: 13 additions & 3 deletions lib/shared-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ SharedMethod.prototype.invoke = function(scope, args, remotingOptions, cb) {
var method = this.getFunction();
var sharedMethod = this;
var formattedArgs = [];
var result;

if (cb === undefined && typeof remotingOptions === 'function') {
cb = remotingOptions;
Expand Down Expand Up @@ -229,7 +228,7 @@ SharedMethod.prototype.invoke = function(scope, args, remotingOptions, cb) {
return cb(err);
}

result = SharedMethod.toResult(returns, [].slice.call(arguments, 1));
var result = SharedMethod.toResult(returns, [].slice.call(arguments, 1));

debug('- %s - result %j', sharedMethod.name, result);

Expand All @@ -243,7 +242,18 @@ SharedMethod.prototype.invoke = function(scope, args, remotingOptions, cb) {

// invoke
try {
return method.apply(scope, formattedArgs);
var retval = method.apply(scope, formattedArgs);
if (retval && typeof retval.then === 'function') {
return retval.then(
function(args) {
var result = SharedMethod.toResult(returns, args);
debug('- %s - promise result %j', sharedMethod.name, result);
cb(null, result);
},
cb // error handler
);
}
return retval;
} catch (err) {
debug('error caught during the invocation of %s', this.name);
return cb(err);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"xml2js": "^0.4.4"
},
"devDependencies": {
"bluebird": "^2.9.6",
"browserify": "~5.11.1",
"chai": "^1.10.0",
"grunt": "~0.4.5",
Expand Down
29 changes: 29 additions & 0 deletions test/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var express = require('express');
var request = require('supertest');
var expect = require('chai').expect;
var factory = require('./helpers/shared-objects-factory.js');
var Promise = global.Promise || require('bluebird');

var ACCEPT_XML_OR_ANY = 'application/xml,*/*;q=0.8';

Expand Down Expand Up @@ -1514,6 +1515,34 @@ describe('strong-remoting-rest', function() {
.expect(500)
.end(expectErrorResponseContaining({message: 'test-error'}, done));
});

it('should resolve promise returned by a hook', function(done) {
var method = givenSharedPrototypeMethod();
objects.before('**', function(ctx) {
return new Promise(function(resolve, reject) {
resolve('value-to-ignore');
});
});

json(method.url).expect(204).end(done);
});

it('should handle rejected promise returned by a hook', function(done) {
var testError = new Error('expected test error');
var method = givenSharedPrototypeMethod();
objects.after('**', function(ctx) {
return new Promise(function(resolve, reject) {
reject(testError);
});
});

json(method.url).expect(500).end(function(err, res) {
if (err) return done(err);
expect(res.body)
.to.have.deep.property('error.message', testError.message);
done();
});
});
});

it('returns 404 for unknown method of a shared class', function(done) {
Expand Down
54 changes: 48 additions & 6 deletions test/shared-method.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var extend = require('util')._extend;
var expect = require('chai').expect;
var SharedMethod = require('../lib/shared-method');
var factory = require('./helpers/shared-objects-factory.js');
var Promise = global.Promise || require('bluebird');

describe('SharedMethod', function() {
describe('sharedMethod.isDelegateFor(suspect, [isStatic])', function() {
Expand Down Expand Up @@ -83,13 +84,54 @@ describe('SharedMethod', function() {
});
});

function givenSharedMethod(options) {
var aFn = function() {
arguments[arguments.length - 1]();
};
it('resolves promise returned from the method', function(done) {
var method = givenSharedMethod(
function() {
return new Promise(function(resolve, reject) {
resolve(['one', 'two']);
});
},
{
returns: [
{ arg: 'first', type: 'string' },
{ arg: 'second', type: 'string' }
]
});

method.invoke('ctx', {}, function(err, result) {
setImmediate(function() {
expect(result).to.eql({ first: 'one', second: 'two' });
done();
});
});
});

it('handles rejected promise returned from the method', function(done) {
var testError = new Error('expected test error');
var method = givenSharedMethod(function() {
return new Promise(function(resolve, reject) {
reject(testError);
});
});

method.invoke('ctx', {}, function(err, result) {
setImmediate(function() {
expect(err).to.equal(testError);
done();
});
});
});

function givenSharedMethod(fn, options) {
if (options === undefined && typeof fn === 'object') {
options = fn;
fn = function() {
arguments[arguments.length - 1]();
};
}

var mockSharedClass = { fn: aFn };
return new SharedMethod(aFn, 'fn', mockSharedClass, options);
var mockSharedClass = { fn: fn };
return new SharedMethod(fn, 'fn', mockSharedClass, options);
}
});
});