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

Convert core to typescript #6340

Merged
merged 18 commits into from
Jan 31, 2020
Merged

Convert core to typescript #6340

merged 18 commits into from
Jan 31, 2020

Conversation

ivanpopelyshev
Copy link
Collaborator

@ivanpopelyshev ivanpopelyshev commented Jan 17, 2020

Its done. Lets review it. Discussion and a few details about conversion is in the post below.

@bigtimebuddy
Copy link
Member

Good start

@bigtimebuddy
Copy link
Member

bigtimebuddy commented Jan 19, 2020

My one request is you do-no-harm with this PR. Keep the changes as straightforward as possible and no refactoring.

There’s always time to improve later.

@ivanpopelyshev
Copy link
Collaborator Author

ivanpopelyshev commented Jan 19, 2020

Well, there is one small deprecation: I removed CubeTexture because it has from method that is not compatible with TS overrides. Easier to deprecate that than to add all necessary declarations.

@ivanpopelyshev
Copy link
Collaborator Author

OK, it should build, but I don't know how to deal with .glsl files. Please help!

@bigtimebuddy
Copy link
Member

@Zyie could use your help on this one

@Zyie
Copy link
Member

Zyie commented Jan 19, 2020

@ivanpopelyshev I believe this should do the trick.

Add this to the the global.d.ts file

declare module '*.frag' {
    const value: string;

    export default value;
}

declare module '*.vert' {
    const value: string;

    export default value;
}

@ivanpopelyshev
Copy link
Collaborator Author

OK, its building but now I have to fight typescript-eslint and few of those rules are just stupid.

   23:23  error    Identifier 'WEBGL_draw_buffers' is not in camel case              @typescript-eslint/camelcase
   24:24  error    Identifier 'OES_texture_float' is not in camel case               @typescript-eslint/camelcase
   25:23  error    Identifier 'WEBGL_lose_context' is not in camel case              @typescript-eslint/camelcase
   26:29  error    Identifier 'OES_vertex_array_object' is not in camel case         @typescript-eslint/camelcase
   27:32  error    Identifier 'EXT_texture_filter_anisotropic' is not in camel case  @typescript-eslint/camelcase
   28:30  error    Identifier 'OES_element_index_uint' is not in camel case          @typescript-eslint/camelcase
   29:24  error    Identifier 'OES_texture_float' is not in camel case               @typescript-eslint/camelcase
   30:30  error    Identifier 'OES_texture_float_linear' is not in camel case        @typescript-eslint/camelcase
   31:28  error    Identifier 'OES_texture_half_float' is not in camel case          @typescript-eslint/camelcase
   32:34  error    Identifier 'OES_texture_half_float_linear' is not in camel case   @typescript-eslint/camelcase
   33:28  error    Identifier 'WEBGL_color_buffer_float' is not in camel case

@codecov-io
Copy link

codecov-io commented Jan 22, 2020

Codecov Report

Merging #6340 into dev will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##             dev   #6340   +/-   ##
=====================================
  Coverage   74.7%   74.7%           
=====================================
  Files        105     105           
  Lines       6053    6053           
=====================================
  Hits        4522    4522           
  Misses      1531    1531

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8370b6e...55282a6. Read the comment docs.

@ivanpopelyshev
Copy link
Collaborator Author

OK, i have a few things:

I've tried to preserve everything even if I saw places where we could do better with TS. only in few cases I actually changed code a bit.

  1. I tried to preserveprivate,protected and readonly modifiers but they are real pain. Maybe I'll remove a few of them to make code better
  2. castToBaseTexture method exists to drop several occurences of texture || baseTexture in places that are not performance-important so one more function call surely wont be bad.
  3. I prefer to write Array<something> for arrays with heavy usage of push/pop, and something[] for constant arrays. Yes, they are the same in TS, but I like that detail from other languages, like ArrayList from java and List from C#. However for experssions like f(x:number[][] = []): number[][] i prefer [] because its short.
  4. Unused parameter requires 5 lines to disable eslint. What if we remove that requirement either from tsconfig? That will drop it to only one line.
  5. autoDetectRenderer() returns Renderer. I dont know whether we want CanvasRenderer there too. I believe it can be addressed in canvas-PR.
  6. CubeTexture was rarely used trash that had static from method with signature different from BaseTexture. I deprecated it.

@ivanpopelyshev ivanpopelyshev marked this pull request as ready for review January 22, 2020 16:26
@Zyie
Copy link
Member

Zyie commented Jan 22, 2020

  1. Unused parameter requires 5 lines to disable eslint. What if we remove that requirement either from tsconfig? That will drop it to only one line.

I think you can add an _ to the start of a parameter and typescript will understand that its an unused variable.

// eslint-disable-next-line @typescript-eslint/no-unused-vars
render(_object: any): void
{
}

This then makes it only one line

