@camunda8/sdk
Version:
[](https://www.npmjs.com/package/@camunda8/sdk)
50 lines • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PollingOperation = PollingOperation;
function defaultPredicate(result) {
return (result !== null &&
result !== undefined &&
result.items &&
result.items.length > 0);
}
class PredicateError extends Error {
constructor(message) {
super(message);
this.name = 'PredicateError';
// Ensure the prototype chain is correctly set up
Object.setPrototypeOf(this, PredicateError.prototype);
this.result = null;
}
}
function PollingOperation(options) {
const interval = options.interval || 1000;
const timeout = options.timeout || 30000;
const operation = options.operation;
// Use default predicate if no predicate provided, otherwise use provided predicate
const predicate = options.predicate || defaultPredicate;
return new Promise((resolve, reject) => {
const startTime = Date.now();
const poll = async () => {
try {
const result = await operation();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (!predicate(result)) {
const error = new PredicateError('Predicate did not match');
error.result = result;
throw error;
}
resolve(result);
}
catch (error) {
if (Date.now() - startTime < timeout) {
setTimeout(poll, interval);
}
else {
reject(error);
}
}
};
poll();
});
}
//# sourceMappingURL=PollingOperation.js.map