Skip to content

Commit fffdb7b

Browse files
authored
Merge pull request #9 from flamebase/develop
Develop
2 parents 0ef778d + a7a095f commit fffdb7b

File tree

3 files changed

+117
-100
lines changed

3 files changed

+117
-100
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[ ![flamebase/flamebase-database-node](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=1.2.0&x2=0)](https://www.npmjs.com/package/flamebase-database-node)
1+
[ ![flamebase/flamebase-database-node](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=1.3.0&x2=0)](https://www.npmjs.com/package/flamebase-database-node)
22
# :fire: flamebase-database-node
33

44
Real time JSON database (server node).
@@ -140,6 +140,13 @@ For sending full reference:
140140
```javascript
141141
FD.syncToDatabase(true);
142142
```
143+
Control synchronization process:
144+
```javascript
145+
FD.syncToDatabase(false, function() {
146+
// data is stored and all push messages has been sent
147+
console.log("data stored and differences sent!")
148+
});
149+
```
143150
- Enable debug logs:
144151
```javascript
145152
FD.debug(true);

index.js

Lines changed: 108 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,32 @@ function FlamebaseDatabase(database, path) {
5252
*/
5353
this.syncFromDatabase = function() {
5454
try {
55-
if (this.debugVal) {
56-
logger.debug("####################### data path: " + path);
57-
}
5855
object.ref = object.db.getData(path);
5956
this.lastStringReference = JSON.stringify(object.ref);
6057
if (this.debugVal) {
61-
logger.debug("####################### ref: " + JSON.stringify(object.ref));
58+
logger.debug("ref: " + path);
59+
logger.debug("len: " + this.lastStringReference.length);
6260
}
63-
object.syncNotifications();
6461
} catch(e) {
65-
console.log("####################### not found, generating {} ");
66-
// object.createEmptyReferenceForPath(e, path);
67-
68-
/*
69-
try {
70-
// console.log("####################### deleting: " + path);
71-
object.db.delete(path);
72-
} catch (e) {
73-
74-
}*/
62+
console.log("####################### error: " + e);
63+
console.log("####################### generating {} ");
7564
object.ref = {};
65+
this.lastStringReference = JSON.stringify(object.ref);
7666
}
7767
};
7868

7969
/**
8070
* sync to database
8171
*/
82-
this.syncToDatabase = function(restart) {
72+
this.syncToDatabase = function(restart, callback) {
8373
if (restart !== undefined && restart) {
8474
this.lastStringReference = JSON.stringify({});
8575
if (this.debugVal) {
8676
logger.debug("cleaning last reference on " + path);
8777
}
8878
}
8979
object.db.push(path, object.ref);
90-
object.syncNotifications();
80+
object.syncNotifications(callback);
9181
};
9282

9383
/**
@@ -116,10 +106,10 @@ function FlamebaseDatabase(database, path) {
116106
/**
117107
* fired if fcm is defined and DB changes or reloads
118108
*/
119-
this.syncNotifications = function() {
120-
if (this.pushConfig !== null && this.fcm !== null) {
109+
this.syncNotifications = function(callback) {
110+
if (this.pushConfig !== null) {
121111
try {
122-
this.sendDetailPushMessage();
112+
this.sendDetailPushMessage(callback);
123113
} catch (e) {
124114
logger.error("error: " + e);
125115
}
@@ -129,7 +119,11 @@ function FlamebaseDatabase(database, path) {
129119
/**
130120
*
131121
*/
132-
this.sendDetailPushMessage = function() {
122+
this.sendDetailPushMessage = function(callback) {
123+
if (this.fcm === null) {
124+
logger.error("# no fcm detected, set an API key")
125+
return;
126+
}
133127

134128
var ios_tokens = [];
135129
var android_tokens = [];
@@ -147,19 +141,14 @@ function FlamebaseDatabase(database, path) {
147141
}
148142
}
149143

150-
var data_android = this.getPartsFor(this.OS.ANDROID, JSON.parse(this.lastStringReference), this.ref);
151-
var data_ios = this.getPartsFor(this.OS.IOS, JSON.parse(this.lastStringReference), this.ref);
152-
153-
if (object.debugVal) {
154-
logger.debug("android_tokens_size: " + android_tokens.length);
155-
logger.debug("ios_tokens_size: " + ios_tokens.length);
156-
logger.debug("data_android_size: " + data_android.parts.length);
157-
logger.debug("data_ios_size: " + data_ios.parts.length);
158-
}
159-
160144
this.lastStringReference = JSON.stringify(this.ref);
161145

162146
if (android_tokens.length > 0) {
147+
var data_android = this.getPartsFor(this.OS.ANDROID, JSON.parse(this.lastStringReference), this.ref);
148+
if (object.debugVal) {
149+
logger.debug("android_tokens_size: " + android_tokens.length);
150+
logger.debug("data_android_size: " + data_android.parts.length);
151+
}
163152
if (data_android.parts.length === 1) {
164153
var data = {};
165154
data.id = id;
@@ -172,7 +161,11 @@ function FlamebaseDatabase(database, path) {
172161
send.data = data;
173162
send.tokens = android_tokens;
174163
send.notification = notification;
175-
this.sendPushMessage(send);
164+
if (ios_tokens.length === 0) {
165+
this.sendPushMessage(send, callback);
166+
} else {
167+
this.sendPushMessage(send);
168+
}
176169
} else if (data_android.parts.length > 1) {
177170
for (var i = 0; i < data_android.parts.length; i++) {
178171
var dat = {};
@@ -186,14 +179,35 @@ function FlamebaseDatabase(database, path) {
186179
sen.data = dat;
187180
sen.tokens = android_tokens;
188181
sen.notification = notification;
189-
this.sendPushMessage(sen);
182+
if (ios_tokens.length === 0 && i === data_android.parts.length - 1) {
183+
this.sendPushMessage(sen, callback);
184+
} else {
185+
this.sendPushMessage(sen);
186+
}
187+
}
188+
} else {
189+
var data = {};
190+
data.id = id;
191+
data.tag = this.pushConfig.tag();
192+
data.action = ACTION_NO_UPDATE;
193+
var send = {};
194+
send.data = data;
195+
send.tokens = android_tokens;
196+
send.notification = notification;
197+
if (ios_tokens.length === 0) {
198+
this.sendPushMessage(send, callback);
199+
} else {
200+
this.sendPushMessage(send);
190201
}
191-
} else if (this.debugVal) {
192-
logger.debug("no differences located");
193202
}
194203
}
195204

196205
if (ios_tokens.length > 0) {
206+
var data_ios = this.getPartsFor(this.OS.IOS, JSON.parse(this.lastStringReference), this.ref);
207+
if (object.debugVal) {
208+
logger.debug("ios_tokens_size: " + ios_tokens.length);
209+
logger.debug("data_ios_size: " + data_ios.parts.length);
210+
}
197211
if (data_ios.parts.length === 1) {
198212
var da = {};
199213
da.id = id;
@@ -206,7 +220,7 @@ function FlamebaseDatabase(database, path) {
206220
se.data = da;
207221
se.tokens = ios_tokens;
208222
se.notification = notification;
209-
this.sendPushMessage(se);
223+
this.sendPushMessage(se, callback);
210224
} else if (data_ios.parts.length > 1) {
211225
for (var i = 0; i < data_ios.parts.length; i++) {
212226
var d = {};
@@ -220,16 +234,31 @@ function FlamebaseDatabase(database, path) {
220234
s.data = d;
221235
s.tokens = ios_tokens;
222236
s.notification = notification;
223-
this.sendPushMessage(s);
237+
if (i === data_ios.parts.length - 1) {
238+
this.sendPushMessage(s, callback);
239+
} else {
240+
this.sendPushMessage(s);
241+
}
224242
}
225-
} else if (this.debugVal) {
226-
logger.debug("no differences located");
243+
} else {
244+
var data = {};
245+
data.id = id;
246+
data.tag = this.pushConfig.tag();
247+
data.action = ACTION_NO_UPDATE;
248+
var s = {};
249+
s.data = data;
250+
s.tokens = ios_tokens;
251+
s.notification = notification;
252+
this.sendPushMessage(s, callback);
227253
}
228254
}
229255
};
230256

231-
this.sendDifferencesForClient = function(before, device) {
232-
257+
this.sendDifferencesForClient = function(before, device, callback) {
258+
if (this.fcm === null) {
259+
logger.error("# no fcm detected, set an API key")
260+
return;
261+
}
233262
var ios_tokens = [];
234263
var android_tokens = [];
235264

@@ -242,19 +271,12 @@ function FlamebaseDatabase(database, path) {
242271
android_tokens.push(device.token);
243272
}
244273

245-
var data_android = this.getPartsFor(this.OS.ANDROID, JSON.parse(before), this.ref);
246-
var data_ios = this.getPartsFor(this.OS.IOS, JSON.parse(before), this.ref);
247-
248-
if (object.debugVal) {
249-
logger.debug("android_tokens_size: " + android_tokens.length);
250-
logger.debug("ios_tokens_size: " + ios_tokens.length);
251-
logger.debug("data_android_size: " + data_android.parts.length);
252-
logger.debug("data_ios_size: " + data_ios.parts.length);
253-
}
254-
255-
this.lastStringReference = JSON.stringify(this.ref);
256-
257274
if (android_tokens.length > 0) {
275+
var data_android = this.getPartsFor(this.OS.ANDROID, JSON.parse(before), this.ref);
276+
if (object.debugVal) {
277+
logger.debug("android_tokens_size: " + android_tokens.length);
278+
logger.debug("data_android_size: " + data_android.parts.length);
279+
}
258280
if (data_android.parts.length === 1) {
259281
var data = {};
260282
data.id = id;
@@ -267,7 +289,11 @@ function FlamebaseDatabase(database, path) {
267289
send.data = data;
268290
send.tokens = android_tokens;
269291
send.notification = notification;
270-
this.sendPushMessage(send);
292+
if (ios_tokens.length === 0) {
293+
this.sendPushMessage(send, callback);
294+
} else {
295+
this.sendPushMessage(send);
296+
}
271297
} else if (data_android.parts.length > 1) {
272298
for (var i = 0; i < data_android.parts.length; i++) {
273299
var dat = {};
@@ -281,7 +307,11 @@ function FlamebaseDatabase(database, path) {
281307
sen.data = dat;
282308
sen.tokens = android_tokens;
283309
sen.notification = notification;
284-
this.sendPushMessage(sen);
310+
if (ios_tokens.length === 0 && i === data_android.parts.length - 1) {
311+
this.sendPushMessage(sen, callback);
312+
} else {
313+
this.sendPushMessage(sen);
314+
}
285315
}
286316
} else {
287317
var data = {};
@@ -292,11 +322,20 @@ function FlamebaseDatabase(database, path) {
292322
send.data = data;
293323
send.tokens = android_tokens;
294324
send.notification = notification;
295-
this.sendPushMessage(send);
325+
if (ios_tokens.length === 0) {
326+
this.sendPushMessage(send, callback);
327+
} else {
328+
this.sendPushMessage(send);
329+
}
296330
}
297331
}
298332

299333
if (ios_tokens.length > 0) {
334+
var data_ios = this.getPartsFor(this.OS.IOS, JSON.parse(before), this.ref);
335+
if (object.debugVal) {
336+
logger.debug("ios_tokens_size: " + ios_tokens.length);
337+
logger.debug("data_ios_size: " + data_ios.parts.length);
338+
}
300339
if (data_ios.parts.length === 1) {
301340
var da = {};
302341
da.id = id;
@@ -309,7 +348,7 @@ function FlamebaseDatabase(database, path) {
309348
se.data = da;
310349
se.tokens = ios_tokens;
311350
se.notification = notification;
312-
this.sendPushMessage(se);
351+
this.sendPushMessage(se, callback);
313352
} else if (data_ios.parts.length > 1) {
314353
for (var i = 0; i < data_ios.parts.length; i++) {
315354
var d = {};
@@ -323,7 +362,11 @@ function FlamebaseDatabase(database, path) {
323362
s.data = d;
324363
s.tokens = ios_tokens;
325364
s.notification = notification;
326-
this.sendPushMessage(s);
365+
if (i === data_ios.parts.length - 1) {
366+
this.sendPushMessage(s, callback);
367+
} else {
368+
this.sendPushMessage(s);
369+
}
327370
}
328371
} else {
329372
var data = {};
@@ -334,12 +377,14 @@ function FlamebaseDatabase(database, path) {
334377
send.data = data;
335378
send.tokens = ios_tokens;
336379
send.notification = notification;
337-
this.sendPushMessage(send);
380+
this.sendPushMessage(send, callback);
338381
}
339382
}
383+
384+
this.lastStringReference = JSON.stringify(this.ref);
340385
};
341386

342-
this.sendPushMessage = function(send) {
387+
this.sendPushMessage = function(send, callback) {
343388
this.queue.pushJob(function() {
344389
return new Promise(function (resolve, reject) {
345390
var message = {
@@ -358,6 +403,9 @@ function FlamebaseDatabase(database, path) {
358403
if (object.debugVal) {
359404
logger.debug("Successfully sent with response: " + JSON.stringifyAligned(JSON.parse(response)));
360405
}
406+
if (callback != undefined) {
407+
callback();
408+
}
361409
resolve();
362410
})
363411
.catch(function (err) {
@@ -413,44 +461,6 @@ function FlamebaseDatabase(database, path) {
413461
return result;
414462
};
415463

416-
/*this.createEmptyReferenceForPath = function(e, path) {
417-
var aux = null;
418-
if (e.toString().indexOf("Can't find dataPath") > -1) {
419-
420-
var mainRef = object.db.getData("/");
421-
var pathParts = path.split("/");
422-
423-
for (var p = 0; p < pathParts.length; p++) {
424-
var pa = pathParts[p];
425-
426-
if (pa.length === 0) {
427-
continue;
428-
}
429-
430-
if (aux === null) {
431-
if (mainRef[pa] !== undefined) {
432-
aux = mainRef[pa];
433-
} else {
434-
mainRef[pa] = {};
435-
aux = mainRef[pa]
436-
}
437-
} else {
438-
if (aux[pa] !== undefined) {
439-
aux = aux[pa];
440-
} else {
441-
aux[pa] = {};
442-
aux = aux[pa]
443-
}
444-
}
445-
}
446-
447-
logger.debug("generated: " + JSON.stringifyAligned(mainRef));
448-
449-
object.ref = mainRef;
450-
object.db.push("/", mainRef);
451-
}
452-
};*/
453-
454464
this.exist = function() {
455465
return !(this.ref === null || this.ref === undefined)
456466
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@
4848
"scripts": {
4949
"test": "echo \"Error: no test specified\" && exit 1"
5050
},
51-
"version": "1.2.0"
51+
"version": "1.3.0"
5252
}

0 commit comments

Comments
 (0)