8000 Add more inline documentation by daffl · Pull Request #703 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
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
7 changes: 4 additions & 3 deletions lib/events.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { EventEmitter } = require('events');
const Proto = require('uberproto');

// Returns a hook that emits service events. Should always be
// used as the very last hook in the chain
const eventHook = exports.eventHook = function eventHook () {
return function (hook) {
const { app, service } = hook;
Expand All @@ -14,6 +16,7 @@ const eventHook = exports.eventHook = function eventHook () {
};
};

// Mixin that turns a service into a Node event emitter
const eventMixin = exports.eventMixin = function eventMixin (service) {
if (service._serviceEvents) {
return;
Expand Down Expand Up @@ -57,9 +60,7 @@ const eventMixin = exports.eventMixin = function eventMixin (service) {
};

module.exports = function () {
return function () {
const app = this;

return function (app) {
// Mappings from service method to event name
Object.assign(app, {
eventMappings: {
Expand Down
22 changes: 14 additions & 8 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
createHookObject, getHooks, processHooks, enableHooks, makeArguments
} = hooks;

// A service mixin that adds `service.hooks()` method and functionality
const hookMixin = exports.hookMixin = function hookMixin (service) {
if (typeof service.hooks === 'function') {
return;
Expand All @@ -30,6 +31,8 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
const service = this;
const args = arguments;

// We have to try/catch this so that argument validation
// returns a rejected promise
try {
validateArguments(method, args);
} catch (e) {
Expand Down Expand Up @@ -68,28 +71,30 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
return hookObject;
});
})
// Make a copy of hookObject from `before` hooks and update type
.then(hookObject =>
Object.assign({}, hookObject, { type: 'after' })
)
// Make a (shallow) copy of hookObject from `before` hooks and update type
.then(hookObject => Object.assign({}, hookObject, { type: 'after' }))
// Run through all `after` hooks
.then(hookObject => {
// Combine all app and service `after` and `finally` hooks and process
const afterHooks = getHooks(app, service, 'after', method, true);
const finallyHooks = getHooks(app, service, 'finally', method, true);
const hookChain = afterHooks.concat(finallyHooks);

return processHooks.call(service, hookChain, hookObject);
})
// Finally, return the result (or the hook object if a hidden flag is set)
.then(hookObject =>
// Finally, return the result
// Or the hook object if the `__returnHook` flag is set
hookObject.params.__returnHook ? hookObject : hookObject.result
)
// Handle errors
.catch(error => {
// Combine all app and service `error` and `finally` hooks and process
const errorHooks = getHooks(app, service, 'error', method, true);
const finallyHooks = getHooks(app, service, 'finally', method, true);
const hookChain = errorHooks.concat(finallyHooks);

// A shallow copy of the hook object
const errorHookObject = Object.assign({}, error.hook || hookObject, {
type: 'error',
result: null,
Expand All @@ -101,11 +106,14 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
.call(service, hookChain, errorHookObject)
.then(hook => {
if (errorHookObject.params.__returnHook) {
// Return either the complete hook if the `__returnHook` flag is set
return Promise.reject(hook);
} else if (hook.result) {
// Return the result if it is set so you can swallow errors
return Promise.resolve(hook.result);
}

// If none of the above, return the error
return Promise.reject(hook.error);
});
});
Expand All @@ -116,9 +124,7 @@ const hookMixin = exports.hookMixin = function hookMixin (service) {
};

module.exports = function () {
return function () {
const app = this;

return function (app) {
// We store a reference of all supported hook types on the app
// in case someone needs it
Object.assign(app, {
Expand Down
2 changes: 2 additions & 0 deletions lib/version.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// Do not change
// This version will be updated automatically when publishing
module.exports = '3.0.0-pre.2';
0