Class AbstractAsyncBatchResolver<Input, Result, Key>

Source
Expand description

Batches multiple asynchronous calls within a single operation, caching results.

import { AbstractAsyncBatchResolver, type Task } from '../src/index.js';

interface User {
readonly id: number;
readonly name: string;
}

class UserLoader extends AbstractAsyncBatchResolver<number, User> {
private readonly _db: Record<number, User> = {
1: { id: 1, name: 'Alice' },
2: { id: 2, name: 'Bob' },
3: { id: 3, name: 'Charlie' },
};

protected override getKey(userId: number): number {
return userId;
}

protected override async onResolve(tasks: Map<number, Task<number, User>>): Promise<void> {
tasks.forEach((task) => {
console.log('Loading user', task.input);
const user = this._db[task.input];
if (user) task.resolve(user);
else task.reject(new Error('User not found'));
tasks.delete(this.getKey(task.input));
});
}
}

const loader = new UserLoader();

const events = [
{ userId: 1, action: 'view' },
{ userId: 2, action: 'edit' },
{ userId: 3, action: 'delete' },
{ userId: 4, action: 'create' },
{ userId: 1, action: 'logout' },
{ userId: 2, action: 'login' },
{ userId: 3, action: 'signup' },
];

await Promise.all(
events.map(async (event) => {
try {
const user = await loader.get(event.userId);
console.log(`User ${user.name} performed ${event.action}`);
} catch (error) {
console.log(`User with ID ${event.userId} not found`);
}
}),
);

Constructors§

Source§

new AbstractAsyncBatchResolver<Input, Result, Key = Input>(
    deduplication?: DeduplicationStrategy,
): AbstractAsyncBatchResolver<Input, Result, Key>

Accessors§

Source§

get size(): number

Methods§

Source§

clear(): void

Clears all pending and cached tasks, rejecting any that are still unresolved.

Source§

delete(input: Input): boolean

Deletes a pending or cached task, rejecting it if still unresolved.

Source§

get(input: Input, deduplication?: DeduplicationStrategy, signal?: AbortSignal): PromiseLike<Result>

Requests a result for a given input, batching the request and utilizing an internal cache.

Source§

getKey(input: Input): Key

Extracts a unique key from an input item for deduplication.

Source§

has(input: Input): boolean

Checks if a task for the given input is pending or cached.

Source§

onResolve(tasks: Map<Key, Task<Input, Result>>): Promise<void>

The core logic for processing a batch of tasks. This method must resolve or reject every task in the provided map.