-
Notifications
You must be signed in to change notification settings - Fork 930
Typings: Add Observer interface and improve typings for Auth, Storage, and Messaging #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Out of curiosity: these typing files seem to be auto-generated. Was that a one-time thing, or is there a process to regenerate them when there's changes to the API? |
typings/app.d.ts
Outdated
@@ -123,7 +123,7 @@ declare namespace firebase.auth { | |||
currentUser : firebase.User | null ; | |||
fetchProvidersForEmail (email : string ) : firebase.Promise < any > ; | |||
getRedirectResult ( ) : firebase.Promise < any > ; | |||
onAuthStateChanged (nextOrObserver : Object , error ? : (a : firebase.auth.Error ) => any , completed ? : ( ) => any ) : ( ) => any ; | |||
onAuthStateChanged (nextOrObserver : Object | ((user: firebase.User | null) => any), error ? : (a : firebase.auth.Error ) => any , completed ? : ( ) => any ) : ( ) => any ; | |||
onIdTokenChanged (nextOrObserver : Object , error ? : (a : firebase.auth.Error ) => any , completed ? : ( ) => any ) : ( ) => any ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're at it, can you make the same change here too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I'll check if there's any other places too.
How about also introducing an Observer
interface to use instead of the more generic Object
?
interface Observer {
next?: (user: firebase.User | null) => any;
error?: (error: firebase.auth.Error) => any;
complete?: () => any;
}
typings/firebase.d.ts
Outdated
@@ -123,7 +123,7 @@ declare namespace firebase.auth { | |||
currentUser : firebase.User | null ; | |||
fetchProvidersForEmail (email : string ) : firebase.Promise < any > ; | |||
getRedirectResult ( ) : firebase.Promise < any > ; | |||
onAuthStateChanged (nextOrObserver : Object , error ? : (a : firebase.auth.Error ) => any , completed ? : ( ) => any ) : ( ) => any ; | |||
onAuthStateChanged (nextOrObserver : Object | ((user: firebase.User | null) => any), error ? : (a : firebase.auth.Error ) => any , completed ? : ( ) => any ) : ( ) => any ; | |||
onIdTokenChanged (nextOrObserver : Object , error ? : (a : firebase.auth.Error ) => any , completed ? : ( ) => any ) : ( ) => any ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The auth changes look fine. You will need someone from messaging and storage to confirm the other changes.
@gauntface Can you also take a look at this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a straightforward improvement, just one comment.
typings/app.d.ts
Outdated
@@ -76,6 +76,12 @@ declare namespace firebase { | |||
uid : string ; | |||
} | |||
|
|||
interface Observer<V, E> { | |||
next?: (value: V | null) => any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a few places inside the SDK, these are typed with a void return value (
firebase-js-sdk/src/app/subscribe.ts
Line 16 in 657055d
export type NextFn<T> = (value: T) => void; |
type NextFn<T> = (value: T) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(note: also applies to the return types in the function signatures below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Should these same changes be reflected in the type annotation comments of the externs files? |
Hmm, forgot about those. As far as I can tell we've been keeping the TS<->Closure typings in sync, so the Closure externs files should also be updated. |
@sphippen I just pushed the changes to the externs to get them in sync with the typings. I'm not extremely familiar with Closure compiler annotations so make sure to review those carefully, please :) I've also made a few more improvements to the typings while I was at it. I added the |
externs/firebase-app-externs.js
Outdated
|
||
firebase.Observer.prototype.complete = function() {}; | ||
|
||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately Closure doesn't support templated typedefs:
../../externs/firebase-app-externs.js:296: WARNING - Bad type annotation. type annotation incompatible with other annotations. See https://github.com/google/closure-compiler/wiki/Bad-Type-Annotation for more information.
* @typedef {function(?V): void}
^
../../externs/firebase-app-externs.js:298: WARNING - Misplaced template annotation. @template is only allowed in class, constructor, interface, function or method declarations
firebase.NextFn;
^^^^^^^^^^^^^^^
../../externs/firebase-app-externs.js:302: WARNING - Bad type annotation. type annotation incompatible with other annotations. See https://github.com/google/closure-compiler/wiki/Bad-Type-Annotation for more information.
* @typedef {function(!E): void}
^
../../externs/firebase-app-externs.js:304: WARNING - Misplaced template annotation. @template is only allowed in class, constructor, interface, function or method declarations
firebase.ErrorFn;
^^^^^^^^^^^^^^^^
It's probably best to leave the NextFn and ErrorFn arguments as raw "!function(...)" annotations instead of adding the typedefs here. I'd be fine if you left CompleteFn and Unsubscribe in as typedefs and added a comment about the templated typedefs being unfeasible or you just removed all of them. Either one is good with me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah well, too bad. I removed the templates from NextFn and ErrorFn and moved the typedefs back to the INTERNAL file, where they're still used. I replaced all the other uses with the original !function(...)
annotations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one last change on the Closure externs that I missed the first time, then this all looks good to me.
externs/firebase-app-externs.js
Outdated
|
||
/** | ||
* @template V | ||
* @param {?V} value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one last change, put the "@template V" and "@template E" annotations in the same comment block as the "@interface" instead of on the individual methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this?
/**
* @template V, E
* @interface
**/
firebase.Observer = function() {};
/**
* @param {?V} value
*/
firebase.Observer.prototype.next = function(value) {};
/**
* @param {!E} error
*/
firebase.Observer.prototype.error = function(error) {};
firebase.Observer.prototype.complete = function() {};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Awesome, looks good to me. Thanks for the PR! And since @bojeil-google and @gauntface have given their OK on this I'll go ahead and merge the PR now. |
Discussion
Fixes issue #54
Testing
n/a
API Changes
n/a