-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireWrapper.js
More file actions
142 lines (123 loc) · 3.89 KB
/
FireWrapper.js
File metadata and controls
142 lines (123 loc) · 3.89 KB
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
import firebase from 'firebase';
import { config } from '../../FirebaseCredentials'
/* thanks to:
https://github.com/Thoughtscript/x_team_vue
*/
/*
asynOpts= {
addIfNotFound: true,
key: 'key',
onChildAdded: null,
onChildChanged: null,
onChilRemoved: null
}
*/
var db;
export function initFB() {
if (!firebase.apps.length) {
firebase.initializeApp(config)
}
db = firebase.database()
}
export var syncFB = {
getDefaultOpts: () => {
return {
addIfNotFound: true,
key: 'key',
validated: true
};
},
getOpts: (opts) => {
if(opts && opts.validated) return opts;
if(!opts) return syncFB.getDefaultOpts();
//verify each parameter exist or add a default value
var defs = syncFB.getDefaultOpts();
for(var key in defs){
if(opts[key] === undefined){
opts[key] = defs[key];
}
}
opts.validated = true;
console.log('opts validated', opts);
return opts;
},
updateObject(base, upd, key){
for(var k in upd){
if(k != key){ // do not update the key
base[k]=upd[k]
}
}
},
subscribeAll: (ref, list, synOpts) => {
var opts = syncFB.getOpts(synOpts);
syncFB.subscribeNew(ref, list, opts);
syncFB.subscribeChanged(ref, list, opts);
syncFB.subscribeRemoved(ref, list, opts);
},
subscribeNew: (ref, list, synOpts) => {
var opts = syncFB.getOpts(synOpts);
db.ref(`/${ref}`)
.on('child_added', function (snap, xy) {
var tmp = snap.val();
tmp[opts.key] = snap.ref.key;
list.push(tmp);
if(synOpts.onChildAdded){
synOpts.onChildAdded(tmp);
}
})
},
subscribeChanged: (ref, list, synOpts) => {
var opts = syncFB.getOpts(synOpts);
db.ref(`/${ref}`)
.on('child_changed', function (snap, key) {
var tmp = snap.val();
tmp[opts.key] = snap.ref.key;
for(let i=0; i< list.length; i++){
if(list[i][opts.key] == tmp[opts.key]){
syncFB.updateObject(list[i], tmp, opts.key);
return;
}
}
// not found, should be added?
if(opts.addIfNotFound){
list.push(tmp);
}
})
},
subscribeRemoved: (ref, list, synOpts) => {
var opts = syncFB.getOpts(synOpts);
db.ref(`/${ref}`)
.on('child_removed', function (snap, key) {
var tmp = snap.val();
tmp[opts.key] = snap.ref.key;
for(let i=0; i< list.length; i++){
if(list[i][opts.key] == tmp[opts.key]){
list.splice(i, 1);
return;
}
}
})
},
}
export var asyncFB = {
/** GET All */
fetchAll: (ref) => new Promise(resolve => {
db.ref(`/${ref}`)
.once('value')
.then(v => resolve(v.val()))
}),
/** GET One */
fetchOne: (ref, key) => new Promise(resolve => {
db.ref(`/${ref}/${key}`)
.once('value')
.then(v => resolve(v.val()))
}),
/** POST One */
addOne: (ref, key, contactData) => new Promise(resolve => {
resolve(db.ref(`/${ref}`).child(key).set(contactData))
}),
/** DELETE One */
removeOne: (ref, key) => new Promise(resolve => {
resolve(db.ref(`/${ref}`).child(key).remove())
})
}