This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from feathersjs/promises-28
Refactoring for hooks to use promises and promise chains
- Loading branch information
Showing
10 changed files
with
871 additions
and
762 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
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 |
---|---|---|
@@ -1,68 +1,55 @@ | ||
import makeDebug from 'debug'; | ||
import { hooks as utils } from 'feathers-commons'; | ||
import { makeHookFn, createMixin } from './commons'; | ||
import { addHookMethod, processHooks } from './commons'; | ||
|
||
const debug = makeDebug('feathers-hooks:after'); | ||
|
||
/** | ||
* Return the hook mixin method for the given name. | ||
* | ||
* @param {String} method The service method name | ||
* @returns {Function} | ||
*/ | ||
function getMixin(method) { | ||
return function() { | ||
var _super = this._super; | ||
|
||
if(!this.__after || !this.__after[method].length) { | ||
return _super.apply(this, arguments); | ||
} | ||
export default function(service) { | ||
if(typeof service.mixin !== 'function') { | ||
return; | ||
} | ||
|
||
const args = Array.from(arguments); | ||
const hookObject = utils.hookObject(method, 'after', args); | ||
const methods = this.methods; | ||
const old = service.after; | ||
|
||
// Make a copy of our hooks | ||
const hooks = this.__after[method].slice(); | ||
debug(`Running ${hooks.length} after hooks for method ${method}`); | ||
addHookMethod(service, 'after', methods); | ||
|
||
// Remove the old callback and replace with the new callback that runs the hook | ||
args.pop(); | ||
// The new _super method callback | ||
args.push(function(error, result) { | ||
if(error) { | ||
// Call the old callback with the error | ||
return hookObject.callback(error); | ||
} | ||
const mixin = {}; | ||
|
||
var fn = function(hookObject) { | ||
return hookObject.callback(null, hookObject.result); | ||
}; | ||
methods.forEach(method => { | ||
if(typeof service[method] !== 'function') { | ||
return; | ||
} | ||
|
||
// Set hookObject result | ||
hookObject.result = result; | ||
mixin[method] = function() { | ||
const originalCallback = arguments[arguments.length - 1]; | ||
|
||
while(hooks.length) { | ||
fn = makeHookFn(hooks.pop(), fn); | ||
} | ||
// Call the _super method which will return the `before` hook object | ||
return this._super.apply(this, arguments) | ||
// Make a copy of hookObject from `before` hooks and update type | ||
.then(hookObject => Object.assign({}, hookObject, { type: 'after' })) | ||
// Run through all `after` hooks | ||
.then(processHooks.bind(this, this.__afterHooks[method])) | ||
// Convert the results and call the original callback if available | ||
.then(hookObject => { | ||
const callback = hookObject.callback || originalCallback; | ||
|
||
return fn.call(this, hookObject); | ||
}.bind(this)); | ||
if(typeof callback === 'function') { | ||
hookObject.callback(null, hookObject.result); | ||
} | ||
|
||
return hookObject.result; | ||
}).catch(error => { | ||
const callback = (error && error.hook && error.hook.callback) || originalCallback; | ||
|
||
return _super.apply(this, args); | ||
}; | ||
} | ||
if(typeof callback === 'function') { | ||
callback(error); | ||
} | ||
|
||
function addHooks(hooks, method) { | ||
const myHooks = this.__after[method]; | ||
throw error; | ||
}); | ||
}; | ||
}); | ||
|
||
if(hooks[method]) { | ||
myHooks.push.apply(myHooks, hooks[method]); | ||
} | ||
service.mixin(mixin); | ||
|
||
if(hooks.all) { | ||
myHooks.push.apply(myHooks, hooks.all); | ||
if(old) { | ||
service.after(old); | ||
} | ||
} | ||
|
||
module.exports = createMixin('after', getMixin, addHooks); |
Oops, something went wrong.