Skip to content
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 delegate default serialization with many args #165

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion package-lock.json
masoud-msk marked this conversation as resolved.
Show resolved Hide resolved

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));
masoud-msk marked this conversation as resolved.
Show resolved Hide resolved

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