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

Commit dd9382d

Browse files
#547 Add a 'getValue' function to mimic the Web API's 'once'
1 parent fd34b6f commit dd9382d

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/firebase.android.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ function toLoginResult(user) {
924924
// provider: user.getProviderId(), // always 'firebase'
925925
providers: providers,
926926
anonymous: user.isAnonymous(),
927+
isAnonymous: user.isAnonymous(),
927928
phoneNumber: user.getPhoneNumber(),
928929
profileImageURL: user.getPhotoUrl() ? user.getPhotoUrl().toString() : null
929930
};
@@ -1514,6 +1515,25 @@ firebase.addValueEventListener = (updateCallback, path) => {
15141515
});
15151516
};
15161517

1518+
firebase.getValue = path => {
1519+
return new Promise((resolve, reject) => {
1520+
try {
1521+
const listener = new com.google.firebase.database.ValueEventListener({
1522+
onDataChange: snapshot => {
1523+
resolve(firebase.getCallbackData('ValueChanged', snapshot));
1524+
},
1525+
onCancelled: databaseError => {
1526+
reject(databaseError.getMessage());
1527+
}
1528+
});
1529+
firebase.instance.child(path).addListenerForSingleValueEvent(listener);
1530+
} catch (ex) {
1531+
console.log("Error in firebase.getValue: " + ex);
1532+
reject(ex);
1533+
}
1534+
});
1535+
};
1536+
15171537
firebase.removeEventListeners = (listeners, path) => {
15181538
return new Promise((resolve, reject) => {
15191539
try {

src/firebase.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export interface User {
268268
name?: string;
269269
phoneNumber?: string;
270270
anonymous: boolean;
271+
isAnonymous: boolean; // This is used by the web API
271272
providers: Array<Provider>;
272273
profileImageURL?: string;
273274
/** iOS only */
@@ -539,6 +540,8 @@ export function init(options?: InitOptions): Promise<any>;
539540
// Database
540541
export function push(path: string, value: any): Promise<PushResult>;
541542

543+
export function getValue(path: string): Promise<any>;
544+
542545
export function setValue(path: string, value: any): Promise<any>;
543546

544547
export function update(path: string, value: any): Promise<any>;

src/firebase.ios.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ firebase.toJsObject = objCObj => {
583583
return node;
584584
};
585585

586-
firebase.getCallbackData = (type, snapshot) => {
586+
firebase.getCallbackData = (type, snapshot: FIRDataSnapshot) => {
587587
return {
588588
type: type,
589589
key: snapshot.key,
@@ -1090,6 +1090,7 @@ function toLoginResult(user) {
10901090
return {
10911091
uid: user.uid,
10921092
anonymous: user.anonymous,
1093+
isAnonymous: user.anonymous,
10931094
// provider: user.providerID, // always 'Firebase'
10941095
providers: providers,
10951096
profileImageURL: user.photoURL ? user.photoURL.absoluteString : null,
@@ -1639,6 +1640,25 @@ firebase.addValueEventListener = (updateCallback, path) => {
16391640
});
16401641
};
16411642

1643+
firebase.getValue = path => {
1644+
return new Promise((resolve, reject) => {
1645+
try {
1646+
const where = path === undefined ? firebase.instance : firebase.instance.childByAppendingPath(path);
1647+
const listener = where.observeSingleEventOfTypeWithBlockWithCancelBlock(
1648+
FIRDataEventType.Value,
1649+
snapshot => {
1650+
resolve(firebase.getCallbackData('ValueChanged', snapshot));
1651+
},
1652+
firebaseError => {
1653+
reject(firebaseError.localizedDescription);
1654+
});
1655+
} catch (ex) {
1656+
console.log("Error in firebase.getValue: " + ex);
1657+
reject(ex);
1658+
}
1659+
});
1660+
};
1661+
16421662
firebase.removeEventListeners = (listeners, path) => {
16431663
return new Promise((resolve, reject) => {
16441664
try {

0 commit comments

Comments
 (0)