All files / packages/core/src/utilities ProgressiveIterator.ts

54.79% Statements 40/73
45.16% Branches 14/31
50% Functions 9/18
54.79% Lines 40/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                                          28x             26x     26x 26x   25x 25x         1x   26x         31x 31x 31x 29x 29x                           1x 1x                               28x 31x     31x   3x 3x 1x     30x 30x 30x 30x 30x       30x     27x         26x   26x 26x 25x   25x 25x 20x   5x       5x 5x             1x 1x                                                                                                                    
export class PromiseIterator<T> extends Promise<T> {
  iterator?: ProgressiveIterator<T>;
}
 
export type ErrorCallback = (message: string | Error) => void;
 
/**
 * A progressive iterator is an async iterator that can have data delivered
 * to it, with newer ones replacing older iterations which have not yet been
 * consume.  That allows iterating over sets of values and delivering updates,
 * but always getting the most recent instance.
 */
export default class ProgressiveIterator<T> {
  public done;
  public name?: string;
 
  private nextValue;
  private waiting;
  private rejectReason;
 
  constructor(name?) {
    this.name = name || 'unknown';
  }
 
  /** Casts a promise, progressive iterator or promise iterator to a
   * progressive iterator, creating one if needed to resolve it.
   */
  public static as(promise) {
    Iif (promise.iterator) {
      return promise.iterator;
    }
    const iterator = new ProgressiveIterator('as iterator');
    promise.then(
      (v) => {
        try {
          iterator.add(v, true);
        } catch (e) {
          iterator.reject(e as Error);
        }
      },
      (reason) => iterator.reject(reason)
    );
    return iterator;
  }
 
  /** Add a most recent result, indicating if the result is the final one */
  public add(x: T, done = false) {
    this.nextValue = x;
    this.done ||= done;
    if (this.waiting) {
      this.waiting.resolve(x);
      this.waiting = undefined;
    }
  }
 
  public resolve() {
    this.done = true;
    if (this.waiting) {
      this.waiting.resolve(this.nextValue);
      this.waiting = undefined;
    }
  }
 
  /** Reject the fetch.  This will prevent further iteration. */
  public reject(reason: Error): void {
    this.rejectReason = reason;
    this.waiting?.reject(reason);
  }
 
  /** Gets the most recent value, without waiting */
  public getRecent(): T {
    if (this.rejectReason) {
      throw this.rejectReason;
    }
    return this.nextValue;
  }
 
  /**
   * Async iteration where the delivered values are the most recently available
   * ones, so not necessarily all values are ever seen.
   */
  public async *[Symbol.asyncIterator]() {
    while (!this.done) {
      Iif (this.rejectReason) {
        throw this.rejectReason;
      }
      if (this.nextValue !== undefined) {
        // console.log('Yielding on', this.name, this.nextValue);
        yield this.nextValue;
        if (this.done) {
          break;
        }
      }
      Eif (!this.waiting) {
        this.waiting = {};
        this.waiting.promise = new Promise((resolve, reject) => {
          this.waiting.resolve = resolve;
          this.waiting.reject = reject;
        });
      }
      // console.log('Awaiting on', this.name);
      await this.waiting.promise;
    }
    // console.log('Final yield on', this.name);
    yield this.nextValue;
  }
 
  /** Runs the forEach method on this filter */
  public async forEach(callback, errorCallback) {
    let index = 0;
    // Need to catch basic iteration errors first
    try {
      for await (const value of this) {
        const { done } = this;
        // Separately catch errors in the callback function
        try {
          await callback(value, done, index);
          index++;
        } catch (e) {
          Iif (!done) {
            console.warn('Caught exception in intermediate value', e);
            continue;
          }
          if (errorCallback) {
            errorCallback(e, done);
          } else E{
            throw e;
          }
        }
      }
    } catch (e) {
      if (errorCallback) {
        errorCallback(e, true);
      } else E{
        throw e;
      }
    }
  }
 
  /** Calls an async function to generate the results on the iterator */
  public generate(
    processFunction,
    errorCallback?: ErrorCallback
  ): Promise<any> {
    return processFunction(this, this.reject.bind(this)).then(
      () => {
        if (!this.done) {
          // Set it to done
          this.resolve();
        }
      },
      (reason) => {
        this.reject(reason);
        if (errorCallback) {
          errorCallback(reason);
        } else {
          console.warn("Couldn't process because", reason);
        }
      }
    );
  }
 
  async nextPromise(): Promise<T> {
    for await (const i of this) {
      if (i) {
        return i;
      }
    }
    return this.nextValue;
  }
 
  async donePromise(): Promise<T> {
    for await (const i of this) {
      // No-op
    }
    return this.nextValue;
  }
 
  public getNextPromise() {
    const promise = this.nextPromise() as PromiseIterator<T>;
    promise.iterator = this;
    return promise;
  }
 
  public getDonePromise() {
    const promise = this.donePromise() as PromiseIterator<T>;
    promise.iterator = this;
    return promise;
  }
}