forked from ef2k/meteor-ddp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteor-ddp.js
231 lines (197 loc) · 5.53 KB
/
meteor-ddp.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* MeteorDdp - a client for DDP version pre1 */
var MeteorDdp = function(wsUri) {
this.VERSIONS = ["pre1"];
this.wsUri = wsUri;
this.sock;
this.defs = {}; // { deferred_id => deferred_object }
this.subs = {}; // { pub_name => deferred_id }
this.watchers = {}; // { coll_name => [cb1, cb2, ...] }
this.collections = {}; // { coll_name => {docId => {doc}, docId => {doc}, ...} }
};
MeteorDdp.prototype._Ids = function() {
var count = 0;
return {
next: function() {
return ++count + '';
}
}
}();
MeteorDdp.prototype.connect = function() {
var self = this;
var conn = new $.Deferred();
self.sock = new WebSocket(self.wsUri);
self.sock.onopen = function() {
self.send({
msg: 'connect',
version: self.VERSIONS[0],
support: self.VERSIONS
});
};
self.sock.onerror = function(err) {
conn.reject(err);
};
self.sock.onmessage = function(msg) {
var data = JSON.parse(msg.data);
switch (data.msg) {
case 'connected':
conn.resolve(data);
break;
case 'result':
self._resolveCall(data);
break;
case 'updated':
// TODO method call was acked
break;
case 'changed':
self._changeDoc(data);
break;
case 'added':
self._addDoc(data);
break;
case 'removed':
self._removeDoc(data);
break;
case 'ready':
self._resolveSubs(data);
break;
case 'nosub':
self._resolveNoSub(data);
break;
case 'addedBefore':
self._addDoc(data);
break;
case 'movedBefore':
// TODO
break;
}
};
return conn.promise();
};
MeteorDdp.prototype._resolveNoSub = function(data) {
if (data.error) {
var error = data.error;
this.defs[data.id].reject(error.reason || 'Subscription not found');
} else {
this.defs[data.id].resolve();
}
};
MeteorDdp.prototype._resolveCall = function(data) {
if (data.error) {
this.defs[data.id].reject(data.error.reason);
} else if (typeof data.result !== 'undefined') {
this.defs[data.id].resolve(data.result);
}
};
MeteorDdp.prototype._resolveSubs = function(data) {
var subIds = data.subs;
for (var i = 0; i < subIds.length; i++) {
this.defs[subIds[i]].resolve();
}
};
MeteorDdp.prototype._changeDoc = function(msg) {
var collName = msg.collection;
var id = msg.id;
var fields = msg.fields;
var cleared = msg.cleared;
var coll = this.collections[collName];
if (fields) {
for (var k in fields) {
coll[id][k] = fields[k];
}
} else if (cleared) {
for (var i = 0; i < cleared.length; i++) {
var fieldName = cleared[i];
delete coll[id][fieldName];
}
}
var changedDoc = coll[id];
this._notifyWatchers(collName, changedDoc, id, msg.msg);
};
MeteorDdp.prototype._addDoc = function(msg) {
var collName = msg.collection;
var id = msg.id;
if (!this.collections[collName]) {
this.collections[collName] = {};
}
/* NOTE: Ordered docs will have a 'before' field containing the id of
* the doc after it. If it is the last doc, it will be null.
*/
this.collections[collName][id] = msg.fields;
var changedDoc = this.collections[collName][id];
this._notifyWatchers(collName, changedDoc, id, msg.msg);
};
MeteorDdp.prototype._removeDoc = function(msg) {
var collName = msg.collection;
var id = msg.id;
var doc = this.collections[collName][id];
var docCopy = JSON.parse(JSON.stringify(doc));
delete this.collections[collName][id];
this._notifyWatchers(collName, docCopy, id, msg.msg);
};
MeteorDdp.prototype._notifyWatchers = function(collName, changedDoc, docId, message) {
changedDoc = JSON.parse(JSON.stringify(changedDoc)); // make a copy
changedDoc._id = docId; // id might be useful to watchers, attach it.
if (!this.watchers[collName]) {
this.watchers[collName] = [];
} else {
for (var i = 0; i < this.watchers[collName].length; i++) {
this.watchers[collName][i](changedDoc, message);
}
}
};
MeteorDdp.prototype._deferredSend = function(actionType, name, params) {
var id = this._Ids.next();
this.defs[id] = new $.Deferred();
var args = params || [];
var o = {
msg: actionType,
params: args,
id: id
};
if (actionType === 'method') {
o.method = name;
} else if (actionType === 'sub') {
o.name = name;
this.subs[name] = id;
}
this.send(o);
return this.defs[id].promise();
};
MeteorDdp.prototype.call = function(methodName, params) {
return this._deferredSend('method', methodName, params);
};
MeteorDdp.prototype.subscribe = function(pubName, params) {
return this._deferredSend('sub', pubName, params);
};
MeteorDdp.prototype.unsubscribe = function(pubName) {
this.defs[id] = new $.Deferred();
if (!this.subs[pubName]) {
this.defs[id].reject(pubName + " was never subscribed");
} else {
var id = this.subs[pubName];
var o = {
msg: 'unsub',
id: id
};
this.send(o);
}
return this.defs[id].promise();
};
MeteorDdp.prototype.watch = function(collectionName, cb) {
if (!this.watchers[collectionName]) {
this.watchers[collectionName] = [];
}
this.watchers[collectionName].push(cb);
};
MeteorDdp.prototype.getCollection = function(collectionName) {
return this.collections[collectionName] || null;
}
MeteorDdp.prototype.getDocument = function(collectionName, docId) {
return this.collections[collectionName][docId] || null;
}
MeteorDdp.prototype.send = function(msg) {
this.sock.send(JSON.stringify(msg));
};
MeteorDdp.prototype.close = function() {
this.sock.close();
};