From f421e882e4fb01ad691bf94209aab486c22cd50b Mon Sep 17 00:00:00 2001 From: nharshunova Date: Thu, 25 Jan 2024 15:40:27 +0100 Subject: [PATCH 1/2] fix(microtask): fix argument list on next fn call --- src/modules/esl-utils/async/microtask.ts | 3 ++- src/modules/esl-utils/async/test/microtask.test.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/modules/esl-utils/async/microtask.ts b/src/modules/esl-utils/async/microtask.ts index 440529814..1f502e2c2 100644 --- a/src/modules/esl-utils/async/microtask.ts +++ b/src/modules/esl-utils/async/microtask.ts @@ -3,13 +3,14 @@ * (as a microtask produced with Promise) */ export function microtask(fn: (...arg: [T?]) => void, thisArg?: object): (arg?: T) => void { - const args: T[] = []; + let args: T[] = []; return function microtaskFn(arg: T): void { args.push(arg); if ((microtaskFn as any).request) return; (microtaskFn as any).request = Promise.resolve().then(() => { delete (microtaskFn as any).request; fn.call(thisArg || this, args); + args = []; }); }; } diff --git a/src/modules/esl-utils/async/test/microtask.test.ts b/src/modules/esl-utils/async/test/microtask.test.ts index 52ba082b1..f129d2116 100644 --- a/src/modules/esl-utils/async/test/microtask.test.ts +++ b/src/modules/esl-utils/async/test/microtask.test.ts @@ -14,13 +14,18 @@ describe('sync/microtask', () => { await Promise.resolve(); expect(fn).toBeCalledTimes(1); }); - test('Decorated as microtask callback receives a list of call arguments', async () => { + test('Decorated as microtask callback receives a correct list of call arguments', async () => { const fn = jest.fn(); const decorated = microtask(fn); - const params = [Symbol('Arg 1'), Symbol('Arg 2'), Symbol('Arg 3')]; + const params1 = [Symbol('Arg 1'), Symbol('Arg 2'), Symbol('Arg 3')]; - for (const param of params) decorated(param); + for (const param of params1) decorated(param); await Promise.resolve(); - expect(fn).toBeCalledWith(expect.arrayContaining(params)); + expect(fn).toBeCalledWith(params1); + + const params2 = [Symbol('Arg 4'), Symbol('Arg 5')]; + for (const param of params2) decorated(param); + await Promise.resolve(); + expect(fn).lastCalledWith(params2); }); }); From c3af68b2c4a9cd20c341516b13dcf1d13d7b1adc Mon Sep 17 00:00:00 2001 From: nharshunova Date: Fri, 26 Jan 2024 11:26:33 +0100 Subject: [PATCH 2/2] refactor(microtask): split test cases --- .../esl-utils/async/test/microtask.test.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/modules/esl-utils/async/test/microtask.test.ts b/src/modules/esl-utils/async/test/microtask.test.ts index f129d2116..18dbca4f1 100644 --- a/src/modules/esl-utils/async/test/microtask.test.ts +++ b/src/modules/esl-utils/async/test/microtask.test.ts @@ -14,16 +14,23 @@ describe('sync/microtask', () => { await Promise.resolve(); expect(fn).toBeCalledTimes(1); }); - test('Decorated as microtask callback receives a correct list of call arguments', async () => { + test('Decorated as microtask callback receives a list of call arguments', async () => { const fn = jest.fn(); const decorated = microtask(fn); - const params1 = [Symbol('Arg 1'), Symbol('Arg 2'), Symbol('Arg 3')]; + const params = [Symbol('Arg 1'), Symbol('Arg 2'), Symbol('Arg 3')]; + for (const param of params) decorated(param); + await Promise.resolve(); + expect(fn).toBeCalledWith(expect.arrayContaining(params)); + }); + test('Decorated as microtask callback refreshes after decorated method call (leak protected)', async () => { + const fn = jest.fn(); + const decorated = microtask(fn); + const params1 = [Symbol('Arg 1'), Symbol('Arg 2')]; for (const param of params1) decorated(param); await Promise.resolve(); - expect(fn).toBeCalledWith(params1); - const params2 = [Symbol('Arg 4'), Symbol('Arg 5')]; + const params2 = [Symbol('Arg 3'), Symbol('Arg 4')]; for (const param of params2) decorated(param); await Promise.resolve(); expect(fn).lastCalledWith(params2);