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: Add type parameter of event subtype for app.message listeners #796 #871

Closed
wants to merge 1 commit into from

Conversation

seratch
Copy link
Member

@seratch seratch commented Apr 9, 2021

Summary

This pull request shows a possible improvement for #796 use case - better support for message event subtypes in TypeScript. I'm still wondering if there may be a better solution than this one. I would love to get inputs and feedback from anyone who is interested in this topic.

Requirements (place an x in each [ ])

@seratch seratch added enhancement M-T: A feature request for new functionality semver:minor TypeScript-specific labels Apr 9, 2021
@seratch seratch added this to the 3.4.0 milestone Apr 9, 2021
@seratch seratch self-assigned this Apr 9, 2021
@codecov
Copy link

codecov bot commented Apr 9, 2021

Codecov Report

Merging #871 (3256fee) into main (4365f45) will not change coverage.
The diff coverage is n/a.

❗ Current head 3256fee differs from pull request most recent head 0fce896. Consider uploading reports for the commit 0fce896 to get more accurate results
Impacted file tree graph

@@           Coverage Diff           @@
##             main     #871   +/-   ##
=======================================
  Coverage   66.08%   66.08%           
=======================================
  Files          13       13           
  Lines        1200     1200           
  Branches      353      353           
=======================================
  Hits          793      793           
  Misses        338      338           
  Partials       69       69           
Impacted Files Coverage Δ
src/App.ts 82.76% <ø> (ø)

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 4365f45...0fce896. Read the comment docs.

);
expectType<void>(
// no subtype in the event payload
app.message<undefined>('foo', async ({ message }) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Setting undefined as the default can be a breaking changes to the existing apps.

EventType extends string = string,
EventSubtype extends string | undefined = undefined
> {
payload: EventSubtype extends '*' ? EventFromType<EventType> : EventFromTypeAndSubtype<EventType, EventSubtype>;
Copy link
Member Author

Choose a reason for hiding this comment

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

To keep backward compatibility, we are unable to use undefined for subtype here.

public message(...listeners: Middleware<SlackEventMiddlewareArgs<'message'>>[]): void;
public message(pattern: string | RegExp, ...listeners: Middleware<SlackEventMiddlewareArgs<'message'>>[]): void;
public message(...patternsOrMiddleware: (string | RegExp | Middleware<SlackEventMiddlewareArgs<'message'>>)[]): void {
public message<Subtype extends string | undefined = '*'>(
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think this asterisk string is cool but what else can be relevant here?

Copy link
Member

Choose a reason for hiding this comment

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

Asterisk made sense to me while reading it over. 👍🏻

@@ -4,8 +4,6 @@ import { App, MessageEvent, GenericMessageEvent, BotMessageEvent, MessageReplied
const app = new App({ token: 'TOKEN', signingSecret: 'Signing Secret' });

expectType<void>(
// TODO: Resolve the event type when having subtype in a listener constraint
// app.message({pattern: 'foo', subtype: 'message_replied'}, async ({ message }) => {});
Copy link
Member Author

Choose a reason for hiding this comment

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

As I mentioned in this comment, app.message({pattern: 'foo', subtype: 'message_replied'} or something like that can be another option. I'm not sure if it's better than the type parameter though.

Copy link
Member

@mwbrooks mwbrooks left a comment

Choose a reason for hiding this comment

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

Personally, like the approach of app.message({pattern: 'foo', subtype: 'message_replied'}. It's the syntax that I would have guessed as well. Did you have another in mind?

public message(...listeners: Middleware<SlackEventMiddlewareArgs<'message'>>[]): void;
public message(pattern: string | RegExp, ...listeners: Middleware<SlackEventMiddlewareArgs<'message'>>[]): void;
public message(...patternsOrMiddleware: (string | RegExp | Middleware<SlackEventMiddlewareArgs<'message'>>)[]): void {
public message<Subtype extends string | undefined = '*'>(
Copy link
Member

Choose a reason for hiding this comment

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

Asterisk made sense to me while reading it over. 👍🏻

): void;
public message<Subtype extends string | undefined = '*'>(
...patternsOrMiddleware: (string | RegExp | Middleware<SlackSubtypedEventMiddlewareArgs<'message', Subtype>>)[]
): void {
const messageMiddleware = patternsOrMiddleware.map((patternOrMiddleware) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO: oops, I forgot to add subtype constraint to the middleware. will do later.

@seratch seratch changed the title Add type parameter of event subtype for app.message listeners #796 WIP: Add type parameter of event subtype for app.message listeners #796 Apr 9, 2021
@seratch seratch marked this pull request as draft April 9, 2021 22:35
@seratch
Copy link
Member Author

seratch commented Apr 9, 2021

converted to draft for safety

@seratch seratch modified the milestones: 3.4.0, 3.5.0 Apr 12, 2021
@seratch seratch modified the milestones: 3.5.0, 3.4.0 May 12, 2021
@seratch
Copy link
Member Author

seratch commented Jun 2, 2021

As we are aiming to release v3.4.0 within a few days and this pull request needs more time, let me move this one to v3.5 milestone.

@seratch seratch modified the milestones: 3.4.0, 3.5.0 Jun 2, 2021
@seratch seratch modified the milestones: 3.5.0, 3.6.0 Jul 2, 2021
@seratch seratch modified the milestones: 3.6.0, 3.7.0 Aug 17, 2021
@seratch seratch added this to the 3.x milestone Aug 23, 2021
@seratch
Copy link
Member Author

seratch commented Aug 15, 2022

Let me close this long-lived but incomplete PR. If someone would like to reuse the code changes in the future, go ahead with it! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement M-T: A feature request for new functionality semver:minor TypeScript-specific
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants