export function createRevalidator() {
    const inflight = new Map();
    return {
        trigger(key, revalidate) {
            if (inflight.has(key))
                return;
            let resolveRevalidation;
            let rejectRevalidation;
            const promise = new Promise((resolve, reject) => {
                resolveRevalidation = resolve;
                rejectRevalidation = reject;
            })
                .catch(() => { })
                .finally(() => inflight.delete(key));
            inflight.set(key, promise);
            try {
                void revalidate().then(resolveRevalidation, rejectRevalidation);
            }
            catch (error) {
                rejectRevalidation(error);
            }
        },
        async waitForRevalidation(key) {
            if (key) {
                await inflight.get(key);
            }
            else {
                while (inflight.size > 0) {
                    await Promise.all(inflight.values());
                }
            }
        },
    };
}
