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

Commit 8c5fb79

Browse files
committed
[query] - Update query.on() to follow web api and returns a Function instead of a promise
- Also remove ref from datasnapshot until we translate it into a web ref instead of native ref.
1 parent e1af4f6 commit 8c5fb79

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

src/app/database/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export namespace database {
1717
val(): any;
1818
}
1919

20-
export class Query {
20+
export class Query implements firebase.Query {
2121
protected path: string;
2222
private queryObject: firebase.Query;
2323
constructor(path: string) {
@@ -34,9 +34,8 @@ export namespace database {
3434
public on(eventType: string, callback: (a: DataSnapshot | null, b?: string) => any,
3535
cancelCallbackOrContext?: Object | null, context?: Object | null): (a: DataSnapshot | null, b?: string) => Function {
3636

37-
this.queryObject.on(eventType, callback).catch(error => {
38-
console.log("firebase.database().on error: " + error);
39-
});
37+
this.queryObject.on(eventType, callback);
38+
4039
return callback; // According to firebase doc we just return the callback given
4140
}
4241

src/firebase.android.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,41 +1640,36 @@ class Query implements QueryBase {
16401640
this.query = this.dbRef;
16411641
}
16421642

1643-
on(eventType: string, callback: (a: any, b?: string) => any): Promise<any> {
1643+
on(eventType: string, callback: (a: any, b?: string) => any): Function {
16441644
const onValueEvent = result => {
1645-
callback(result);
1645+
callback(result);
16461646
};
16471647

1648-
return new Promise((resolve, reject) => {
1649-
try {
1650-
if (firebase.instance === null) {
1651-
reject("Run init() first!");
1652-
return;
1653-
}
1654-
const listener = this.createEventListener(eventType, onValueEvent);
1648+
try {
1649+
if (firebase.instance === null) {
1650+
throw new Error("Run init() first!");
1651+
}
1652+
const listener = this.createEventListener(eventType, onValueEvent);
16551653

1656-
if (eventType === "value") {
1657-
this.query.addValueEventListener(listener as com.google.firebase.database.ValueEventListener);
1658-
} else if (eventType === "child_added" || eventType === "child_changed" || eventType === "child_removed" || eventType === "child_moved") {
1659-
this.query.addChildEventListener(listener as com.google.firebase.database.ChildEventListener);
1660-
} else {
1661-
callback({
1662-
error: "Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'"
1663-
});
1664-
reject("Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'");
1665-
return;
1666-
}
1667-
// Add listener to our map which keeps track of eventType: child/value events
1668-
if (!Query.eventListenerMap.has(eventType)) {
1669-
Query.eventListenerMap.set(eventType, []);
1670-
}
1671-
Query.eventListenerMap.get(eventType).push(listener); // We need to keep track of the listeners to fully remove them when calling off
1672-
resolve();
1673-
} catch (ex) {
1674-
console.log("Error in firebase.on: " + ex);
1675-
reject(ex);
1654+
if (eventType === "value") {
1655+
this.query.addValueEventListener(listener as com.google.firebase.database.ValueEventListener);
1656+
} else if (eventType === "child_added" || eventType === "child_changed" || eventType === "child_removed" || eventType === "child_moved") {
1657+
this.query.addChildEventListener(listener as com.google.firebase.database.ChildEventListener);
1658+
} else {
1659+
callback({
1660+
error: "Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'"
1661+
});
16761662
}
1677-
});
1663+
// Add listener to our map which keeps track of eventType: child/value events
1664+
if (!Query.eventListenerMap.has(eventType)) {
1665+
Query.eventListenerMap.set(eventType, []);
1666+
}
1667+
Query.eventListenerMap.get(eventType).push(listener); // We need to keep track of the listeners to fully remove them when calling off
1668+
} catch (ex) {
1669+
console.error("Error in firebase.on: " + ex);
1670+
} finally {
1671+
return callback;
1672+
}
16781673
}
16791674

16801675
once(eventType: string): Promise<DataSnapshot> {
@@ -2116,6 +2111,9 @@ firebase.transaction = (path: string, transactionUpdate: (currentState) => any,
21162111
});
21172112
};
21182113

2114+
function nativeRefToWebRef(ref: com.google.firebase.database.DatabaseReference) {
2115+
2116+
}
21192117
// Converts Android DataSnapshot into Web Datasnapshot
21202118
function nativeSnapshotToWebSnapshot(snapshot: com.google.firebase.database.DataSnapshot): DataSnapshot {
21212119
function forEach(action: (datasnapshot: DataSnapshot) => any): boolean {
@@ -2131,7 +2129,7 @@ function nativeSnapshotToWebSnapshot(snapshot: com.google.firebase.database.Data
21312129

21322130
return {
21332131
key: snapshot.getKey(),
2134-
ref: snapshot.getRef(),
2132+
// ref: snapshot.getRef(), TODO: Convert native ref to webRef
21352133
child: (path: string) => nativeSnapshotToWebSnapshot(snapshot.child(path)),
21362134
exists: () => snapshot.exists(),
21372135
forEach: (func: (datasnapshot) => any) => forEach(func),

src/firebase.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ export interface OnDisconnect {
549549

550550
// WebAPI Query
551551
export interface Query {
552-
on(eventType: string, callback: (a: any, b?: string) => any): Promise<any>;
552+
on(eventType: string, callback: (a: any, b?: string) => any): Function;
553553

554554
once(eventType: string): Promise<DataSnapshot>;
555555

@@ -576,7 +576,7 @@ export interface Query {
576576

577577
export interface DataSnapshot {
578578
key: string;
579-
ref: any; // TODO: Type it so that it returns a databaseReference.
579+
// ref: any; // TODO: It's not properly typed and returns a native Ref which will lead to errors
580580
child(path: string): DataSnapshot;
581581

582582
exists(): boolean;

src/firebase.ios.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,11 +1402,10 @@ class Query implements QueryBase {
14021402
this.query = this.dbRef;
14031403
}
14041404

1405-
on(eventType: string, callback: (a: any, b?: string) => any): Promise<any> {
1405+
on(eventType: string, callback: (a: any, b?: string) => any): Function {
14061406
const onValueEvent = result => {
14071407
callback(result);
14081408
};
1409-
return new Promise((resolve, reject) => {
14101409
try {
14111410
if (eventType === "value" || eventType === "child_added" || eventType === "child_changed"
14121411
|| eventType === "child_removed" || eventType === "child_moved") {
@@ -1420,15 +1419,13 @@ class Query implements QueryBase {
14201419
callback({
14211420
error: "Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'"
14221421
});
1423-
reject("Invalid eventType. Use one of the following: 'value', 'child_added', 'child_changed', 'child_removed', or 'child_moved'");
1424-
return;
14251422
}
1426-
resolve();
14271423
} catch (ex) {
14281424
console.log("Error in firebase.on: " + ex);
1429-
reject(ex);
14301425
}
1431-
});
1426+
finally {
1427+
return callback;
1428+
}
14321429
}
14331430

14341431
once(eventType: string): Promise<DataSnapshot> {
@@ -1838,7 +1835,7 @@ function nativeSnapshotToWebSnapshot(snapshot: FIRDataSnapshot): DataSnapshot {
18381835

18391836
return {
18401837
key: snapshot.key,
1841-
ref: snapshot.ref,
1838+
// ref: snapshot.ref,
18421839
child: (path: string) => nativeSnapshotToWebSnapshot(snapshot.childSnapshotForPath(path)),
18431840
exists: () => snapshot.exists(),
18441841
forEach: (func: (datasnapshot) => any) => forEach(func),

0 commit comments

Comments
 (0)