Skip to content

Commit

Permalink
fix(delegate): Fix default serialization with many args
Browse files Browse the repository at this point in the history
  • Loading branch information
masoud-msk committed Aug 4, 2024
1 parent ad980ac commit c91481f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/delegate/delegate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ describe('delegate', () => {
expect(counter).toEqual(2);
});

it('should delegate method with same key invocation - default key serialization - many args', async () => {
let counter = 0;

class T {
@delegate()
async foo(...args: number[]): Promise<number> {
counter += 1;
await sleep(20);

return Promise.resolve(args.reduce((a, b) => a + b));
}
}

const t = new T();

const res = await Promise.all([
t.foo(1, 1, 1, 1),
t.foo(1, 1, 1, 2),
t.foo(1, 1, 1, 1),
]);
expect(res).toEqual([4, 5, 4]);
expect(counter).toEqual(2);
});

it('should delegate method with same key invocation - custom serialization', async () => {
let counter = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/delegate/delegatify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function delegatify<D = any, A extends any[] = any[]>(
keyResolver?: (...args: A) => string,
): AsyncMethod<D, A> {
const delegatedKeysMap = new Map<string, Promise<any>>();
const keyGenerator: (...args: any[]) => string = keyResolver ?? JSON.stringify;
const keyGenerator: (...args: any[]) => string = keyResolver ?? ((...args) => JSON.stringify(args));

return function (...args: A): Promise<D> {
const key = keyGenerator(...args);
Expand Down

0 comments on commit c91481f

Please sign in to comment.