@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
73 lines • 1.47 kB
JavaScript
// src/notifyManager.ts
import { scheduleMicrotask } from "./utils.js";
function createNotifyManager() {
let queue = [];
let transactions = 0;
let notifyFn = (callback) => {
callback();
};
let batchNotifyFn = (callback) => {
callback();
};
const batch = (callback) => {
let result;
transactions++;
try {
result = callback();
} finally {
transactions--;
if (!transactions) {
flush();
}
}
return result;
};
const schedule = (callback) => {
if (transactions) {
queue.push(callback);
} else {
scheduleMicrotask(() => {
notifyFn(callback);
});
}
};
const batchCalls = (callback) => {
return (...args) => {
schedule(() => {
callback(...args);
});
};
};
const flush = () => {
const originalQueue = queue;
queue = [];
if (originalQueue.length) {
scheduleMicrotask(() => {
batchNotifyFn(() => {
originalQueue.forEach((callback) => {
notifyFn(callback);
});
});
});
}
};
const setNotifyFunction = (fn) => {
notifyFn = fn;
};
const setBatchNotifyFunction = (fn) => {
batchNotifyFn = fn;
};
return {
batch,
batchCalls,
schedule,
setNotifyFunction,
setBatchNotifyFunction
};
}
var notifyManager = createNotifyManager();
export {
createNotifyManager,
notifyManager
};
//# sourceMappingURL=notifyManager.js.map