Skip to content

Commit

Permalink
Change names back
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Sep 27, 2022
1 parent b29e1a0 commit b096c8e
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/polling/src/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Poll<T = any, U = any, V extends string = 'standby'>
this.name = options.name || Private.DEFAULT_NAME;

if ('auto' in options ? options.auto : true) {
setTimeout(() => void this.start(), 0);
setTimeout(() => this.start());
}
}

Expand Down Expand Up @@ -211,12 +211,9 @@ export class Poll<T = any, U = any, V extends string = 'standby'>
return;
}

// Clear the schedule if possible.
clearTimeout(this._scheduled);

// Update poll state.
const pending = this._tick;
const tick = new PromiseDelegate<this>();
const scheduled = new PromiseDelegate<this>();
const state = {
interval: this.frequency.interval,
payload: null,
Expand All @@ -225,27 +222,32 @@ export class Poll<T = any, U = any, V extends string = 'standby'>
...next
} as IPoll.State<T, U, V>;
this._state = state;
this._tick = tick;
this._tick = scheduled;

// Clear the schedule if possible.
clearTimeout(this._timeout);

// Emit ticked signal, resolve pending promise, and await its settlement.
this._ticked.emit(this.state);
pending.resolve(this);
await pending.promise;

if (state.interval === Poll.NEVER) {
this._timeout = undefined;
return;
}

// Schedule next execution and cache its timeout handle.
const execute = () => {
if (this.isDisposed || this.tick !== tick.promise) {
if (this.isDisposed || this.tick !== scheduled.promise) {
return;
}

this._execute();
};

// Cache the handle in case it needs to be unscheduled.
this._scheduled =
state.interval === Poll.NEVER
? undefined
: setTimeout(execute, state.interval);
this._timeout = setTimeout(execute, state.interval);
}

/**
Expand Down Expand Up @@ -341,11 +343,11 @@ export class Poll<T = any, U = any, V extends string = 'standby'>
private _frequency: IPoll.Frequency;
private _linger: number;
private _lingered = 0;
private _scheduled: ReturnType<typeof setTimeout> | undefined;
private _standby: Poll.Standby | (() => boolean | Poll.Standby);
private _state: IPoll.State<T, U, V>;
private _tick = new PromiseDelegate<this>();
private _ticked = new Signal<this, IPoll.State<T, U, V>>(this);
private _timeout: ReturnType<typeof setTimeout> | undefined;
}

/**
Expand Down

0 comments on commit b096c8e

Please sign in to comment.