vue-use-query
Version:
vue use query
81 lines (80 loc) • 2.57 kB
JavaScript
import { scheduleMicrotask } from './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 {
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) {
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
export var notifyManager = new NotifyManager();