Merge pull request #8331 from ciekawy/master

handle promises returned by firebase API
This commit is contained in:
Masahiro Wakame
2016-03-04 21:52:17 +09:00
2 changed files with 210 additions and 22 deletions

View File

@@ -27,9 +27,43 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authWithCustomToken(AUTH_TOKEN).then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
/*
* Firebase.authAnonymously()
*/
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authAnonymously().then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
// Log me in
@@ -60,6 +94,25 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authWithPassword({
"email": "bobtony@firebase.com",
"password": "correcthorsebatterystaple"
}).then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
/*
* Firebase.authWithOAuthPopup()
*/
@@ -75,6 +128,23 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authWithOAuthPopup("twitter").then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
/*
* Firebase.authWithOAuthRedirect
*/
@@ -90,6 +160,23 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function () {
// We'll never get here, as the page will redirect on success.
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Log me in
dataRef.authWithOAuthRedirect("twitter").then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
/*
* Firebase.authWithOAuthToken()
*/
@@ -104,6 +191,24 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/');
}
});
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
var onComplete = function (authData: FirebaseAuthData) {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
// Authenticate with Facebook using an existing OAuth 2.0 access token
dataRef.authWithOAuthToken("facebook", "<ACCESS-TOKEN>").then(onComplete, onError);
// Same as before but use returned Promise to handle the result
}
() => {
var dataRef = new Firebase('https://samplechat.firebaseio-demo.com');
// Authenticate with Twitter using an existing OAuth 1.0a credential set
@@ -226,6 +331,20 @@ var x4:string = fredRef3.name();
// when the data has finished synchronizing
}
() => {
var fredNameRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred/name');
var onComplete = function () {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
fredNameRef.set({ first: 'Fred', last: 'Flintstone' }).then(onComplete, onError);
// Same as the previous example but use returned Promise to handle the result
}
/*
* Firebase.update()
*/
@@ -247,6 +366,21 @@ var x4:string = fredRef3.name();
};
fredNameRef.update({ first: 'Wilma', last: 'Flintstone' }, onComplete);
}
() => {
var fredNameRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred/name');
var onComplete = function () {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
fredNameRef.update({ first: 'Fred', last: 'Flintstone' }).then(onComplete, onError);
// Same as the previous example but use returned Promise to handle the result
}
() => {
var fredRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred');
//The following 2 function calls are equivalent
@@ -276,6 +410,19 @@ var x4:string = fredRef3.name();
// a message when the delete has finished synchronizing
}
() => {
var onComplete = function () {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
fredRef.remove().then(onComplete, onError);
// Same as the previous example but use returned Promise to handle the result
}
/*
* Firebase.push()
*/
@@ -313,6 +460,33 @@ var x4:string = fredRef3.name();
// priority of the data so he'll be ordered relative to other users by his rank
}
() => {
var fredRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred');
var user = {
name: {
first: 'Fred',
last: 'Flintstone'
},
rank: 1000
};
fredRef.setWithPriority(user, 1000);
var onComplete = function () {
console.log('Synchronization succeeded');
};
var onError = function (error: any) {
if (error) {
console.log('Synchronization failed');
}
};
fredRef.setWithPriority(user, 1000).then(onComplete, onError);
// Same as the previous example but use returned Promise to handle the result
}
/*
* Firebase.setPriority()
*/

View File

@@ -1,6 +1,6 @@
// Type definitions for Firebase API 2.0.2
// Type definitions for Firebase API 2.4.1
// Project: https://www.firebase.com/docs/javascript/firebase
// Definitions by: Vincent Botone <https://github.com/vbortone/>, Shin1 Kashimura <https://github.com/in-async/>, Sebastien Dubois <https://github.com/dsebastien/>
// Definitions by: Vincent Botone <https://github.com/vbortone/>, Shin1 Kashimura <https://github.com/in-async/>, Sebastien Dubois <https://github.com/dsebastien/>, Szymon Stasik <https://github.com/ciekawy/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
interface FirebaseAuthResult {
@@ -68,27 +68,31 @@ interface FirebaseOnDisconnect {
* Ensures the data at this location is set to the specified value when the client is disconnected
* (due to closing the browser, navigating to a new page, or network issues).
*/
set(value: any, onComplete?: (error: any) => void): void;
set(value: any, onComplete: (error: any) => void): void;
set(value: any): Promise<void>;
/**
* Ensures the data at this location is set to the specified value and priority when the client is disconnected
* (due to closing the browser, navigating to a new page, or network issues).
*/
setWithPriority(value: any, priority: string, onComplete?: (error: any) => void): void;
setWithPriority(value: any, priority: number, onComplete?: (error: any) => void): void;
setWithPriority(value: any, priority: string|number, onComplete: (error: any) => void): void;
setWithPriority(value: any, priority: string|number): Promise<void>;
/**
* Writes the enumerated children at this Firebase location when the client is disconnected
* (due to closing the browser, navigating to a new page, or network issues).
*/
update(value: Object, onComplete?: (error: any) => void): void;
update(value: Object, onComplete: (error: any) => void): void;
update(value: Object): Promise<void>;
/**
* Ensures the data at this location is deleted when the client is disconnected
* (due to closing the browser, navigating to a new page, or network issues).
*/
remove(onComplete?: (error: any) => void): void;
remove(onComplete: (error: any) => void): void;
remove(): Promise<void>;
/**
* Cancels all previously queued onDisconnect() set or update events for this location and all children.
*/
cancel(onComplete?: (error: any) => void): void;
cancel(onComplete: (error: any) => void): void;
cancel(): Promise<void>;
}
interface FirebaseQuery {
@@ -141,9 +145,7 @@ interface FirebaseQuery {
/**
* Creates a Query which includes children which match the specified value.
*/
equalTo(value: string, key?: string): FirebaseQuery;
equalTo(value: number, key?: string): FirebaseQuery;
equalTo(value: boolean, key?: string): FirebaseQuery;
equalTo(value: string|number|boolean, key?: string): FirebaseQuery;
/**
* Generates a new Query object limited to the first certain number of children.
*/
@@ -163,32 +165,38 @@ interface Firebase extends FirebaseQuery {
* @deprecated Use authWithCustomToken() instead.
* Authenticates a Firebase client using the provided authentication token or Firebase Secret.
*/
auth(authToken: string, onComplete?: (error: any, result: FirebaseAuthResult) => void, onCancel?:(error: any) => void): void;
auth(authToken: string, onComplete: (error: any, result: FirebaseAuthResult) => void, onCancel?:(error: any) => void): void;
auth(authToken: string): Promise<FirebaseAuthResult>;
/**
* Authenticates a Firebase client using an authentication token or Firebase Secret.
*/
authWithCustomToken(autoToken: string, onComplete: (error: any, authData: FirebaseAuthData) => void, options?:Object): void;
authWithCustomToken(autoToken: string, options?:Object): Promise<FirebaseAuthData>;
/**
* Authenticates a Firebase client using a new, temporary guest account.
*/
authAnonymously(onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void;
authAnonymously(options?: Object): Promise<FirebaseAuthData>;
/**
* Authenticates a Firebase client using an email / password combination.
*/
authWithPassword(credentials: FirebaseCredentials, onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void;
authWithPassword(credentials: FirebaseCredentials, options?: Object): Promise<FirebaseAuthData>;
/**
* Authenticates a Firebase client using a popup-based OAuth flow.
*/
authWithOAuthPopup(provider: string, onComplete:(error: any, authData: FirebaseAuthData) => void, options?: Object): void;
authWithOAuthPopup(provider: string, options?: Object): Promise<FirebaseAuthData>;
/**
* Authenticates a Firebase client using a redirect-based OAuth flow.
*/
authWithOAuthRedirect(provider: string, onComplete: (error: any) => void, options?: Object): void;
authWithOAuthRedirect(provider: string, options?: Object): Promise<void>;
/**
* Authenticates a Firebase client using OAuth access tokens or credentials.
*/
authWithOAuthToken(provider: string, credentials: string, onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void;
authWithOAuthToken(provider: string, credentials: Object, onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void;
authWithOAuthToken(provider: string, credentials: string|Object, onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void;
authWithOAuthToken(provider: string, credentials: string|Object, options?: Object): Promise<FirebaseAuthData>;
/**
* Synchronously access the current authentication state of the client.
*/
@@ -233,30 +241,33 @@ interface Firebase extends FirebaseQuery {
/**
* Writes data to this Firebase location.
*/
set(value: any, onComplete?: (error: any) => void): void;
set(value: any, onComplete: (error: any) => void): void;
set(value: any): Promise<void>;
/**
* Writes the enumerated children to this Firebase location.
*/
update(value: Object, onComplete?: (error: any) => void): void;
update(value: Object, onComplete: (error: any) => void): void;
update(value: Object): Promise<void>;
/**
* Removes the data at this Firebase location.
*/
remove(onComplete?: (error: any) => void): void;
remove(onComplete: (error: any) => void): void;
remove(): Promise<void>;
/**
* Generates a new child location using a unique name and returns a Firebase reference to it.
* @returns {Firebase} A Firebase reference for the generated location.
*/
push(value?: any, onComplete?: (error: any) => void): Firebase;
push(value?: any, onComplete?: (error: any) => void): FirebaseWithPromise<void>;
/**
* Writes data to this Firebase location. Like set() but also specifies the priority for that data.
*/
setWithPriority(value: any, priority: string, onComplete?: (error: any) => void): void;
setWithPriority(value: any, priority: number, onComplete?: (error: any) => void): void;
setWithPriority(value: any, priority: string|number, onComplete: (error: any) => void): void;
setWithPriority(value: any, priority: string|number): Promise<void>;
/**
* Sets a priority for the data at this Firebase location.
*/
setPriority(priority: string, onComplete?: (error: any) => void): void;
setPriority(priority: number, onComplete?: (error: any) => void): void;
setPriority(priority: string|number, onComplete: (error: any) => void): void;
setPriority(priority: string|number): Promise<void>;
/**
* Atomically modifies the data at this location.
*/
@@ -283,6 +294,9 @@ interface Firebase extends FirebaseQuery {
resetPassword(credentials: FirebaseResetPasswordCredentials, onComplete: (error: any) => void): void;
onDisconnect(): FirebaseOnDisconnect;
}
interface FirebaseWithPromise<T> extends Firebase, Promise<T> {}
interface FirebaseStatic {
/**
* Constructs a new Firebase reference from a full Firebase URL.