Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 8fcf2d3

Browse files
#14 does an empty query fire the onvalue function ?
1 parent 578af65 commit 8fcf2d3

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ Let's say we have the structure as defined at `setValue`, then use this query to
114114
onQueryEvent,
115115
"/companies",
116116
{
117+
// set this to true if you want to check if the value exists or just want the event to fire once
118+
// default false, so it listens continuously
119+
singleEvent: true,
117120
// order by company.country
118121
orderBy: {
119122
type: firebase.QueryOrderByType.CHILD,

firebase-common.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ declare module "nativescript-plugin-firebase" {
1313
*/
1414
PASSWORD
1515
}
16-
16+
1717
/**
1818
* The allowed values for QueryOptions.orderBy.type.
1919
*/
2020
enum QueryOrderByType {
2121
KEY,
2222
VALUE,
2323
CHILD,
24-
PRIORITY
24+
PRIORITY
2525
}
2626

2727
/**
@@ -30,7 +30,7 @@ declare module "nativescript-plugin-firebase" {
3030
enum QueryRangeType {
3131
START_AT,
3232
END_AT,
33-
EQUAL_TO
33+
EQUAL_TO
3434
}
3535

3636
/**

firebase.android.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,21 @@ firebase.query = function (updateCallback, path, options) {
305305
}
306306
}
307307

308-
firebase._addObservers(query, updateCallback);
308+
if (options.singleEvent) {
309+
var listener = new com.firebase.client.ValueEventListener({
310+
onDataChange: function (snapshot) {
311+
updateCallback(firebase.getCallbackData('ValueChanged', snapshot));
312+
},
313+
onCancelled: function (firebaseError) {
314+
updateCallback({
315+
error: firebaseError.getMessage()
316+
});
317+
}
318+
});
319+
query.addListenerForSingleValueEvent(listener);
320+
} else {
321+
firebase._addObservers(query, updateCallback);
322+
}
309323
resolve();
310324
} catch (ex) {
311325
console.log("Error in firebase.query: " + ex);

firebase.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ declare module "nativescript-plugin-firebase" {
4141
type: QueryLimitType;
4242
value: number;
4343
};
44+
45+
/**
46+
* Set this to true if you don't want to listen for any future updates,
47+
* but just want to retrieve the current value.
48+
* You can also use this to check if certain data is in the database.
49+
* Default false.
50+
*/
51+
singleEvent?: boolean;
4452
}
4553

4654
/**
@@ -70,7 +78,7 @@ declare module "nativescript-plugin-firebase" {
7078
email: string;
7179
password: string;
7280
}
73-
81+
7482
/**
7583
* The returned object in the callback handlers
7684
* of the addChildEventListener and addValueEventListener functions.

firebase.ios.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,13 @@ firebase.query = function (updateCallback, path, options) {
268268
}
269269
}
270270

271-
firebase._addObservers(query, updateCallback);
271+
if (options.singleEvent) {
272+
query.observeSingleEventOfTypeWithBlock(FEventType.FEventTypeValue, function (snapshot) {
273+
updateCallback(firebase.getCallbackData('ValueChanged', snapshot));
274+
});
275+
} else {
276+
firebase._addObservers(query, updateCallback);
277+
}
272278
resolve();
273279
} catch (ex) {
274280
console.log("Error in firebase.query: " + ex);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-plugin-firebase",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description" : "Fire. Base. Firebase!",
55
"main" : "firebase.js",
66
"nativescript": {

0 commit comments

Comments
 (0)