UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

36 lines 1.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.asyncDebounce = asyncDebounce; const debounce_js_1 = __importDefault(require("lodash/debounce.js")); /** * A version of lodash/debounce that always returns a promise but wait for the debounced function to return to resolve it. * @param func The function to debounce * @param wait The debounce delay * @returns A debounced function that returns a promise */ function asyncDebounce(func, wait) { const resolveSet = new Set(); const rejectSet = new Set(); const debounced = (0, debounce_js_1.default)((args) => { func(...args) .then((...res) => { resolveSet.forEach(resolve => resolve(...res)); }) .catch((...res) => { rejectSet.forEach(reject => reject(...res)); }) .finally(() => { resolveSet.clear(); rejectSet.clear(); }); }, wait); return (...args) => new Promise((resolve, reject) => { resolveSet.add(resolve); rejectSet.add(reject); debounced(args); }); } //# sourceMappingURL=asyncDebounce.js.map