@@ -52,42 +52,32 @@ function FlamebaseDatabase(database, path) {
52
52
*/
53
53
this . syncFromDatabase = function ( ) {
54
54
try {
55
- if ( this . debugVal ) {
56
- logger . debug ( "####################### data path: " + path ) ;
57
- }
58
55
object . ref = object . db . getData ( path ) ;
59
56
this . lastStringReference = JSON . stringify ( object . ref ) ;
60
57
if ( this . debugVal ) {
61
- logger . debug ( "####################### ref: " + JSON . stringify ( object . ref ) ) ;
58
+ logger . debug ( "ref: " + path ) ;
59
+ logger . debug ( "len: " + this . lastStringReference . length ) ;
62
60
}
63
- object . syncNotifications ( ) ;
64
61
} 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 {} " ) ;
75
64
object . ref = { } ;
65
+ this . lastStringReference = JSON . stringify ( object . ref ) ;
76
66
}
77
67
} ;
78
68
79
69
/**
80
70
* sync to database
81
71
*/
82
- this . syncToDatabase = function ( restart ) {
72
+ this . syncToDatabase = function ( restart , callback ) {
83
73
if ( restart !== undefined && restart ) {
84
74
this . lastStringReference = JSON . stringify ( { } ) ;
85
75
if ( this . debugVal ) {
86
76
logger . debug ( "cleaning last reference on " + path ) ;
87
77
}
88
78
}
89
79
object . db . push ( path , object . ref ) ;
90
- object . syncNotifications ( ) ;
80
+ object . syncNotifications ( callback ) ;
91
81
} ;
92
82
93
83
/**
@@ -116,10 +106,10 @@ function FlamebaseDatabase(database, path) {
116
106
/**
117
107
* fired if fcm is defined and DB changes or reloads
118
108
*/
119
- this . syncNotifications = function ( ) {
120
- if ( this . pushConfig !== null && this . fcm !== null ) {
109
+ this . syncNotifications = function ( callback ) {
110
+ if ( this . pushConfig !== null ) {
121
111
try {
122
- this . sendDetailPushMessage ( ) ;
112
+ this . sendDetailPushMessage ( callback ) ;
123
113
} catch ( e ) {
124
114
logger . error ( "error: " + e ) ;
125
115
}
@@ -129,7 +119,11 @@ function FlamebaseDatabase(database, path) {
129
119
/**
130
120
*
131
121
*/
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
+ }
133
127
134
128
var ios_tokens = [ ] ;
135
129
var android_tokens = [ ] ;
@@ -147,19 +141,14 @@ function FlamebaseDatabase(database, path) {
147
141
}
148
142
}
149
143
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
-
160
144
this . lastStringReference = JSON . stringify ( this . ref ) ;
161
145
162
146
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
+ }
163
152
if ( data_android . parts . length === 1 ) {
164
153
var data = { } ;
165
154
data . id = id ;
@@ -172,7 +161,11 @@ function FlamebaseDatabase(database, path) {
172
161
send . data = data ;
173
162
send . tokens = android_tokens ;
174
163
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
+ }
176
169
} else if ( data_android . parts . length > 1 ) {
177
170
for ( var i = 0 ; i < data_android . parts . length ; i ++ ) {
178
171
var dat = { } ;
@@ -186,14 +179,35 @@ function FlamebaseDatabase(database, path) {
186
179
sen . data = dat ;
187
180
sen . tokens = android_tokens ;
188
181
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 ) ;
190
201
}
191
- } else if ( this . debugVal ) {
192
- logger . debug ( "no differences located" ) ;
193
202
}
194
203
}
195
204
196
205
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
+ }
197
211
if ( data_ios . parts . length === 1 ) {
198
212
var da = { } ;
199
213
da . id = id ;
@@ -206,7 +220,7 @@ function FlamebaseDatabase(database, path) {
206
220
se . data = da ;
207
221
se . tokens = ios_tokens ;
208
222
se . notification = notification ;
209
- this . sendPushMessage ( se ) ;
223
+ this . sendPushMessage ( se , callback ) ;
210
224
} else if ( data_ios . parts . length > 1 ) {
211
225
for ( var i = 0 ; i < data_ios . parts . length ; i ++ ) {
212
226
var d = { } ;
@@ -220,16 +234,31 @@ function FlamebaseDatabase(database, path) {
220
234
s . data = d ;
221
235
s . tokens = ios_tokens ;
222
236
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
+ }
224
242
}
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 ) ;
227
253
}
228
254
}
229
255
} ;
230
256
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
+ }
233
262
var ios_tokens = [ ] ;
234
263
var android_tokens = [ ] ;
235
264
@@ -242,19 +271,12 @@ function FlamebaseDatabase(database, path) {
242
271
android_tokens . push ( device . token ) ;
243
272
}
244
273
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
-
257
274
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
+ }
258
280
if ( data_android . parts . length === 1 ) {
259
281
var data = { } ;
260
282
data . id = id ;
@@ -267,7 +289,11 @@ function FlamebaseDatabase(database, path) {
267
289
send . data = data ;
268
290
send . tokens = android_tokens ;
269
291
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
+ }
271
297
} else if ( data_android . parts . length > 1 ) {
272
298
for ( var i = 0 ; i < data_android . parts . length ; i ++ ) {
273
299
var dat = { } ;
@@ -281,7 +307,11 @@ function FlamebaseDatabase(database, path) {
281
307
sen . data = dat ;
282
308
sen . tokens = android_tokens ;
283
309
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
+ }
285
315
}
286
316
} else {
287
317
var data = { } ;
@@ -292,11 +322,20 @@ function FlamebaseDatabase(database, path) {
292
322
send . data = data ;
293
323
send . tokens = android_tokens ;
294
324
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
+ }
296
330
}
297
331
}
298
332
299
333
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
+ }
300
339
if ( data_ios . parts . length === 1 ) {
301
340
var da = { } ;
302
341
da . id = id ;
@@ -309,7 +348,7 @@ function FlamebaseDatabase(database, path) {
309
348
se . data = da ;
310
349
se . tokens = ios_tokens ;
311
350
se . notification = notification ;
312
- this . sendPushMessage ( se ) ;
351
+ this . sendPushMessage ( se , callback ) ;
313
352
} else if ( data_ios . parts . length > 1 ) {
314
353
for ( var i = 0 ; i < data_ios . parts . length ; i ++ ) {
315
354
var d = { } ;
@@ -323,7 +362,11 @@ function FlamebaseDatabase(database, path) {
323
362
s . data = d ;
324
363
s . tokens = ios_tokens ;
325
364
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
+ }
327
370
}
328
371
} else {
329
372
var data = { } ;
@@ -334,12 +377,14 @@ function FlamebaseDatabase(database, path) {
334
377
send . data = data ;
335
378
send . tokens = ios_tokens ;
336
379
send . notification = notification ;
337
- this . sendPushMessage ( send ) ;
380
+ this . sendPushMessage ( send , callback ) ;
338
381
}
339
382
}
383
+
384
+ this . lastStringReference = JSON . stringify ( this . ref ) ;
340
385
} ;
341
386
342
- this . sendPushMessage = function ( send ) {
387
+ this . sendPushMessage = function ( send , callback ) {
343
388
this . queue . pushJob ( function ( ) {
344
389
return new Promise ( function ( resolve , reject ) {
345
390
var message = {
@@ -358,6 +403,9 @@ function FlamebaseDatabase(database, path) {
358
403
if ( object . debugVal ) {
359
404
logger . debug ( "Successfully sent with response: " + JSON . stringifyAligned ( JSON . parse ( response ) ) ) ;
360
405
}
406
+ if ( callback != undefined ) {
407
+ callback ( ) ;
408
+ }
361
409
resolve ( ) ;
362
410
} )
363
411
. catch ( function ( err ) {
@@ -413,44 +461,6 @@ function FlamebaseDatabase(database, path) {
413
461
return result ;
414
462
} ;
415
463
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
-
454
464
this . exist = function ( ) {
455
465
return ! ( this . ref === null || this . ref === undefined )
456
466
} ;
0 commit comments