-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
131 lines (108 loc) · 3.07 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Meteor } from 'meteor/meteor';
import { DDPCommon } from 'meteor/ddp-common';
const SUBSCRIPTION_NAME = '__graphql_subscription__';
const SUBSCRIPTION_METHOD_NAME = '__graphql_subscribe__';
const UNSUBSCRIPTION_METHOD_NAME = '__graphql_unsubscribe__';
export class Client {
constructor() {
this.connected = false;
this.maxId = 0;
this.subscriptions = {};
Meteor.connection._stream.on('message', rawMessage => {
const message = DDPCommon.parseDDP(rawMessage);
if (this.connected && message.msg === 'connected') {
this.reconnect();
}
if (message.msg === 'changed' && message.collection === SUBSCRIPTION_NAME) {
const id = parseInt(message.id);
const payload = message.fields;
if (this.subscriptions[id]) {
if (payload.errors) {
this.subscriptions[id].handler(payload.errors, null);
} else {
this.subscriptions[id].handler(null, payload.data);
}
}
}
});
}
/**
* @private
*/
connect() {
if (!this.connected) {
return new Promise((resolve) => {
Meteor.subscribe(SUBSCRIPTION_NAME, () => {
this.connected = true;
resolve();
});
});
}
return Promise.resolve();
}
/**
* @private
*/
reconnect() {
Object.keys(this.subscriptions).forEach(subId => {
const { query, variables, operationName, context, handler } = this.subscriptions[subId];
this.subscribe({ query, variables, operationName, context, subscriptionId: subId }, handler);
});
}
/**
* @param {String} query
* @param {Object} [variables]
* @param {String} operationName
* @param {Object} [context]
* @param {Number} [subscriptionId]
* @param {Function} handler
* @returns {Number}
*/
async subscribe({query, variables = {}, operationName, context, subscriptionId = null }, handler) {
await this.connect();
if (!query) {
throw new Error('Must provice `query` to subscribe.');
}
if (!handler) {
throw new Error('Must provide `handler` to subscribe.');
}
const subId = subscriptionId || this.generateSubscriptionId();
const message = Object.assign({
query,
variables,
operationName,
context
}, { id: subId });
this.subscriptions[subId] = { query, variables, operationName, context, handler };
Meteor.call(SUBSCRIPTION_METHOD_NAME, message, (error) => {
if (error) {
handler([new Error(`Subscription failed: ${error.message}`)]);
this.unsubscribe(subId);
}
});
return subId;
}
/**
* @returns {Number}
* @private
*/
generateSubscriptionId() {
const id = this.maxId;
this.maxId += 1;
return id;
}
/**
* @param {Number} id
*/
async unsubscribe(id) {
await this.connect();
delete this.subscriptions[id];
Meteor.call(UNSUBSCRIPTION_METHOD_NAME, { id });
}
/**
*
*/
unsubsribeAll() {
Object.keys(this.subscriptions).forEach(subId => this.unsubscribe(parseInt(subId)));
}
}