Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,10 @@ describe('App', () => {
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});

app.use((_args) => { ackFn(); });
app.use((_args) => {
ackFn();
_args.next();
});
app.action('block_action_id', ({ }) => { actionFn(); });
app.action({ callback_id: 'message_action_callback_id' }, ({ }) => { actionFn(); });
app.action(
Expand Down Expand Up @@ -563,8 +566,9 @@ describe('App', () => {
receiver: fakeReceiver,
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});
app.use(({ logger, body }) => {
app.use(({ logger, body, next }) => {
logger.info(body);
next();
});

app.event('app_home_opened', ({ logger, event }) => {
Expand Down Expand Up @@ -628,8 +632,9 @@ describe('App', () => {
return Promise.resolve({ userToken: token, botId: 'B123' });
},
});
app.use(async ({ client }) => {
app.use(async ({ client, next }) => {
await client.auth.test();
next();
});
const clients: WebClient[] = [];
app.event('app_home_opened', async ({ client }) => {
Expand Down
44 changes: 44 additions & 0 deletions src/middleware/process.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// tslint:disable:no-implicit-dependencies
import { assert } from 'chai';
import { processMiddleware } from './process';
import { Context } from 'mocha';
import { AnyMiddlewareArgs, Middleware } from '../types';

describe('processMiddleware()', () => {
const middlewareOne: Middleware<AnyMiddlewareArgs> = ({ next, context }) => {
const fn = () => {
context.one = true;
next();
};
setTimeout(fn, 10);
};
const middlewareTwo: Middleware<AnyMiddlewareArgs> = ({ next, context }) => {
const fn = () => {
context.two = true;
next();
};
setTimeout(fn, 10);
};
it('processes all middleware before processing listener', (done) => {
processMiddleware(
// @ts-ignore
{},
[middlewareOne, middlewareTwo],
(context: Context) => {
// ensure that the last middleware ran to completion (i.e., called `next`) before afterMiddleware is called
assert.isTrue(context.one);
assert.isTrue(context.two);
done();
},
(error?: Error) => {
if (error) {
console.error(`after post process: ${error && error.message}`);
}
assert(false);
},
{},
null,
null,
);
});
});
5 changes: 1 addition & 4 deletions src/middleware/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ export function processMiddleware(
// Continue processing
if (thisMiddleware !== undefined && !(errorOrPostProcess instanceof Error)) {
const isLastMiddleware = middlewareIndex === (middleware.length - 1);
const nextWhenNotLast = isLastMiddleware ? noop : next;

// In this condition, errorOrPostProcess will be a postProcess function or undefined
postProcessFns[middlewareIndex - 1] = errorOrPostProcess === undefined ? noopPostProcess : errorOrPostProcess;
thisMiddleware({ context, logger, client, next: nextWhenNotLast, ...initialArguments });
thisMiddleware({ context, logger, client, next, ...initialArguments });

if (isLastMiddleware) {
postProcessFns[middlewareIndex] = noopPostProcess;
process.nextTick(next);
}
return;
}
Expand Down Expand Up @@ -82,5 +80,4 @@ export function processMiddleware(
firstMiddleware({ context, logger, client, next, ...initialArguments });
}

function noop(): void { } // tslint:disable-line:no-empty
const noopPostProcess: PostProcessFn = (error, done) => { done(error); };