@camunda8/sdk
Version:
[](https://www.npmjs.com/package/@camunda8/sdk)
51 lines • 1.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Subscription = void 0;
const events_1 = __importDefault(require("events"));
class Subscription extends events_1.default {
constructor(restCall) {
super();
this._cancelled = false;
// Define a function generator that fetches data at regular intervals
this.dataGenerator = async function* (interval
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) {
while (true) {
try {
const data = await restCall();
if (data) {
// or 404
// If data is available, yield it
yield data;
// Break the loop if data is available
break;
}
}
catch (error) {
console.error('Error fetching data:', error);
}
// Wait for the specified interval before trying again
await new Promise((resolve) => setTimeout(resolve, interval));
}
};
this.start();
}
cancel() {
this._cancelled = true;
this.emit('cancelled');
}
get cancelled() {
return this._cancelled;
}
async start() {
const generator = this.dataGenerator(5000); // Fetch data every 5 seconds
for await (const data of generator) {
this.emit('data', data);
}
}
}
exports.Subscription = Subscription;
//# sourceMappingURL=Subscription.js.map