-
Notifications
You must be signed in to change notification settings - Fork 1
/
community_fb_manager.js
118 lines (105 loc) · 3.93 KB
/
community_fb_manager.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
/**
* @author Filip Prochazka (@jacktech24)
*/
"use strict";
const EventEmitter = require('events');
const Firebase = require('firebase');
const GeoFire = require('geofire');
class CommunityFirebaseManager extends EventEmitter {
constructor(urlKey, secret, dataModel) {
super();
this._cachedData = {};
this._syncedCount = 0;
this._requiredSyncCount = 0;
this._dataModel = dataModel;
this._rootFirebase = new Firebase('https://' + urlKey + '.firebaseio.com/');
this._geoFire = new GeoFire(this._rootFirebase.child(dataModel.geofirePath));
var self = this;
this._rootFirebase.authWithCustomToken(secret, function (error, authData) {
if (!error) {
self.emit('auth_success');
self.on('path_synced', () => {
if (self._syncedCount === self._requiredSyncCount) {
self.emit('initialized');
}
});
self._syncInitialData();
} else {
self.emit('auth_error');
}
});
}
pushEvent(event, processor) {
if (processor.eventsFilter(event, this._cachedData)) {
this.emit('processing_event', event);
var output = {
save: {},
delete: [],
update: {},
save_geofire: {},
delete_geofire: []
};
var self = this;
var firebaseData = processor.processEvent(event, this._cachedData, output);
if (firebaseData['save']) {
let pushPaths = Object.keys(firebaseData['save']);
pushPaths.forEach((path) => {
self._rootFirebase.child(path).set(firebaseData['save'][path]);
});
}
if (firebaseData['save_geofire']) {
let geofireKeys = Object.keys(firebaseData['save_geofire']);
geofireKeys.forEach((key) => {
self._geoFire.set(key, firebaseData['save_geofire'][key]);
});
}
if (firebaseData['delete']) {
firebaseData['delete'].forEach((path) => {
self._rootFirebase.child(path).set(null);
});
}
if (firebaseData['delete_geofire']) {
firebaseData['delete_geofire'].forEach((key) => {
self._geoFire.remove(key);
});
}
if (firebaseData['update']) {
let updatePaths = Object.keys(firebaseData['update']);
updatePaths.forEach((path) => {
self._rootFirebase.child(path).update(firebaseData['update'][path]);
});
}
}
}
get syncedData() {
return this._cachedData;
}
_syncInitialData() {
var self = this;
this._dataModel.syncPaths.forEach((path) => {
self._requiredSyncCount++;
self._syncFirebasePathAndObserve(path);
});
}
_syncFirebasePathAndObserve(path) {
this._rootFirebase.child(path).once('value', function (snap) {
this._cachedData[path] = snap.val();
this._observeFirebasePath(path);
this._syncedCount++;
this.emit('path_synced', path);
}.bind(this));
}
_observeFirebasePath(path) {
var localBase = this._rootFirebase.child(path);
localBase.on('child_added', function (snap, prevChildKey) {
this._cachedData[path][snap.key()] = snap.val();
}.bind(this));
localBase.on('child_changed', function (snap, prevChildKey) {
this._cachedData[path][snap.key()] = snap.val();
}.bind(this));
localBase.on('child_removed', function (snap, prevChildKey) {
delete this._cachedData[path][snap.key()];
}.bind(this));
}
}
module.exports = CommunityFirebaseManager;