OK, its building but now I have to fight typescript-eslint and few of those rules are just stupid.

I agree think we should disable the @typescript-eslint/camelcase rule

@bigtimebuddy
Copy link
Member

@EvidentlyCube could you review this?

@EvidentlyCube
Copy link
Contributor

@bigtimebuddy I'll review it tomorrow!

Copy link
Member

@Zyie Zyie left a comment

Choose a reason for hiding this comment

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

Wow this is huge, well done @ivanpopelyshev, very impressive.

One thing I've noticed is that this PR has a very different style to the other typescript conversion pr's.

  • Previously all public variables declared as public
  • static variables were defined at the top of the class not the bottom
  • variables were organised by public, protected, private

Not sure how much you guys care about the consistency but i thought i would mention it

packages/core/src/AbstractRenderer.ts Outdated Show resolved Hide resolved
packages/core/src/AbstractRenderer.ts Outdated Show resolved Hide resolved
packages/core/src/Renderer.ts Outdated Show resolved Hide resolved
packages/core/src/Renderer.ts Outdated Show resolved Hide resolved
packages/core/src/batch/AbstractBatchRenderer.ts Outdated Show resolved Hide resolved
packages/core/src/textures/resources/VideoResource.ts Outdated Show resolved Hide resolved
packages/core/src/textures/resources/VideoResource.ts Outdated Show resolved Hide resolved
packages/core/src/textures/resources/VideoResource.ts Outdated Show resolved Hide resolved
packages/core/src/textures/resources/VideoResource.ts Outdated Show resolved Hide resolved
packages/core/src/textures/resources/SVGResource.ts Outdated Show resolved Hide resolved
Copy link
Member

@Zyie Zyie left a comment

Choose a reason for hiding this comment

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

Wow this is huge, well done @ivanpopelyshev, very impressive.

One thing I've noticed is that this PR has a very different style to the other typescript conversion pr's.

  • Previously all public variables declared as public
  • static variables were defined at the top of the class not the bottom
  • variables were organised by public, protected, private

Not sure how much you guys care about the consistency but i thought i would mention it

@@ -57,7 +59,7 @@ export const INSTALLED = [];
* texture should be updated from the video. Leave at 0 to update at every render
* @return {PIXI.resources.Resource} The created resource.
*/
export function autoDetectResource(source, options)
export function autoDetectResource(source: any, options: any): Resource
Copy link
Member

Choose a reason for hiding this comment

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

The options here are not any

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We only can do something like {[key: string: any}, because we don't know the resources that user adds there.

Copy link
Member

Choose a reason for hiding this comment

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

Yes we do. The options are in the documentation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can add those fields to satisfy autocomplete but we need `{[key:string]:any} just because its possible to use in custom resources.

Copy link
Member

Choose a reason for hiding this comment

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

We should define strict types and then add a generic type for extensibility. Just using the generic type isn’t that great

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I dont care about this place and it can be patched later when we find a case it doesnt work. Its still experimental API :)

You may change it and and other related places, and commit to this branch.

constructor(source, options)
items: Array<BaseTexture>;

constructor(source: Array<string|Resource>, options?: any)
Copy link
Member

Choose a reason for hiding this comment

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

The options here are not any

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

they are being passed to autoDetectResource, we only can do something like {[key: string: any}

Copy link
Member

Choose a reason for hiding this comment

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

You need stricter types here for options. My suggestion is to make CubeResourceOptions and then combine all the options into the autoDetectResource: CubeResourceOptions|CanvasResourceOptions|VideoResourseOptions

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

^^^ its up to you. However I remind you that "|" might have side effects. I remember our struggle with Point|ObservablePoint ;)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that’s fine in this case because these Resource-specific options are non-functional and don’t have method signatures like Point v ObservablePoint.

packages/core/src/Renderer.ts Show resolved Hide resolved
packages/core/src/Renderer.ts Show resolved Hide resolved
@ivanpopelyshev
Copy link
Collaborator Author

OK then i'll fix it and add public to fields that are public according to jsdoc and common sense.

Extra information.

Both I and @GoodBoyDigital have custom scene trees for pixijs. Its better if @pixi/core does not depend on @pixi/display.

Part of that is typings-related - that's why I made IFilterTarget, IMaskTarget, ISpriteMaskTarget.
However there's dependency from Renderer/AbstractRenderer classes that require code and API change, and that will be addressed in separate PR.

@ivanpopelyshev
Copy link
Collaborator Author

ivanpopelyshev commented Jan 23, 2020

OK, I'm applying your idea regarding public/protected/private . However I leave the fields that have mixed usage without any modifiers. I also remove readonly in many cases, because otherwise core code will look bad.

We still have those modifiers in jsdoc, so we can apply them if TS will allow us to have custom modifiers.

This requires one more pass over all the code.

@ivanpopelyshev
Copy link
Collaborator Author

Regarding statics - I left them where they were originally and our team expects them there. If we move stuff like from to the top of the file, it will take attention of user instead of actual constructor.

@ivanpopelyshev
Copy link
Collaborator Author

Wow this is huge, well done @ivanpopelyshev, very impressive.

Thank you! I have predisposition for large and brain-melting tasks, but I couldn't have a chance to show that without people who "prepare the field" for me.

@ivanpopelyshev
Copy link
Collaborator Author

OK, access modifiers are ready.

@ivanpopelyshev
Copy link
Collaborator Author

Btw, @Zyie, I had to rename all run to emit because we dont have typings for run in runner, and maybe we have to ask @GoodBoyDigital how to do it, its his place.

@ivanpopelyshev
Copy link
Collaborator Author

ivanpopelyshev commented Jan 26, 2020

I think we can merge this thing and treat autoDetectResource and IRendererContext improvements in separate PR.

Right now the only serious thing here is IRendererContext name, should we rename it?

@bigtimebuddy
Copy link
Member

I like IRenderContext

@ShukantPal ShukantPal self-requested a review January 26, 2020 23:39
Copy link
Contributor

@EvidentlyCube EvidentlyCube left a comment

Choose a reason for hiding this comment

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

Summary:

  • There are a bunch of conversations marked as resolved but they don't seem to involve the code actually being changed
  • There is a lot of inconsistency in when access modifiers are applied. I personally prefer to always have them explicit, but for me also any approach is fine provided it's standarized across the whole codebase :). I suggest adding eslint rule to cover that.
  • Ditto for Array<X> vs X[]
  • One more thing I noted, there are a bunch of {[key: string]: T}} in the code, how about introducting a generic type type Dictionary = {[key: string]: T}`?
  • 95 files is a lot, if I sound dry in my comments I didn't intend to!

And a suggestion for the future - when enormous modules like this one are converted to TS it might be a good idea to split it into smaller steps, maybe 20 files each. I have no hope of being able to go through 95 files and remain concentrated :).

