-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.js
36 lines (30 loc) · 1.01 KB
/
client.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
import { getHooksAfter, getHooksBefore } from './common';
import MethodInvoker from 'meteor/ddp-client/common/MethodInvoker.js';
const originalSend = MethodInvoker.prototype.sendMessage;
const originalReceive = MethodInvoker.prototype.receiveResult;
MethodInvoker.prototype.sendMessage = function sendMessage() {
if (this._message.msg === 'method') {
const beforeFns = getHooksBefore(this._message.method);
for (const beforeFn of beforeFns) {
if (beforeFn.apply(this, this._message.params) === false) {
return false;
}
}
}
return originalSend.call(this);
};
MethodInvoker.prototype.receiveResult = function receiveResult(error, result) {
const context = {
error,
result,
};
if (!this.gotResult()) {
const afterFns = getHooksAfter(this._message.method);
for (const afterFn of afterFns) {
try { afterFn.apply(context, this._message.params); } catch (err) {
console.error(err);
}
}
}
originalReceive.call(this, context.error, context.result);
};