@jivanf/vest
Version:
Declarative Form Validations Framework
47 lines (44 loc) • 1.57 kB
JavaScript
import { registerReconciler } from 'vest';
import { isPromise } from 'vest-utils';
import { Isolate, IsolateSelectors } from 'vestjs-runtime';
const isolateType = 'Debounce';
function debounce(callback, delay = 0) {
let timeout = null;
const f = () => (payload) => new Promise((resolve, reject) => {
timeout = setTimeout(() => {
let res = false;
try {
res = callback(payload);
}
catch (e) {
return reject(e);
}
if (res === false) {
return reject();
}
return isPromise(res) ? res.then(resolve, reject) : resolve(res);
}, delay);
});
const i = Isolate.create(isolateType, f, {
clearTimeout: () => {
if (timeout) {
clearTimeout(timeout);
}
},
});
return i.output;
}
class IsolateDebounceReconciler {
static match(currentNode, historyNode) {
return (IsolateSelectors.isIsolateType(currentNode, isolateType) &&
IsolateSelectors.isIsolateType(historyNode, isolateType));
}
static reconcile(current, history) {
var _a, _b;
(_b = (_a = history === null || history === void 0 ? void 0 : history.data) === null || _a === void 0 ? void 0 : _a.clearTimeout) === null || _b === void 0 ? void 0 : _b.call(_a);
return current;
}
}
registerReconciler(IsolateDebounceReconciler);
export { IsolateDebounceReconciler, debounce as default };
//# sourceMappingURL=debounce.development.js.map