From bcfa376d12b75b2199e5b3b3b8adbada75ed7635 Mon Sep 17 00:00:00 2001 From: Robin Baum Date: Tue, 28 Nov 2023 15:46:43 +0100 Subject: [PATCH 1/2] Adds middleware for setting callbackWaitsForEmptyEventLoop false --- src/doNotWaitForEmptyEventLoop/index.spec.ts | 21 ++++++++++++++++++++ src/doNotWaitForEmptyEventLoop/index.ts | 10 ++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/doNotWaitForEmptyEventLoop/index.spec.ts create mode 100644 src/doNotWaitForEmptyEventLoop/index.ts diff --git a/src/doNotWaitForEmptyEventLoop/index.spec.ts b/src/doNotWaitForEmptyEventLoop/index.spec.ts new file mode 100644 index 0000000..08e8b9e --- /dev/null +++ b/src/doNotWaitForEmptyEventLoop/index.spec.ts @@ -0,0 +1,21 @@ +import { compose, types } from '../core'; +import { doNotWaitForEmptyEventLoop } from './index'; +import { inject } from '../inject'; + +let handler; + +beforeEach(() => { + handler = compose( + types<{}, any>(), + inject({}), + doNotWaitForEmptyEventLoop(), + )(async () => {}); +}); + +it('should set callbackWaitsForEmptyEventLoop to false', async () => { + const context = {}; + await handler({}, context); + expect(context).toEqual({ + callbackWaitsForEmptyEventLoop: false + }); +}); diff --git a/src/doNotWaitForEmptyEventLoop/index.ts b/src/doNotWaitForEmptyEventLoop/index.ts new file mode 100644 index 0000000..b97c6c9 --- /dev/null +++ b/src/doNotWaitForEmptyEventLoop/index.ts @@ -0,0 +1,10 @@ +import { Context } from 'aws-lambda'; +import { Middleware } from '../core'; + +export const doNotWaitForEmptyEventLoop = + (): Middleware => + (next) => + (event, context: Context, ...args) => { + context.callbackWaitsForEmptyEventLoop = false; + return next(event, context, ...args); + }; From eff29858ca5b95288d5d19f0ef9e9ed80f214eab Mon Sep 17 00:00:00 2001 From: Robin Baum Date: Tue, 28 Nov 2023 15:47:43 +0100 Subject: [PATCH 2/2] Prettifies code --- src/doNotWaitForEmptyEventLoop/index.spec.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/doNotWaitForEmptyEventLoop/index.spec.ts b/src/doNotWaitForEmptyEventLoop/index.spec.ts index 08e8b9e..8f14b5b 100644 --- a/src/doNotWaitForEmptyEventLoop/index.spec.ts +++ b/src/doNotWaitForEmptyEventLoop/index.spec.ts @@ -5,17 +5,13 @@ import { inject } from '../inject'; let handler; beforeEach(() => { - handler = compose( - types<{}, any>(), - inject({}), - doNotWaitForEmptyEventLoop(), - )(async () => {}); + handler = compose(types<{}, any>(), inject({}), doNotWaitForEmptyEventLoop())(async () => {}); }); it('should set callbackWaitsForEmptyEventLoop to false', async () => { const context = {}; await handler({}, context); expect(context).toEqual({ - callbackWaitsForEmptyEventLoop: false + callbackWaitsForEmptyEventLoop: false, }); });