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

Commit 66f6216

Browse files
committed
[query][ios] - Make webapi queries return a datasnapshot that follows the webapi
1 parent 217ebdf commit 66f6216

File tree

1 file changed

+42
-47
lines changed

1 file changed

+42
-47
lines changed

src/firebase.ios.ts

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,55 +1395,60 @@ firebase.webQuery = (path: string): QueryBase => {
13951395
};
13961396

13971397
class Query implements QueryBase {
1398-
private query: FIRDatabaseQuery; // Keep track of internal query state allowing us to chain filter/range/limit
1398+
private query: FIRDatabaseQuery | FIRDatabaseReference; // Keep track of internal query state allowing us to chain filter/range/limit
13991399
private static eventListenerMap: Map<string, Array<any>> = new Map(); // A map to keep track all all the listeners attached for the specified eventType
14001400

1401-
constructor(private dbRef: FIRDatabaseReference, private path: string) { }
1401+
constructor(private dbRef: FIRDatabaseReference, private path: string) {
1402+
this.query = this.dbRef;
1403+
}
14021404

14031405
on(eventType: string, callback: (a: any, b?: string) => any): Promise<any> {
14041406
const onValueEvent = result => {
1405-
if (result.error) {
1406-
callback(result); // CAREFUL before we were calling result.error!
1407-
} else {
1408-
callback({
1409-
key: result.key,
1410-
val: () => result.value,
1411-
exists: () => !!result.value
1412-
});
1413-
}
1407+
callback(result);
14141408
};
14151409
return new Promise((resolve, reject) => {
14161410
try {
14171411
if (eventType === "value" || eventType === "child_added" || eventType === "child_changed"
14181412
|| eventType === "child_removed" || eventType === "child_moved") {
1419-
// This.query may not exist if we call on without any sorts
1420-
const firDatabaseHandle = this.query ? this.attachEventObserver(this.query, eventType, onValueEvent) :
1421-
this.attachEventObserver(this.dbRef, eventType, onValueEvent);
1413+
const firDataEventType = this.eventToFIRDataEventType(eventType);
1414+
const firDatabaseHandle = this.attachEventObserver(this.query, firDataEventType, onValueEvent);
14221415
if (!Query.eventListenerMap.has(eventType)) {
14231416
Query.eventListenerMap.set(eventType, []);
14241417
}
14251418
Query.eventListenerMap.get(eventType).push(firDatabaseHandle); // We need to keep track of the listeners to fully remove them when calling off
14261419
} else {
1420+
callback({
1421+
error: "Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'"
1422+
});
14271423
reject("Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'");
14281424
return;
14291425
}
14301426
resolve();
14311427
} catch (ex) {
1432-
console.log("Error in firebase.addValueEventListener: " + ex);
1428+
console.log("Error in firebase.on: " + ex);
14331429
reject(ex);
14341430
}
14351431
});
14361432
}
14371433

1438-
once(eventType: string): Promise<any> {
1434+
once(eventType: string): Promise<DataSnapshot> {
14391435
return new Promise((resolve, reject) => {
1440-
firebase.getValue(this.path).then(result => {
1441-
resolve({
1442-
key: result.key,
1443-
val: () => result.value,
1444-
exists: () => !!result.value
1445-
});
1446-
});
1436+
try {
1437+
const firDataEventType = this.eventToFIRDataEventType(eventType);
1438+
this.query.observeEventTypeWithBlockWithCancelBlock(
1439+
firDataEventType,
1440+
snapshot => {
1441+
resolve(nativeSnapshotToWebSnapshot(snapshot));
1442+
},
1443+
firebaseError => {
1444+
reject({
1445+
error: firebaseError.localizedDescription
1446+
});
1447+
});
1448+
} catch (ex) {
1449+
console.log("Error in firebase.once: " + ex);
1450+
reject(ex);
1451+
}
14471452
});
14481453
}
14491454

@@ -1461,34 +1466,22 @@ class Query implements QueryBase {
14611466
}
14621467

14631468
orderByChild(value: string): Query {
1464-
if (this.query) {
1465-
throw new Error("You can't combine multiple orderBy calls!");
1466-
}
1467-
this.query = this.dbRef.queryOrderedByChild(value);
1469+
this.query = this.query.queryOrderedByChild(value);
14681470
return this;
14691471
}
14701472

14711473
orderByKey(): Query {
1472-
if (this.query) {
1473-
throw new Error("You can't combine multiple orderBy calls!");
1474-
}
1475-
this.query = this.dbRef.queryOrderedByKey();
1474+
this.query = this.query.queryOrderedByKey();
14761475
return this;
14771476
}
14781477

14791478
orderByPriority(): Query {
1480-
if (this.query) {
1481-
throw new Error("You can't combine multiple orderBy calls!");
1482-
}
1483-
this.query = this.dbRef.queryOrderedByPriority();
1479+
this.query = this.query.queryOrderedByPriority();
14841480
return this;
14851481
}
14861482

14871483
orderByValue(): Query {
1488-
if (this.query) {
1489-
throw new Error("You can't combine multiple orderBy calls!");
1490-
}
1491-
this.query = this.dbRef.queryOrderedByValue();
1484+
this.query = this.query.queryOrderedByValue();
14921485
return this;
14931486
}
14941487

@@ -1532,12 +1525,7 @@ class Query implements QueryBase {
15321525
return this;
15331526
}
15341527

1535-
/**
1536-
* Depending on the eventType, attach listeners at the specified Database location. Follow the WebApi which listens
1537-
* to specific events (Android is more generic value / child - which includes all events add, change, remove etc).
1538-
* Similar to firebase._addObserver but I do not want to listen for every event
1539-
*/
1540-
private attachEventObserver(dbRef: FIRDatabaseQuery | FIRDatabaseReference, eventType: string, callback): number {
1528+
private eventToFIRDataEventType(eventType: string): FIRDataEventType {
15411529
let firEventType: FIRDataEventType;
15421530
switch (eventType) {
15431531
case "value":
@@ -1556,11 +1544,18 @@ class Query implements QueryBase {
15561544
firEventType = FIRDataEventType.ChildMoved;
15571545
break;
15581546
}
1559-
1547+
return firEventType;
1548+
}
1549+
/**
1550+
* Depending on the eventType, attach listeners at the specified Database location. Follow the WebApi which listens
1551+
* to specific events (Android is more generic value / child - which includes all events add, change, remove etc).
1552+
* Similar to firebase._addObserver but I do not want to listen for every event
1553+
*/
1554+
private attachEventObserver(dbRef: FIRDatabaseQuery | FIRDatabaseReference, firEventType: FIRDataEventType, callback): number {
15601555
const listener = dbRef.observeEventTypeWithBlockWithCancelBlock(
15611556
firEventType,
15621557
snapshot => {
1563-
callback(firebase.getCallbackData(eventType, snapshot));
1558+
callback(nativeSnapshotToWebSnapshot(snapshot));
15641559
},
15651560
firebaseError => {
15661561
callback({

0 commit comments

Comments
 (0)