|
1 | 1 | const debug = require('debug')('feathers-sync'); |
2 | | -const { hooks, _ } = require('@feathersjs/commons'); |
| 2 | +const feathers = require('@feathersjs/feathers'); |
| 3 | +const { _ } = require('@feathersjs/commons'); |
3 | 4 | const SYNC = Symbol('feathers-sync/enabled'); |
4 | 5 |
|
| 6 | +const defaultEvents = ['created', 'updated', 'removed', 'patched']; |
| 7 | +const getServiceOptions = service => { |
| 8 | + if (typeof feathers.getServiceOptions === 'function') { |
| 9 | + return feathers.getServiceOptions(service); |
| 10 | + } |
| 11 | + |
| 12 | + return {}; |
| 13 | +}; |
| 14 | + |
5 | 15 | module.exports = app => { |
6 | 16 | if (app[SYNC]) { |
7 | 17 | return; |
@@ -30,17 +40,20 @@ module.exports = app => { |
30 | 40 |
|
31 | 41 | app.mixins.push((service, path) => { |
32 | 42 | if (typeof service._emit !== 'function') { |
| 43 | + const { events: customEvents = service.events } = getServiceOptions(service); |
| 44 | + const events = defaultEvents.concat(customEvents); |
| 45 | + |
33 | 46 | service._emit = service.emit; |
34 | 47 | service.emit = function (event, data, ctx) { |
35 | 48 | const disabled = ctx && ctx[SYNC] === false; |
36 | 49 |
|
37 | | - if (!service._serviceEvents.includes(event) || disabled) { |
| 50 | + if (!events.includes(event) || disabled) { |
38 | 51 | debug(`Passing through non-service event '${path} ${event}'`); |
39 | 52 | return this._emit(event, data, ctx); |
40 | 53 | } |
41 | 54 |
|
42 | | - const context = hooks.isHookObject(ctx) |
43 | | - ? _.omit(ctx, 'app', 'service') |
| 55 | + const context = ctx && (ctx.app === app || ctx.service === service) |
| 56 | + ? _.omit(ctx, 'app', 'service', 'self') |
44 | 57 | : ctx; |
45 | 58 |
|
46 | 59 | debug(`Sending sync-out event '${path} ${event}'`); |
|
0 commit comments