Skip to content
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

Flow type workers #4903

Merged
merged 5 commits into from
Jun 28, 2017
Merged

Flow type workers #4903

merged 5 commits into from
Jun 28, 2017

Conversation

jfirebaugh
Copy link
Contributor

This adds flow annotations to the majority of the Worker-related code. I started on #4876, wanted to make some refactors, and realized that it would be better to have the safety of some static type checking as I did.

The first commit here switches the eslint sourceType option to module, so that we can use module types without resorting to flow comments.

Q: Why not use flow comments?
A: They're ugly, and they're not as good as native type annotations: not syntax highlighted, not linted (e.g. no warnings if you import something and don't use it), no documentation.js support, no IDE integration.

Q: What are the side effects of "sourceType": "module"?
A: AFAICT, it doesn't force us to actually use non-type import/export rather than CommonJS require, it just means that we can't 'use strict' in each file -- this is enforced by eslint because modules are always strict. I worked around this by writing a simple strictify transform that puts them back in. (I didn't use the published one because it's buggy and unmaintained.)

Q: Shouldn't you be able to use import type without requiring "sourceType": "module"?
A: Yes, I think you should be. If you agree, 👍 this bug.

Beyond that, the eslint + flow combo pretty much works. The only issue I've found is that eslint chokes on unnamed parameters in function types. So just name them:

Works

type Callback = (error: ?Error, result: ?Result) => void;

Doesn't Work

type Callback = (?Error, ?Result) => void;

@jfirebaugh jfirebaugh force-pushed the flow-worker branch 2 times, most recently from 9adfa41 to b59c4a7 Compare June 27, 2017 21:43
Copy link
Contributor

@anandthakker anandthakker left a comment

Choose a reason for hiding this comment

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

💯 this is so great.

*/
constructor(coord, size, sourceMaxZoom) {
constructor(coord: any, size: number, sourceMaxZoom: number) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be coord: TileCoord?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a rough pass; for many types I just used any where flow would otherwise complain. tile_coord.js hasn't been typed yet.

this.state = 'loading';

this.placementThrottler = new Throttler(300, this._immediateRedoPlacement.bind(this));
}

registerFadeDuration(animationLoop, duration) {
registerFadeDuration(animationLoop: any, duration: any) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not duration: number?

workerSources: { [string]: { [string]: WorkerSource } };

constructor(self: WorkerGlobalScope) {
this.self = (self: any);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 I don't see a cleaner way to do this. Could we stick a // comment here explaining the cast to any?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, would be nice if Object.assign(self, {registerWorkerSource: ..., registerRTLTextPlugin: ...}) worked, but it doesn't. I'll comment.

@@ -91,7 +119,7 @@ class CollisionTile {
];
}

serialize(transferables) {
serialize(transferables /*: ?Array<Transferable> */): SerializedCollisionTile {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why a comment for the parameter type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oversight.

@jfirebaugh jfirebaugh merged commit 763ae14 into master Jun 28, 2017
@jfirebaugh jfirebaugh deleted the flow-worker branch June 28, 2017 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants