Skip to content

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

Merged
merged 1 commit into from
Jul 5, 2017

Conversation

jsayol
Copy link
Contributor

@jsayol jsayol commented Jun 15, 2017

Discussion

Fixes issue #54

Testing

n/a

API Changes

n/a

@jsayol
Copy link
Contributor Author

jsayol commented Jun 15, 2017

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?

@jshcrowthe jshcrowthe requested a review from bojeil-google June 16, 2017 05:07
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 ;
Copy link
Contributor

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?

Copy link
Contributor Author

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;
}

@@ -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 ;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both done.

@jsayol jsayol changed the title fix(auth): improve typings for onAuthStateChanged fix(auth): improve typings for Auth methods Jun 16, 2017
@jsayol jsayol changed the title fix(auth): improve typings for Auth methods Typings: Add Observer interface and improve typings for Auth, Storage, and Messaging Jun 16, 2017
Copy link
Contributor

@bojeil-google bojeil-google left a 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.

@bojeil-google bojeil-google requested a review from sphippen June 16, 2017 17:13
@jshcrowthe
Copy link
Contributor

@gauntface Can you also take a look at this?

@jshcrowthe jshcrowthe requested a review from gauntface June 16, 2017 18:38
Copy link
Contributor

@sphippen sphippen left a 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;
Copy link
Contributor

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 (

export type NextFn<T> = (value: T) => void;
,
type NextFn<T> = (value: T) => void;
). Let's make the return types here "void" instead of "any" for consistency.

Copy link
Contributor

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@jsayol
Copy link
Contributor Author

jsayol commented Jun 28, 2017

Should these same changes be reflected in the type annotation comments of the externs files?

@sphippen
Copy link
Contributor

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.

@jsayol
Copy link
Contributor Author

jsayol commented Jun 28, 2017

@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 NextFn, ErrorFn, and CompleteFn types (they were already present in the externs) and I modified the signature of the firebase.storage.UploadTask#on() method to reflect the two different usages.


firebase.Observer.prototype.complete = function() {};

/**
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@sphippen sphippen left a 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.


/**
* @template V
* @param {?V} value
Copy link
Contributor

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.

Copy link
Contributor Author

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() {};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@sphippen
Copy link
Contributor

sphippen commented Jul 5, 2017

Awesome, looks good to me.
Also @jsayol, re: automatic generation of the typings, they have always been maintained by hand. If I remember correctly the TypeScript typings were based on a machine-converted form of the Closure externs files, but we had to do some manual cleanup afterward and have been maintaining them by hand since.

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.

@sphippen sphippen merged commit 6aaef70 into firebase:master Jul 5, 2017
@jsayol jsayol deleted the fix-issue-54 branch July 13, 2017 07:21
@firebase firebase locked and limited conversation to collaborators Oct 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants