UNPKG

vue-use-query

Version:

vue use query

84 lines (83 loc) 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.notifyManager = void 0; var utils_1 = require("./utils"); // CLASS var NotifyManager = /** @class */ (function () { function NotifyManager() { this.queue = []; this.transactions = 0; this.notifyFn = function (callback) { callback(); }; this.batchNotifyFn = function (callback) { callback(); }; } NotifyManager.prototype.batch = function (callback) { this.transactions++; var result = callback(); this.transactions--; if (!this.transactions) { this.flush(); } return result; }; NotifyManager.prototype.schedule = function (callback) { var _this = this; if (this.transactions) { this.queue.push(callback); } else { utils_1.scheduleMicrotask(function () { _this.notifyFn(callback); }); } }; /** * All calls to the wrapped function will be batched. */ NotifyManager.prototype.batchCalls = function (callback) { var _this = this; return (function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } _this.schedule(function () { callback.apply(void 0, args); }); }); }; NotifyManager.prototype.flush = function () { var _this = this; var queue = this.queue; this.queue = []; if (queue.length) { utils_1.scheduleMicrotask(function () { _this.batchNotifyFn(function () { queue.forEach(function (callback) { _this.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. */ NotifyManager.prototype.setNotifyFunction = function (fn) { this.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. */ NotifyManager.prototype.setBatchNotifyFunction = function (fn) { this.batchNotifyFn = fn; }; return NotifyManager; }()); // SINGLETON exports.notifyManager = new NotifyManager();