Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generic events#10193

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
shirakaba wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromgeneric-events
Open

Generic events#10193

shirakaba wants to merge2 commits intomainfromgeneric-events

Conversation

shirakaba
Copy link
Contributor

@shirakabashirakaba commentedFeb 1, 2023
edited
Loading

With this PR, EventData and methods for adding/removing listeners will now be generic:

// This event is inherited from View, so eventData.object is// inferred to be a View.newButton().on("loaded",(eventData)=>{eventData.object;// TypeScript infers this to be a View});// You can, however, explicitly pass the generic to narrow// that down to Button:newButton().on<Button>("loaded",(eventData)=>{eventData.object;// TypeScript infers this to be a Button});// Button-specific events don't require passing the generic:newButton().on("tap",(eventData)=>{eventData.object;// TypeScript infers this to be a Button});

Please do give comment on the discussion points I've posted and (once we've reviewed it and prepared a build of it) a try on your projects, as I fully expect it'll result in some TypeScript grumbles.

PR Checklist

What is the current behavior?

Types for EventData and adding/removing listeners are non-generic, so we always have to writeeventData.object as Button if we want anything more specific than an Observable.

What is the new behavior?

Improved inference in most cases. The events in Application have always beenvery messy though, so I've left those as defaulting to Observable even though some could be narrowed to View, as I simply can't get my head round them.

Note that this PR would introduce breaking changes for library authors. As although we do provide default args for each generic, if they've extendedon anywhere, they'll have to add the generic, as demonstrated in thisplayground:

image

For discussion

What should the generics be?

The generics implemented in this PR are "better than nothing", but still not ideal. See what I ideally wanted to implement vs. what I was able to get working:

// What I wanted:exportabstractclassViewextendsViewCommon{on<TextendsView= this>(event:'loaded',callback:(args:EventData<T>)=>void,thisArg?:any);}// The only thing I could get to work:exportabstractclassViewextendsViewCommon{on<TextendsObservable=View>(event:'loaded',callback:(args:EventData<T>)=>void,thisArg?:any);}

The former is more strictly correct, but I just couldn't get TypeScript to accept it 🤷

Should we narrow the default generic for EventData in the first place?

Although I think itis correct for the methods for adding/removing event listeners to have more narrow generic defaults than justObservable, e.g.:

exportclassSegmentedBarextendsViewimplementsAddChildFromBuilder,AddArrayFromBuilder{on<TextendsObservable=SegmentedBar>(event:'selectedIndexChanged',callback:(args:SelectedIndexChangedEventData<T>)=>void,thisArg?:any):void;}

... I am unsure about whether the EventData interfaces should behave the same:

// Would we prefer a narrow default?exportinterfaceSelectedIndexChangedEventData<TextendsObservable=SegmentedBar>extendsEventData<T>{oldIndex:number;newIndex:number;}// ... or just stick to Observable?exportinterfaceSelectedIndexChangedEventData<TextendsObservable=Observable>extendsEventData<T>{oldIndex:number;newIndex:number;}

Mainly because we don't know whether others might repurpose the same event (SelectedIndexChangedEvent can be used for more than just SegmentedBar, and indeed there are duplicates of this interface across the codebase). Of course consumers can just specify the generic explicitly if they want to override the assumed default of SegmentedBar, so it's not the end of the world whichever choice we go with.

@nx-cloud
Copy link

nx-cloudbot commentedFeb 1, 2023
edited
Loading

☁️ Nx Cloud Report

We didn't find any information for the current pull request with the commit9a07b1b.
You might need to set the 'NX_BRANCH' environment variable in your CI pipeline.

Checkthe Nx Cloud Github Integration documentation for more information.


Sent with 💌 fromNxCloud.

@cla-botcla-botbot added the cla: yes labelFeb 1, 2023
@ammarahm-ed
Copy link
Contributor

ammarahm-ed commentedFeb 15, 2023
edited
Loading

This is ok but I think it doesn't really solve the problem. All Views "Should" infer to correct types in events automatically without passing some Generic or anything.

newButton().on<Button>("loaded",(eventData)=>{eventData.object;// TypeScript infers this to be a Button});

is almost as much work as doing

newButton().on("loaded",(eventData)=>{eventData.objectasButton;});

The ideal way would be to not have to pass any generic at all when you are subscribing to an event on Button. For example:

newButton().on("loaded",(eventData)=>{eventData.object// is Button automatically});

Also my question, why does this work or to be more specific, how does this work:

// Button-specific events don't require passing the generic:newButton().on("tap",(eventData)=>{eventData.object;// TypeScript infers this to be a Button});

@farfromrefug
Copy link
Collaborator

@ammarahm-ed be careful some views fire event for which the object is not them self! I know some plugins already do that. And I think some N components also do that. Will try to find them

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@NathanWalkerNathanWalkerAwaiting requested review from NathanWalker

@farfromrefugfarfromrefugAwaiting requested review from farfromrefug

@rigor789rigor789Awaiting requested review from rigor789

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@shirakaba@ammarahm-ed@farfromrefug

[8]ページ先頭

©2009-2025 Movatter.jp