react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
104 lines (83 loc) • 1.98 kB
JavaScript
;
exports.__esModule = true;
exports.createNotifyManager = createNotifyManager;
exports.notifyManager = void 0;
var _utils = require("./utils");
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 {
(0, _utils.scheduleMicrotask)(() => {
notifyFn(callback);
});
}
};
/**
* All calls to the wrapped function will be batched.
*/
const batchCalls = callback => {
return (...args) => {
schedule(() => {
callback(...args);
});
};
};
const flush = () => {
const originalQueue = queue;
queue = [];
if (originalQueue.length) {
(0, _utils.scheduleMicrotask)(() => {
batchNotifyFn(() => {
originalQueue.forEach(callback => {
notifyFn(callback);
});
});
});
}
};
/**
* Use this method to set a custom notify function.
* This can be used to for example wrap notifications with `React.act` while running tests.
*/
const setNotifyFunction = fn => {
notifyFn = fn;
};
/**
* Use this method to set a custom function to batch notifications together into a single tick.
* By default React Query will use the batch function provided by ReactDOM or React Native.
*/
const setBatchNotifyFunction = fn => {
batchNotifyFn = fn;
};
return {
batch,
batchCalls,
schedule,
setNotifyFunction,
setBatchNotifyFunction
};
} // SINGLETON
const notifyManager = createNotifyManager();
exports.notifyManager = notifyManager;