@unilogin/sdk
Version:
SDK is a JS library, that communicates with relayer. SDK allows managing contract, by creating basic contract-calling messages.
49 lines (40 loc) • 1.01 kB
text/typescript
import {sleep} from '@unilogin/commons';
type ObserverRunnerState = 'stop' | 'stopping' | 'running';
abstract class ObserverRunner {
protected state: ObserverRunnerState = 'stop';
abstract async execute(): Promise<void>;
tick = 1000;
timeout: any = null;
async loop() {
if (this.state === 'stop') {
return;
}
await this.execute();
if (this.state === 'stopping') {
this.state = 'stop';
} else if (this.state === 'running') {
this.timeout = setTimeout(() => this.loop(), this.tick);
}
}
start() {
if (this.state === 'stop') {
this.state = 'running';
this.loop()
.catch(console.error);
}
}
stop() {
this.state = 'stop';
clearTimeout(this.timeout);
}
async finalizeAndStop() {
this.state = this.isStopped() ? 'stop' : 'stopping';
do {
await sleep(this.tick);
} while (!this.isStopped());
}
protected isStopped() {
return this.state === 'stop';
}
}
export default ObserverRunner;