Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial changes for Feathers v3 (#605)
* Refactoring for Feathers v3. Framework independent, hooks in core. * Further work on event dispatching * Finalize basic v3 functionality * Finalizing test coverage and initial v3 alpha * Update commonns dependency and prepare for 3.0 prereleases
- Loading branch information
Showing
31 changed files
with
1,202 additions
and
2,449 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,82 @@ | ||
import { EventEmitter } from 'events'; | ||
import Proto from 'uberproto'; | ||
|
||
export function eventHook () { | ||
return function (hook) { | ||
const { app, service } = hook; | ||
const eventName = app.eventMappings[hook.method]; | ||
const isHookEvent = service._hookEvents && service._hookEvents.indexOf(eventName) !== -1; | ||
|
||
// If this event is not being sent yet and we are not in an error hook | ||
if (eventName && isHookEvent && hook.type !== 'error') { | ||
service.emit(eventName, hook.result, hook); | ||
} | ||
}; | ||
} | ||
|
||
export function eventMixin (service) { | ||
if (service._serviceEvents) { | ||
return; | ||
} | ||
|
||
const app = this; | ||
// Indicates if the service is already an event emitter | ||
const isEmitter = typeof service.on === 'function' && | ||
typeof service.emit === 'function'; | ||
|
||
// If not, mix it in (the service is always an Uberproto object that has a .mixin) | ||
if (typeof service.mixin === 'function' && !isEmitter) { | ||
service.mixin(EventEmitter.prototype); | ||
} | ||
|
||
// Define non-enumerable properties of | ||
Object.defineProperties(service, { | ||
// A list of all events that this service sends | ||
_serviceEvents: { | ||
value: Array.isArray(service.events) ? service.events.slice() : [] | ||
}, | ||
|
||
// A list of events that should be handled through the event hooks | ||
_hookEvents: { | ||
value: [] | ||
} | ||
}); | ||
|
||
// `app.eventMappings` has the mapping from method name to event name | ||
Object.keys(app.eventMappings).forEach(method => { | ||
const event = app.eventMappings[method]; | ||
const alreadyEmits = service._serviceEvents.indexOf(event) !== -1; | ||
|
||
// Add events for known methods to _serviceEvents and _hookEvents | ||
// if the service indicated it does not send it itself yet | ||
if (typeof service[method] === 'function' && !alreadyEmits) { | ||
service._serviceEvents.push(event); | ||
service._hookEvents.push(event); | ||
} | ||
}); | ||
} | ||
|
||
export default function () { | ||
return function () { | ||
const app = this; | ||
|
||
// Mappings from service method to event name | ||
Object.assign(app, { | ||
eventMappings: { | ||
create: 'created', | ||
update: 'updated', | ||
remove: 'removed', | ||
patch: 'patched' | ||
} | ||
}); | ||
|
||
// Register the event hook | ||
// `finally` hooks always run last after `error` and `after` hooks | ||
app.hooks({ finally: eventHook() }); | ||
|
||
// Make the app an event emitter | ||
Proto.mixin(EventEmitter.prototype, app); | ||
|
||
app.mixins.push(eventMixin); | ||
}; | ||
} |
Oops, something went wrong.