Quellcode durchsuchen

Simplify schedule and fix tests.

Afshin T. Darian vor 6 Jahren
Ursprung
Commit
42e3f4d331
2 geänderte Dateien mit 6 neuen und 28 gelöschten Zeilen
  1. 3 25
      packages/coreutils/src/poll.ts
  2. 3 3
      tests/test-coreutils/src/poll.spec.ts

+ 3 - 25
packages/coreutils/src/poll.ts

@@ -183,7 +183,6 @@ export class Poll<T = any, U = any> implements IDisposable, IPoll<T, U> {
           return;
         }
 
-        this._locked = false;
         return this.schedule({
           interval: Private.IMMEDIATE,
           phase: 'when-resolved'
@@ -196,7 +195,6 @@ export class Poll<T = any, U = any> implements IDisposable, IPoll<T, U> {
 
         console.warn(`Poll (${this.name}) started despite rejection.`, reason);
 
-        this._locked = false;
         return this.schedule({
           interval: Private.IMMEDIATE,
           phase: 'when-rejected'
@@ -297,7 +295,6 @@ export class Poll<T = any, U = any> implements IDisposable, IPoll<T, U> {
       return;
     }
 
-    this._queue.length = 0;
     this._state = {
       ...Private.DISPOSED_STATE,
       timestamp: new Date().getTime()
@@ -370,31 +367,21 @@ export class Poll<T = any, U = any> implements IDisposable, IPoll<T, U> {
     }
 
     const pending = this._tick;
-    const queue = this._queue;
-    let last = this.state;
 
     // The `when` promise in the constructor options acts as a gate.
-    if (last.phase === 'constructed') {
+    if (this.state.phase === 'constructed') {
       if (next.phase !== 'when-rejected' && next.phase !== 'when-resolved') {
         await pending.promise;
       }
     }
 
-    // Update `last` after pending promise settles.
-    last = this.state;
-
-    // If scheduling is locked enqueue next state.
-    if (this._locked) {
-      queue.push(next);
-      return;
-    }
-
     // Check if the phase transition should be canceled.
-    if (next.cancel && next.cancel(last)) {
+    if (next.cancel && next.cancel(this.state)) {
       return;
     }
 
     // Update poll state.
+    const last = this.state;
     const scheduled = new PromiseDelegate<this>();
     const state: IPoll.State<T, U> = {
       interval: this.frequency.interval,
@@ -432,13 +419,6 @@ export class Poll<T = any, U = any> implements IDisposable, IPoll<T, U> {
         : state.interval === Private.NEVER
           ? -1
           : setTimeout(execute, state.interval);
-
-    this._locked = false;
-
-    // Unwind the queue if necessary.
-    if (queue.length) {
-      return this.schedule(queue.shift());
-    }
   }
 
   /**
@@ -489,8 +469,6 @@ export class Poll<T = any, U = any> implements IDisposable, IPoll<T, U> {
   private _disposed = new Signal<this, void>(this);
   private _factory: Poll.Factory<T, U>;
   private _frequency: IPoll.Frequency;
-  private _locked = true;
-  private _queue = new Array<Partial<IPoll.State>>();
   private _standby: Poll.Standby | (() => boolean | Poll.Standby);
   private _state: IPoll.State<T, U>;
   private _tick = new PromiseDelegate<this>();

+ 3 - 3
tests/test-coreutils/src/poll.spec.ts

@@ -229,16 +229,16 @@ describe('Poll', () => {
       });
       const ticker: IPoll.Phase[] = [];
       const tocker: IPoll.Phase[] = [];
-      poll.ticked.connect((_, state) => {
+      poll.ticked.connect(async (_, state) => {
         ticker.push(state.phase);
         expect(ticker.length).to.equal(tocker.length + 1);
       });
-      const tock = (poll: IPoll) => {
+      const tock = async (poll: IPoll) => {
         tocker.push(poll.state.phase);
         expect(ticker.join(' ')).to.equal(tocker.join(' '));
         poll.tick.then(tock).catch(() => undefined);
       };
-      void poll.tick.then(tock);
+      await poll.tick.then(tock);
       await poll.stop();
       await poll.start();
       await poll.tick;