rvx
Version:
A signal based rendering library
31 lines • 980 B
JavaScript
import { captureSelf, teardown } from "../core/lifecycle.js";
import { watch } from "../core/signals.js";
export class WatchForTimeoutError extends Error {
}
export function watchFor(expr, condition, timeout) {
if (typeof condition === "number") {
timeout = condition;
condition = Boolean;
}
else if (condition === undefined) {
condition = Boolean;
}
return new Promise((resolve, reject) => {
captureSelf(dispose => {
watch(expr, value => {
if (condition(value)) {
dispose();
resolve(value);
}
});
if (timeout !== undefined) {
const handle = setTimeout(() => {
dispose();
reject(new WatchForTimeoutError());
}, timeout);
teardown(() => clearTimeout(handle));
}
});
});
}
//# sourceMappingURL=watch-for.js.map