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

Add batchInterval option to dataLoader #5474

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/client/src/internals/dataLoader.ts
Expand Up @@ -39,6 +39,7 @@ const throwFatalError = () => {
*/
export function dataLoader<TKey, TValue>(
batchLoader: BatchLoader<TKey, TValue>,
batchInterval = 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an options object instead

Suggested change
batchInterval = 0,
opts: {
/**
* .......... docs
*/
batchInterval?: number;
} = {},

) {
let pendingItems: BatchItem<TKey, TValue>[] | null = null;
let dispatchTimer: ReturnType<typeof setTimeout> | null = null;
Expand Down Expand Up @@ -159,7 +160,7 @@ export function dataLoader<TKey, TValue>(
});

if (!dispatchTimer) {
dispatchTimer = setTimeout(dispatch);
dispatchTimer = setTimeout(dispatch, batchInterval);
}
const cancel = () => {
item.aborted = true;
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/links/HTTPBatchLinkOptions.ts
Expand Up @@ -6,6 +6,7 @@ import type { HTTPHeaders, Operation } from './types';
export type HTTPBatchLinkOptions<TRoot extends AnyClientTypes> =
HTTPLinkBaseOptions<TRoot> & {
maxURLLength?: number;
batchInterval?: number;
Comment on lines 7 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be

HTTPLinkBaseOptions<TRoot> & DataLoaderOptions & { maxURLLength?: '..' }

that way, it inherits whatever docs and options we add on dataloader

/**
* Headers to be set on outgoing requests or a callback that of said headers
* @link http://trpc.io/docs/client/headers
Expand Down
Expand Up @@ -78,7 +78,10 @@ export function createHTTPBatchLink(
return { validate, fetch };
};

const query = dataLoader<Operation, HTTPResult>(batchLoader('query'));
const query = dataLoader<Operation, HTTPResult>(
batchLoader('query'),
opts.batchInterval,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
opts.batchInterval,
opts

);
const mutation = dataLoader<Operation, HTTPResult>(
batchLoader('mutation'),
);
Expand Down