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

changed arg type of EventmitHandler #2

Closed
wants to merge 1 commit into from

Conversation

ncukondo
Copy link

@ncukondo ncukondo commented Feb 1, 2021

It allows variable args with type.

For example

const event1 = eventmit();
// Register handler
event1.on(() => {
    console.log(1, "value");
});
// Invoke handler
event1.emit();

const event2 = eventmit<{ key: string }>();
// Register handler
event2.on((value) => {
    console.log(1, value);
});
event2.on((value) => {
    console.log(2, value);
});
// Invoke handler
event2.emit({
    key: "value"
});
// Unregister handler
event.offAll();

const event3 = eventmit<[type:string,message:string]>();
// Register handler
event3.on((type,message) => {
    console.log(1, `eventType:${type} message:${message}`);
});
// Invoke handler
event3.emit("customEvent1","some message");

It allows variable args with type.

For example
const event1 = eventmit();
// Register handler
event1.on(() => {
    console.log(1, "value");
});
// Invoke handler
event1.emit();

const event2 = eventmit<{ key: string }>();
// Register handler
event2.on((value) => {
    console.log(1, value);
});
event2.on((value) => {
    console.log(2, value);
});
// Invoke handler
event2.emit({
    key: "value"
});
// Unregister handler
event.offAll();

const event3 = eventmit<[type:string,message:string]>();
// Register handler
event3.on((type,message) => {
    console.log(1, `eventType:${type} message:${message}`);
});
// Invoke handler
event3.emit("customEvent1","some message");
@azu azu added Status: Proposal Request for comments Type: Feature New Feature labels Feb 1, 2021
@azu
Copy link
Owner

azu commented Feb 1, 2021

Thanks for PR.
I understand your needs.
📝 I found the same issue on mitt developit/mitt#115

However, I want to keep API simple.
The generics keyword is a tuple of types, but EventmitHandler and emit receive arguments as rest elements.
I think that It is hard to make this API instinctive.

// tuple/array types
// tuple is best type because tuple label is useful
const event3 = eventmit<[type:string,message:string]>();
// vs. ...rest element
// tuple is implicitly expanded as rest parameter 
event3.on((type,message) => {
    console.log(1, `eventType:${type} message:${message}`);
});
event3.emit("customEvent1", "some message");

I have not the strong motivation for this. I close this PR.
Thanks.

@azu azu closed this Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Proposal Request for comments Type: Feature New Feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants