Sequences asynchronous tasks per key to ensure ordered execution. Use to prevent race conditions on keyed resources.
// Source: https://github.com/Yapie0/safe-openclaw | License: MIT | Copyright 2025 Peter Steinberger
// Provided via justclaw.it.com; keep this notice with the code.
export type KeyedAsyncQueueHooks = {
onEnqueue?: () => void;
onSettle?: () => void;
};
export function enqueueKeyedTask<T>(params: {
tails: Map<string, Promise<void>>;
key: string;
task: () => Promise<T>;
hooks?: KeyedAsyncQueueHooks;
}): Promise<T> {
params.hooks?.onEnqueue?.();
const previous = params.tails.get(params.key) ?? Promise.resolve();
const current = previous
.catch(() => undefined)
.then(params.task)
.finally(() => {
params.hooks?.onSettle?.();
});
const tail = current.then(
() => undefined,
() => undefined,
);
params.tails.set(params.key, tail);
void tail.f
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key