-
Notifications
You must be signed in to change notification settings - Fork 399
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
Fix #1098 next() is optional in middleware in TypeScript #1099
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1099 +/- ##
=======================================
Coverage 71.71% 71.71%
=======================================
Files 15 15
Lines 1354 1354
Branches 402 402
=======================================
Hits 971 971
Misses 312 312
Partials 71 71
Continue to review full report at Codecov.
|
@@ -291,12 +291,12 @@ describe('WorkflowStep', () => { | |||
|
|||
describe('processStepMiddleware', () => { | |||
it('should call each callback in user-provided middleware', async () => { | |||
const { next: _next, ...fakeArgs } = createFakeStepEditAction() as unknown as AllWorkflowStepMiddlewareArgs; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createFakeStepEditAction()
(and others similar) adds next
inside but we remove it from fakeArgs
in this line. AllWorkflowStepMiddlewareArgs
minus next
property does not compile with processStepMiddleware
in this test method. This is why I've removed the next: sinon.fake()
in this test. Just in case, we can need to do more tests to verify that we are not breaking anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only vaguely remember writing this, but what I think I recall is that the removal of next
here was to mimic the switch out that occurs of the default/usual next
and the replacement of it with the user-provided callback. It's been a while, however, so not sure if this file has since been changed since that was originally written.
@@ -886,7 +887,8 @@ export default class App { | |||
context, | |||
client, | |||
logger: this.logger, | |||
}), | |||
// `next` is already set in the outer processMiddleware |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code here is a bit tricky. You can check the internals of processMiddleware
. The outer processMiddleware
call quietly sets next
and middleware execution relies on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the comments on 884 and 871 technically misleading then, if next
is actually set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@srajiang Indeed, last()
and L871 may be confusing. In my understanding, the last element of an array of middleware technically can have next()
but we don't do anything with it. As you saw, inside the processMiddleware
method, we call last()
instead of next middleware with recursive invokeMiddleware
call.
We may want to update the L871 comment when merging this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, nice improvement!
src/WorkflowStep.spec.ts
Outdated
@@ -291,12 +291,12 @@ describe('WorkflowStep', () => { | |||
|
|||
describe('processStepMiddleware', () => { | |||
it('should call each callback in user-provided middleware', async () => { | |||
const { next: _next, ...fakeArgs } = createFakeStepEditAction() as unknown as AllWorkflowStepMiddlewareArgs; | |||
const { ...fakeArgs } = createFakeStepEditAction() as unknown as AllWorkflowStepMiddlewareArgs; | |||
const { processStepMiddleware } = await importWorkflowStep(); | |||
|
|||
const fn1 = sinon.spy((async ({ next: continuation }) => { | |||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can now safely remove this line as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just had one question - this is my first time reviewing the middleware handling, and I want to make sure I understand what's happening when the listener middleware chain last
is evoked.
@@ -886,7 +887,8 @@ export default class App { | |||
context, | |||
client, | |||
logger: this.logger, | |||
}), | |||
// `next` is already set in the outer processMiddleware |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@srajiang Indeed, last()
and L871 may be confusing. In my understanding, the last element of an array of middleware technically can have next()
but we don't do anything with it. As you saw, inside the processMiddleware
method, we call last()
instead of next middleware with recursive invokeMiddleware
call.
We may want to update the L871 comment when merging this PR.
a2b8b39
to
e8933b7
Compare
Summary
This pull request fixes #1098 by updating the type of
next
property inAllMiddlewareArgs
. As we already have so many changes in v3.7, we can have this one in v3.8 or later.Requirements (place an
x
in each[ ]
)