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