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

(WIP, DON'T MERGE) Fix typings in scenes/context.ts #1828

Draft
wants to merge 5 commits into
base: v4
Choose a base branch
from

Conversation

Evertt
Copy link

@Evertt Evertt commented Apr 9, 2023

Fixes #1827

Copy link
Member

@MKRhere MKRhere left a comment

Choose a reason for hiding this comment

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

lgtm, thanks!

@MKRhere
Copy link
Member

MKRhere commented Apr 9, 2023

This will need a fix in Wizard Context; but before that, does this fix even work as expected for you?

@Evertt
Copy link
Author

Evertt commented Apr 9, 2023

This will need a fix in Wizard Context; but before that, does this fix even work as expected for you?

I initially wanted to type it as Exclude<D["state"], undefined>, but TypeScript wouldn't let me. But for some reason, when I type it as D["state"] it's already non-optional in my IDE. Which, to be honest, doesn't really make sense it me. But whether it comes out as optional or non-optional, both of those are already better than simply object in my opinion.

Edit

I'm happy to add this to the Wizard as well. I didn't do it thus yet, because I haven't been using the Wizard so I didn't run into the same problem.

@Evertt
Copy link
Author

Evertt commented Apr 12, 2023

@MKRhere Would you want me to apply this fix in the Wizard scene as well? Because I just tried it and I noticed that the wizard scene is set up quite differently from the base scene. And so I would be inclined to refactor it such that it works much more similar to how the base scene works. But I don't know if you guys made it different on purpose.

@MKRhere
Copy link
Member

MKRhere commented Apr 12, 2023

We cannot change the behaviour within a major version, and even for v5 there must be justification for the change. Right now, we just want Wizard scene to work along with this change, so there are no type errors.

@Evertt
Copy link
Author

Evertt commented Apr 12, 2023

We cannot change the behaviour within a major version, and even for v5 there must be justification for the change.

Okay, well I can think of a justification? Why would you allow to explicitly define a D extends SceneSessionData in scenes/context.ts:

export default class SceneContextScene<C extends SessionContext<SceneSession<D>>, D extends SceneSessionData = SceneSessionData> {}

But not do the same for the Wizard?

export default class WizardContextWizard<C extends SessionContext<WizardSession> & {
    scene: SceneContextScene<C, WizardSessionData>;
}> {}

I can't see any good reason for this disparity. So I'd propose that in v5, WizardContextWizard will basically exactly like SceneContextScene:

export default class WizardContextWizard<
    C extends WizardContext<D>,
    D extends WizardSessionData = WizardSessionData,
> {}

And then also the property readonly state: object could be turned into a getter and setter just like in SceneContextScene.

And from what I understand, v5 is just around the corner, so we can wait with implementing this PR until v5 for all I care. :-)

@MKRhere
Copy link
Member

MKRhere commented Apr 12, 2023

That would be a non-breaking change, because existing code doesn't break if D is not passed. Please make that change and we can land it in v4.

Even though TypeScript generates errors on some line, which is my I had to use `// @ts-expect-error` a few times, I can assure you that this is more type-correct.
@Evertt
Copy link
Author

Evertt commented Apr 25, 2023

@MKRhere I just pushed a new commit. Now I've really committed to getting it right. So it gets correctly compiled by tsc and also all the tests run by npm run test now run successfully.

I am aware that I changed the required generic types of some types. This was the only way to make it work and be type-correct.

If that means it can't be accepted for v4, shall I then make a new PR for v5?

edit

Wait, I still made a mistake, which was not caught by tsc or the test run, but I caught it by using it in my own code. I'll make a second edit once I've fixed this mistake.

edit 2

@MKRhere Okay now I fixed all of it. You can try it out for yourself. It's much more type accurate now and it even shows you a warning text when you're giving the wrong type!

image

edit 3

Nope, still not all of it is fixed. It's clear that you guys did not anticipate that each scene may want its own well-defined state.
And so now the Stage class is confused, and it's taking me too much time to fix it. 😅

So, yeah, don't merge this PR, consider it a Work In Progress

Let me know if you see any value in this though? In the end, I'd like to allow the user to define a unique state type for each scene, and for the Stage class to not be bothered by that.

And even, if one does something like:

const { enter, leave } = stage

enter<WelcomeSceneState>({ /* whatever fill in the data that the welcome scene expects... */ })

@Evertt Evertt changed the title Fix typings in scenes/context.ts (WIP, DON'T MERGE) Fix typings in scenes/context.ts Apr 25, 2023
@oitan
Copy link

oitan commented Aug 12, 2023

Is there a plan to merge this? Current types related to session, scene are broken by my experience. Or is this not related to it?

@Evertt
Copy link
Author

Evertt commented Aug 14, 2023

I'm sorry, I've been struggling with a depressive episode recently and I haven't been able to get myself to finish this or anything else. If someone else would be willing to take this over then I'd appreciate that.

@oitan
Copy link

oitan commented Aug 15, 2023

I'm sorry, I've been struggling with a depressive episode recently and I haven't been able to get myself to finish this or anything else. If someone else would be willing to take this over then I'd appreciate that.

I'm sorry to hear that. I hope you will get better soon! If this will not be a burden to you, can you please let me know what's left and push all the progress you have? I would appreciate it.

@MKRhere
Copy link
Member

MKRhere commented Aug 15, 2023

@Evertt Please do not stress yourself, take your time to recover. I'll look into this once I have time.

Current types related to session, scene are broken by my experience

@oitan This PR is only related to ctx.scene.state not being strongly typed. Generally session types are sound. If you have a different problem, please make an issue, and we'll look into that.

@MKRhere MKRhere marked this pull request as draft August 15, 2023 13:49
@SwapnilSoni1999
Copy link

Eagerly waiting for this to be merged! :) thanks sir

@Evertt
Copy link
Author

Evertt commented Mar 12, 2024

Eagerly waiting for this to be merged! :) thanks sir

@SwapnilSoni1999 Hey, well the good news is that my latest depressive episode seems have finally come to an end. The bad news is that my life is now much busier so I still can't promise that I will finish this.

But if anyone else is willing to pick up the slack then I very much welcome that. Why not you? 😉

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.

The typing of class SceneContextScene is incorrect
4 participants