protected readonly _tempDisplayObjectParent: DisplayObject;
protected _backgroundColor: number;
protected _backgroundColorString: string;
_backgroundColorRgba: number[];
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 different file you declare private explicitly, shouldn't that be followed here?
Also, it'd probably be a good idea to enforce explicit access modifiers in eslint if that's the way to go.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That particular field is used in RenderTextureSystem. I've decided not to add modifiers that can affect code readability.

packages/core/src/AbstractRenderer.ts Show resolved Hide resolved
packages/core/src/Renderer.ts Show resolved Hide resolved
packages/core/src/batch/BatchSystem.ts Outdated Show resolved Hide resolved
{
// do as you please!

filterManager.applyFilter(this, input, output, clearMode, currentState);
filterManager.applyFilter(this, input, output, clearMode);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this code changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

because that parameter is absent in filterManager.

packages/core/src/geometry/Buffer.ts Show resolved Hide resolved
packages/core/src/geometry/Geometry.ts Show resolved Hide resolved
packages/core/src/renderTexture/RenderTexturePool.ts Outdated Show resolved Hide resolved
@ivanpopelyshev
Copy link
Collaborator Author

@EvidentlyCube I forgot to push it :)

@ivanpopelyshev
Copy link
Collaborator Author

And a suggestion for the future - when enormous modules like this one are converted to TS it might be a good idea to split it into smaller steps, maybe 20 files each. I have no hope of being able to go through 95 files and remain concentrated :).

I feel like I'm rushing things, but from other point of view - we cant merge other PR's related to core while this one exists. Glory to our reviewers!

@bigtimebuddy bigtimebuddy added this to the v5.3.0 milestone Jan 27, 2020
@bigtimebuddy
Copy link
Member

While not perfect, I think this is good enough to merge. Further fixes can be added to address some of the outstanding things. Are you good @ivanpopelyshev or are you waiting for any other responses?

@ivanpopelyshev
Copy link
Collaborator Author

There are two possible improvements and one potential problem we can address in PR's.

  1. IRendererContext improvements - WebGL1 + polyfill
  2. autoDetectResource typings
  3. The problem with castToBaseTexture that is actually a problem with custom filters and other stuff - I need to confirm it before fix.

Summary: YES, we can merge it.

@ivanpopelyshev
Copy link
Collaborator Author

Actually, we cant merge it. There are failing examples, i didnt test everything :(

@bigtimebuddy
Copy link
Member

What’s broken? Could you keep me posted.

@ivanpopelyshev
Copy link
Collaborator Author

Graphics, both batched and main versions. Probably something with Geometry.. https://www.pixiplayground.com/#/edit/jPgE3ZEUhhVb7GJx9DuJb

@ivanpopelyshev
Copy link
Collaborator Author

OK, I've checked all examples. The one that doesnt work - ParticleContainer - fixed in #6371

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.

None yet

7 participants