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

Commit 1a81627

Browse files
committed
[query][android] - Make webapi queries return a datasnapshot that follows the webapi
1 parent a33ee36 commit 1a81627

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

src/firebase.android.ts

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,10 +1503,10 @@ firebase.getValue = path => {
15031503
}
15041504

15051505
const listener = new com.google.firebase.database.ValueEventListener({
1506-
onDataChange: snapshot => {
1506+
onDataChange: (snapshot: com.google.firebase.database.DataSnapshot) => {
15071507
resolve(firebase.getCallbackData('ValueChanged', snapshot));
15081508
},
1509-
onCancelled: databaseError => {
1509+
onCancelled: (databaseError: com.google.firebase.database.DatabaseError) => {
15101510
reject(databaseError.getMessage());
15111511
}
15121512
});
@@ -1622,9 +1622,6 @@ firebase.update = (path, val) => {
16221622
}
16231623
});
16241624
};
1625-
firebase.query = (updateCallback, path, options) => {
1626-
return new Promise((resolve, reject) => {
1627-
16281625

16291626
firebase.webQuery = (path: string): QueryBase => {
16301627
if (!firebase.initialized) {
@@ -1639,34 +1636,31 @@ class Query implements QueryBase {
16391636
private query: com.google.firebase.database.Query; // Keep track of internal query state allowing us to chain filter/range/limit
16401637
private static eventListenerMap: Map<string, Array<any>> = new Map(); // A map to keep track all all the listeners attached for the specified eventType
16411638

1642-
constructor(private dbRef: com.google.firebase.database.DatabaseReference, private path: string) { }
1639+
constructor(private dbRef: com.google.firebase.database.DatabaseReference, private path: string) {
1640+
this.query = this.dbRef;
1641+
}
16431642

16441643
on(eventType: string, callback: (a: any, b?: string) => any): Promise<any> {
16451644
const onValueEvent = result => {
1646-
if (result.error) {
1647-
callback(result); // CAREFUL before we were calling result.error!
1648-
} else {
1649-
callback({
1650-
key: result.key,
1651-
val: () => result.value,
1652-
exists: () => !!result.value
1653-
});
1654-
}
1645+
callback(result);
16551646
};
1647+
16561648
return new Promise((resolve, reject) => {
16571649
try {
16581650
if (firebase.instance === null) {
16591651
reject("Run init() first!");
16601652
return;
16611653
}
16621654
const listener = this.createEventListener(eventType, onValueEvent);
1663-
if (!this.query) this.query = this.dbRef; // Need this when calling on() without doing a sort as this.query is undefined
16641655

16651656
if (eventType === "value") {
16661657
this.query.addValueEventListener(listener as com.google.firebase.database.ValueEventListener);
16671658
} else if (eventType === "child_added" || eventType === "child_changed" || eventType === "child_removed" || eventType === "child_moved") {
16681659
this.query.addChildEventListener(listener as com.google.firebase.database.ChildEventListener);
16691660
} else {
1661+
callback({
1662+
error: "Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'"
1663+
});
16701664
reject("Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'");
16711665
return;
16721666
}
@@ -1677,21 +1671,36 @@ class Query implements QueryBase {
16771671
Query.eventListenerMap.get(eventType).push(listener); // We need to keep track of the listeners to fully remove them when calling off
16781672
resolve();
16791673
} catch (ex) {
1680-
console.log("Error in firebase.addValueEventListener: " + ex);
1674+
console.log("Error in firebase.on: " + ex);
16811675
reject(ex);
16821676
}
16831677
});
16841678
}
16851679

1686-
once(eventType: string): Promise<any> {
1680+
once(eventType: string): Promise<DataSnapshot> {
16871681
return new Promise((resolve, reject) => {
1688-
firebase.getValue(this.path).then(result => {
1689-
resolve({
1690-
key: result.key,
1691-
val: () => result.value,
1692-
exists: () => !!result.value
1682+
try {
1683+
if (firebase.instance === null) {
1684+
reject("Run init() first!");
1685+
return;
1686+
}
1687+
const listener = new com.google.firebase.database.ValueEventListener({
1688+
onDataChange: (snapshot: com.google.firebase.database.DataSnapshot) => {
1689+
resolve(nativeSnapshotToWebSnapshot(snapshot));
1690+
},
1691+
onCancelled: (databaseError: com.google.firebase.database.DatabaseError) => {
1692+
reject({
1693+
error: databaseError.getMessage()
1694+
});
1695+
}
16931696
});
1694-
});
1697+
// Kind of akward since Android only has single listener for the value event type...
1698+
firebase.instance.child(this.path).addListenerForSingleValueEvent(listener);
1699+
}
1700+
catch (ex) {
1701+
console.log("Error in firebase.once: " + ex);
1702+
reject(ex);
1703+
}
16951704
});
16961705
}
16971706

@@ -1709,34 +1718,22 @@ class Query implements QueryBase {
17091718
}
17101719

17111720
orderByChild(value: string): Query {
1712-
if (this.query) {
1713-
throw new Error("You can't combine multiple orderBy calls!");
1714-
}
1715-
this.query = this.dbRef.orderByChild(value);
1721+
this.query = this.query.orderByChild(value);
17161722
return this;
17171723
}
17181724

17191725
orderByKey(): Query {
1720-
if (this.query) {
1721-
throw new Error("You can't combine multiple orderBy calls!");
1722-
}
1723-
this.query = this.dbRef.orderByKey();
1726+
this.query = this.query.orderByKey();
17241727
return this;
17251728
}
17261729

17271730
orderByPriority(): Query {
1728-
if (this.query) {
1729-
throw new Error("You can't combine multiple orderBy calls!");
1730-
}
1731-
this.query = this.dbRef.orderByPriority();
1731+
this.query = this.query.orderByPriority();
17321732
return this;
17331733
}
17341734

17351735
orderByValue(): Query {
1736-
if (this.query) {
1737-
throw new Error("You can't combine multiple orderBy calls!");
1738-
}
1739-
this.query = this.dbRef.orderByValue();
1736+
this.query = this.query.orderByValue();
17401737
return this;
17411738
}
17421739

@@ -1790,7 +1787,7 @@ class Query implements QueryBase {
17901787
if (eventType === "value") {
17911788
listener = new com.google.firebase.database.ValueEventListener({
17921789
onDataChange: (snapshot: com.google.firebase.database.DataSnapshot) => {
1793-
callback(firebase.getCallbackData('ValueChanged', snapshot));
1790+
callback(nativeSnapshotToWebSnapshot(snapshot));
17941791
},
17951792
onCancelled: (databaseError: com.google.firebase.database.DatabaseError) => {
17961793
callback({
@@ -1807,22 +1804,22 @@ class Query implements QueryBase {
18071804
},
18081805
onChildAdded: (snapshot: com.google.firebase.database.DataSnapshot, previousChildKey: string) => {
18091806
if (eventType === "child_added") {
1810-
callback(firebase.getCallbackData(eventType, snapshot));
1807+
callback(nativeSnapshotToWebSnapshot(snapshot));
18111808
}
18121809
},
18131810
onChildRemoved: (snapshot: com.google.firebase.database.DataSnapshot) => {
18141811
if (eventType === "child_removed") {
1815-
callback(firebase.getCallbackData(eventType, snapshot));
1812+
callback(nativeSnapshotToWebSnapshot(snapshot));
18161813
}
18171814
},
18181815
onChildChanged: (snapshot: com.google.firebase.database.DataSnapshot, previousChildKey: string) => {
18191816
if (eventType === "child_changed") {
1820-
callback(firebase.getCallbackData(eventType, snapshot));
1817+
callback(nativeSnapshotToWebSnapshot(snapshot));
18211818
}
18221819
},
18231820
onChildMoved: (snapshot: com.google.firebase.database.DataSnapshot, previousChildKey: string) => {
18241821
if (eventType === "child_moved") {
1825-
callback(firebase.getCallbackData(eventType, snapshot));
1822+
callback(nativeSnapshotToWebSnapshot(snapshot));
18261823
}
18271824
}
18281825
});

0 commit comments

Comments
 (0)