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.
I think this is the most elegant place to add the app object so that it's always available, even on internal requests that don't pass through one of the providers. The next issue that will surface is probably circular requests where service A requests data from B, B from C, and C from A.
- Loading branch information
1 parent
344bce1
commit b3475d1
Showing
3 changed files
with
76 additions
and
43 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,54 +1,58 @@ | ||
import { hooks as utils } from 'feathers-commons'; | ||
import { addHookMethod, processHooks } from './commons'; | ||
|
||
export default function(service) { | ||
if(typeof service.mixin !== 'function') { | ||
return; | ||
} | ||
export default function(app) { | ||
return function(service){ | ||
if(typeof service.mixin !== 'function') { | ||
return; | ||
} | ||
|
||
const methods = this.methods; | ||
const old = service.before; | ||
const mixin = {}; | ||
const methods = this.methods; | ||
const old = service.before; | ||
const mixin = {}; | ||
|
||
addHookMethod(service, 'before', methods); | ||
addHookMethod(service, 'before', methods); | ||
|
||
methods.forEach(method => { | ||
if(typeof service[method] !== 'function') { | ||
return; | ||
} | ||
methods.forEach(method => { | ||
if(typeof service[method] !== 'function') { | ||
return; | ||
} | ||
|
||
mixin[method] = function() { | ||
const _super = this._super.bind(this); | ||
const hookObject = utils.hookObject(method, 'before', arguments); | ||
const hooks = this.__beforeHooks[method]; | ||
|
||
// Run all hooks | ||
let promise = processHooks.call(this, hooks, hookObject); | ||
|
||
// Then call the original method | ||
return promise.then(hookObject => { | ||
return new Promise((resolve, reject) => { | ||
const args = utils.makeArguments(hookObject); | ||
|
||
// We replace the callback with resolving the promise | ||
args.splice(args.length - 1, 1, (error, result) => { | ||
if(error) { | ||
reject(error); | ||
} else { | ||
hookObject.result = result; | ||
resolve(hookObject); | ||
} | ||
}); | ||
mixin[method] = function() { | ||
const _super = this._super.bind(this); | ||
const hookObject = utils.hookObject(method, 'before', arguments); | ||
const hooks = this.__beforeHooks[method]; | ||
|
||
hookObject.app = app; | ||
|
||
_super(... args); | ||
// Run all hooks | ||
let promise = processHooks.call(this, hooks, hookObject); | ||
|
||
// Then call the original method | ||
return promise.then(hookObject => { | ||
return new Promise((resolve, reject) => { | ||
const args = utils.makeArguments(hookObject); | ||
|
||
// We replace the callback with resolving the promise | ||
args.splice(args.length - 1, 1, (error, result) => { | ||
if(error) { | ||
reject(error); | ||
} else { | ||
hookObject.result = result; | ||
resolve(hookObject); | ||
} | ||
}); | ||
|
||
_super(... args); | ||
}); | ||
}); | ||
}); | ||
}; | ||
}); | ||
}; | ||
}); | ||
|
||
service.mixin(mixin); | ||
service.mixin(mixin); | ||
|
||
if(old) { | ||
service.before(old); | ||
} | ||
if(old) { | ||
service.before(old); | ||
} | ||
}; | ||
} |
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