-
Notifications
You must be signed in to change notification settings - Fork 211
/
Meteor.js
315 lines (279 loc) · 9.07 KB
/
Meteor.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { NetInfo, Platform, View } from 'react-native';
import reactMixin from 'react-mixin';
import Trackr from 'trackr';
import EJSON from 'ejson';
import DDP from '../lib/ddp.js';
import Random from '../lib/Random';
import Data from './Data';
import { Collection } from './Collection';
import call from './Call';
import Mixin from './components/Mixin';
import MeteorListView from './components/ListView';
import MeteorComplexListView from './components/ComplexListView';
import createContainer from './components/createContainer';
import withTracker from './components/ReactMeteorData';
import composeWithTracker from './components/composeWithTracker';
import FSCollection from './CollectionFS/FSCollection';
import FSCollectionImagesPreloader from './CollectionFS/FSCollectionImagesPreloader';
import ReactiveDict from './ReactiveDict';
import User from './user/User';
import Accounts from './user/Accounts';
module.exports = {
composeWithTracker,
Accounts,
Tracker: Trackr,
EJSON,
MeteorListView,
MeteorComplexListView,
ReactiveDict,
Collection,
FSCollectionImagesPreloader:
Platform.OS == 'android' ? View : FSCollectionImagesPreloader,
collection(name, options) {
return new Collection(name, options);
},
FSCollection,
createContainer,
withTracker,
getData() {
return Data;
},
connectMeteor(reactClass) {
return reactMixin.onClass(reactClass, Mixin);
},
...User,
status() {
return {
connected: Data.ddp ? Data.ddp.status == 'connected' : false,
status: Data.ddp ? Data.ddp.status : 'disconnected',
//retryCount: 0
//retryTime:
//reason:
};
},
call: call,
disconnect() {
if (Data.ddp) {
Data.ddp.disconnect();
}
},
_subscriptionsRestart() {
for (var i in Data.subscriptions) {
const sub = Data.subscriptions[i];
Data.ddp.unsub(sub.subIdRemember);
sub.subIdRemember = Data.ddp.sub(sub.name, sub.params);
}
},
waitDdpConnected: Data.waitDdpConnected.bind(Data),
reconnect() {
Data.ddp && Data.ddp.connect();
},
connect(endpoint, options) {
if (!endpoint) endpoint = Data._endpoint;
if (!options) options = Data._options;
Data._endpoint = endpoint;
Data._options = options;
this.ddp = Data.ddp = new DDP({
endpoint: endpoint,
SocketConstructor: WebSocket,
...options,
});
NetInfo.isConnected.addEventListener('connectionChange', isConnected => {
if (isConnected && Data.ddp.autoReconnect) {
Data.ddp.connect();
}
});
Data.ddp.on('connected', () => {
// Clear the collections of any stale data in case this is a reconnect
if (Data.db && Data.db.collections) {
for (var collection in Data.db.collections) {
Data.db[collection].remove({});
}
}
Data.notify('change');
console.info('Connected to DDP server.');
this._loadInitialUser().then(() => {
this._subscriptionsRestart();
});
});
let lastDisconnect = null;
Data.ddp.on('disconnected', () => {
Data.notify('change');
console.info('Disconnected from DDP server.');
if (!Data.ddp.autoReconnect) return;
if (!lastDisconnect || new Date() - lastDisconnect > 3000) {
Data.ddp.connect();
}
lastDisconnect = new Date();
});
Data.ddp.on('added', message => {
if (!Data.db[message.collection]) {
Data.db.addCollection(message.collection);
}
Data.db[message.collection].upsert({
_id: message.id,
...message.fields,
});
});
Data.ddp.on('ready', message => {
const idsMap = new Map();
for (var i in Data.subscriptions) {
const sub = Data.subscriptions[i];
idsMap.set(sub.subIdRemember, sub.id);
}
for (var i in message.subs) {
const subId = idsMap.get(message.subs[i]);
if (subId) {
const sub = Data.subscriptions[subId];
sub.ready = true;
sub.readyDeps.changed();
sub.readyCallback && sub.readyCallback();
}
}
});
Data.ddp.on('changed', message => {
const unset = {};
if (message.cleared) {
message.cleared.forEach(field => {
unset[field] = null;
});
}
Data.db[message.collection] &&
Data.db[message.collection].upsert({
_id: message.id,
...message.fields,
...unset,
});
});
Data.ddp.on('removed', message => {
Data.db[message.collection] &&
Data.db[message.collection].del(message.id);
});
Data.ddp.on('result', message => {
const call = Data.calls.find(call => call.id == message.id);
if (typeof call.callback == 'function')
call.callback(message.error, message.result);
Data.calls.splice(Data.calls.findIndex(call => call.id == message.id), 1);
});
Data.ddp.on('nosub', message => {
for (var i in Data.subscriptions) {
const sub = Data.subscriptions[i];
if (sub.subIdRemember == message.id) {
console.warn('No subscription existing for', sub.name);
}
}
});
},
subscribe(name) {
var params = Array.prototype.slice.call(arguments, 1);
var callbacks = {};
if (params.length) {
var lastParam = params[params.length - 1];
if (typeof lastParam == 'function') {
callbacks.onReady = params.pop();
} else if (
lastParam &&
(typeof lastParam.onReady == 'function' ||
typeof lastParam.onError == 'function' ||
typeof lastParam.onStop == 'function')
) {
callbacks = params.pop();
}
}
// Is there an existing sub with the same name and param, run in an
// invalidated Computation? This will happen if we are rerunning an
// existing computation.
//
// For example, consider a rerun of:
//
// Tracker.autorun(function () {
// Meteor.subscribe("foo", Session.get("foo"));
// Meteor.subscribe("bar", Session.get("bar"));
// });
//
// If "foo" has changed but "bar" has not, we will match the "bar"
// subcribe to an existing inactive subscription in order to not
// unsub and resub the subscription unnecessarily.
//
// We only look for one such sub; if there are N apparently-identical subs
// being invalidated, we will require N matching subscribe calls to keep
// them all active.
let existing = false;
for (var i in Data.subscriptions) {
const sub = Data.subscriptions[i];
if (sub.inactive && sub.name === name && EJSON.equals(sub.params, params))
existing = sub;
}
let id;
if (existing) {
id = existing.id;
existing.inactive = false;
if (callbacks.onReady) {
// If the sub is not already ready, replace any ready callback with the
// one provided now. (It's not really clear what users would expect for
// an onReady callback inside an autorun; the semantics we provide is
// that at the time the sub first becomes ready, we call the last
// onReady callback provided, if any.)
if (!existing.ready) existing.readyCallback = callbacks.onReady;
}
if (callbacks.onStop) {
existing.stopCallback = callbacks.onStop;
}
} else {
// New sub! Generate an id, save it locally, and send message.
id = Random.id();
const subIdRemember = Data.ddp.sub(name, params);
Data.subscriptions[id] = {
id: id,
subIdRemember: subIdRemember,
name: name,
params: EJSON.clone(params),
inactive: false,
ready: false,
readyDeps: new Trackr.Dependency(),
readyCallback: callbacks.onReady,
stopCallback: callbacks.onStop,
stop: function() {
Data.ddp.unsub(this.subIdRemember);
delete Data.subscriptions[this.id];
this.ready && this.readyDeps.changed();
if (callbacks.onStop) {
callbacks.onStop();
}
},
};
}
// return a handle to the application.
var handle = {
stop: function() {
if (Data.subscriptions[id]) Data.subscriptions[id].stop();
},
ready: function() {
if (!Data.subscriptions[id]) return false;
var record = Data.subscriptions[id];
record.readyDeps.depend();
return record.ready;
},
subscriptionId: id,
};
if (Trackr.active) {
// We're in a reactive computation, so we'd like to unsubscribe when the
// computation is invalidated... but not if the rerun just re-subscribes
// to the same subscription! When a rerun happens, we use onInvalidate
// as a change to mark the subscription "inactive" so that it can
// be reused from the rerun. If it isn't reused, it's killed from
// an afterFlush.
Trackr.onInvalidate(function(c) {
if (Data.subscriptions[id]) {
Data.subscriptions[id].inactive = true;
}
Trackr.afterFlush(function() {
if (Data.subscriptions[id] && Data.subscriptions[id].inactive) {
handle.stop();
}
});
});
}
return handle;
},
};