-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow services to return a promise instead of calling the callback (#41…
…).
- Loading branch information
Showing
7 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = [ | ||
require('./event') | ||
require('./promise'), | ||
require('./event') | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var makeWrapper = function() { | ||
return function() { | ||
var result = this._super.apply(this, arguments); | ||
var callback = arguments[arguments.length - 1]; | ||
|
||
if(typeof result !== 'undefined' && _.isFunction(result.then) && _.isFunction(callback)) { | ||
result.then(function(data) { | ||
callback(null, data); | ||
}, function(error) { | ||
callback(error); | ||
}); | ||
} | ||
return result; | ||
}; | ||
}; | ||
|
||
module.exports = function (service) { | ||
if (typeof service.mixin === 'function') { | ||
var mixin = _.transform(_.pick(service, this.methods), function(result, num, key) { | ||
result[key] = makeWrapper(); | ||
}); | ||
|
||
service.mixin(mixin); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var Proto = require('uberproto'); | ||
var q = require('q'); | ||
var _ = require('lodash'); | ||
|
||
var mixin = require('../../lib/mixins/promise'); | ||
|
||
describe('Promises/A+ mixin', function () { | ||
it('Calls a callback when a promise is returned from the original service', function (done) { | ||
// A dummy context (this will normally be the application) | ||
var context = { | ||
methods: ['get'] | ||
}; | ||
var FixtureService = Proto.extend({ | ||
get: function (id) { | ||
return q({ | ||
id: id, | ||
description: 'You have to do ' + id | ||
}); | ||
} | ||
}); | ||
|
||
mixin.call(context, FixtureService); | ||
|
||
var instance = Proto.create.call(FixtureService); | ||
instance.get('dishes', {}, function (error, data) { | ||
assert.deepEqual(data, { | ||
id: 'dishes', | ||
description: 'You have to do dishes' | ||
}); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('calls back with an error for a failing deferred', function(done) { | ||
// A dummy context (this will normally be the application) | ||
var context = { | ||
methods: ['get'] | ||
}; | ||
var FixtureService = Proto.extend({ | ||
get: function () { | ||
var dfd = q.defer(); | ||
|
||
_.defer(function() { | ||
dfd.reject(new Error('Something went wrong')); | ||
}); | ||
|
||
return dfd.promise; | ||
} | ||
}); | ||
|
||
mixin.call(context, FixtureService); | ||
|
||
var instance = Proto.create.call(FixtureService); | ||
instance.get('dishes', {}, function (error) { | ||
assert.ok(error); | ||
assert.equal(error.message, 'Something went wrong'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('does not try to call the callback if it does not exist', function(done) { | ||
// A dummy context (this will normally be the application) | ||
var context = { | ||
methods: ['create'] | ||
}; | ||
var FixtureService = Proto.extend({ | ||
create: function (data) { | ||
var dfd = q.defer(); | ||
|
||
_.defer(function() { | ||
dfd.resolve(data); | ||
}); | ||
|
||
return dfd.promise; | ||
} | ||
}); | ||
var original = { | ||
id: 'laundry', | ||
description: 'You have to do laundry' | ||
}; | ||
|
||
mixin.call(context, FixtureService); | ||
|
||
var instance = Proto.create.call(FixtureService); | ||
instance.create(original, {}).then(function(data) { | ||
assert.deepEqual(data, original); | ||
done(); | ||
}); | ||
}); | ||
}); |