|
1 | | -const debug = require('debug')('feathers-sync'); |
2 | | -const feathers = require('@feathersjs/feathers'); |
3 | | -const { _ } = require('@feathersjs/commons'); |
4 | | -const SYNC = Symbol('feathers-sync/enabled'); |
| 1 | +const debug = require('debug')('feathers-sync') |
| 2 | +const feathers = require('@feathersjs/feathers') |
| 3 | +const { _ } = require('@feathersjs/commons') |
| 4 | +const SYNC = Symbol('feathers-sync/enabled') |
5 | 5 |
|
6 | | -const defaultEvents = ['created', 'updated', 'removed', 'patched']; |
| 6 | +const defaultEvents = ['created', 'updated', 'removed', 'patched'] |
7 | 7 | const getServiceOptions = service => { |
8 | 8 | if (typeof feathers.getServiceOptions === 'function') { |
9 | | - return feathers.getServiceOptions(service); |
| 9 | + return feathers.getServiceOptions(service) |
10 | 10 | } |
11 | 11 |
|
12 | | - return {}; |
13 | | -}; |
| 12 | + return {} |
| 13 | +} |
14 | 14 |
|
15 | 15 | module.exports = app => { |
16 | 16 | if (app[SYNC]) { |
17 | | - return; |
| 17 | + return |
18 | 18 | } |
19 | 19 |
|
20 | | - app[SYNC] = true; |
| 20 | + app[SYNC] = true |
21 | 21 |
|
22 | 22 | if (app.sync) { |
23 | | - throw new Error('Only one type of feathers-sync can be configured on the same application'); |
| 23 | + throw new Error('Only one type of feathers-sync can be configured on the same application') |
24 | 24 | } |
25 | 25 |
|
26 | 26 | app.on('sync-in', (rawData) => { |
27 | | - const { event, path, data, context } = app.sync.deserialize(rawData); |
28 | | - const service = app.service(path); |
| 27 | + const { event, path, data, context } = app.sync.deserialize(rawData) |
| 28 | + const service = app.service(path) |
29 | 29 | const hook = context |
30 | 30 | ? Object.assign({ app, service }, context) |
31 | | - : context; |
| 31 | + : context |
32 | 32 |
|
33 | 33 | if (service) { |
34 | | - debug(`Dispatching sync-in event '${path} ${event}'`); |
35 | | - service._emit(event, data, hook); |
| 34 | + debug(`Dispatching sync-in event '${path} ${event}'`) |
| 35 | + service._emit(event, data, hook) |
36 | 36 | } else { |
37 | | - debug(`Invalid sync event '${path} ${event}'`); |
| 37 | + debug(`Invalid sync event '${path} ${event}'`) |
38 | 38 | } |
39 | | - }); |
| 39 | + }) |
40 | 40 |
|
41 | 41 | app.mixins.push((service, path) => { |
42 | 42 | if (typeof service._emit !== 'function') { |
43 | | - const { events: customEvents = service.events } = getServiceOptions(service); |
44 | | - const events = defaultEvents.concat(customEvents); |
| 43 | + const { events: customEvents = service.events } = getServiceOptions(service) |
| 44 | + const events = defaultEvents.concat(customEvents) |
45 | 45 |
|
46 | | - service._emit = service.emit; |
| 46 | + service._emit = service.emit |
47 | 47 | service.emit = function (event, data, ctx) { |
48 | | - const disabled = ctx && ctx[SYNC] === false; |
| 48 | + const disabled = ctx && ctx[SYNC] === false |
49 | 49 |
|
50 | 50 | if (!events.includes(event) || disabled) { |
51 | | - debug(`Passing through non-service event '${path} ${event}'`); |
52 | | - return this._emit(event, data, ctx); |
| 51 | + debug(`Passing through non-service event '${path} ${event}'`) |
| 52 | + return this._emit(event, data, ctx) |
53 | 53 | } |
54 | 54 |
|
55 | | - const serializedContext = ctx && typeof ctx.toJSON === 'function' ? ctx.toJSON() : ctx; |
| 55 | + const serializedContext = ctx && typeof ctx.toJSON === 'function' ? ctx.toJSON() : ctx |
56 | 56 | const context = ctx && (ctx.app === app || ctx.service === service) |
57 | 57 | ? _.omit(serializedContext, 'app', 'service', 'self') |
58 | | - : serializedContext; |
| 58 | + : serializedContext |
59 | 59 |
|
60 | | - debug(`Sending sync-out event '${path} ${event}'`); |
| 60 | + debug(`Sending sync-out event '${path} ${event}'`) |
61 | 61 |
|
62 | 62 | return app.emit('sync-out', app.sync.serialize({ |
63 | 63 | event, path, data, context |
64 | | - })); |
65 | | - }; |
| 64 | + })) |
| 65 | + } |
66 | 66 | } |
67 | | - }); |
68 | | -}; |
| 67 | + }) |
| 68 | +} |
69 | 69 |
|
70 | | -module.exports.SYNC = SYNC; |
| 70 | +module.exports.SYNC = SYNC |
0 commit comments