-
Notifications
You must be signed in to change notification settings - Fork 5
/
Data.js
49 lines (46 loc) · 1.11 KB
/
Data.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
var tracker = require('./lib/tracker/trackr');
var minimongo = require('./lib/minimongo/minimongo');
const db = new minimongo();
db.debug = false;
module.exports = {
_endpoint: null,
_options: null,
ddp: null,
subscriptions: {},
db: db,
calls: [],
getUrl() {
return this._endpoint.substring(0, this._endpoint.indexOf('/websocket'));
},
_cbs: [],
onChange(cb) {
this.db.on('change', cb);
this.ddp.on('connected', cb);
this.ddp.on('disconnected', cb);
this.on('loggingIn', cb);
this.on('change', cb);
},
offChange(cb) {
this.db.off('change', cb);
this.ddp.off('connected', cb);
this.ddp.off('disconnected', cb);
this.off('loggingIn', cb);
this.off('change', cb);
},
on(eventName, cb) {
this._cbs.push({
eventName: eventName,
callback: cb
});
},
off(eventName, cb) {
this._cbs.splice(this._cbs.findIndex(_cb=>_cb.callback == cb && _cb.eventName == eventName), 1);
},
notify(eventName) {
this._cbs.map(cb=>{
if(cb.eventName == eventName && typeof cb.callback == 'function') {
cb.callback();
}
});
}
}