• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / loaders.gl / 24153816851

08 Apr 2026 07:17PM UTC coverage: 53.247% (-12.1%) from 65.319%
24153816851

push

github

web-flow
chore: Move from tape to vitest (#3351)

8651 of 17291 branches covered (50.03%)

Branch coverage included in aggregate %.

7 of 7 new or added lines in 1 file covered. (100.0%)

2031 existing lines in 296 files now uncovered.

17563 of 31940 relevant lines covered (54.99%)

5279.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.26
/modules/worker-utils/src/lib/async-queue/async-queue.ts
1
// loaders.gl
2
// SPDX-License-Identifier: MIT
3
// Copyright (c) vis.gl contributors
4

5
// From https://github.com/rauschma/async-iter-demo/tree/master/src under MIT license
6
// http://2ality.com/2016/10/asynchronous-iteration.html
7

8
/**
9
 * Async Queue
10
 * - AsyncIterable: An async iterator can be
11
 * - Values can be pushed onto the queue
12
 * @example
13
 *   const asyncQueue = new AsyncQueue();
14
 *   setTimeout(() => asyncQueue.enqueue('tick'), 1000);
15
 *   setTimeout(() => asyncQueue.enqueue(new Error('done')), 10000);
16
 *   for await (const value of asyncQueue) {
17
 *     console.log(value); // tick
18
 *   }
19
 */
20
export default class AsyncQueue<T> {
21
  private _values: any[];
22
  private _settlers: any[];
23
  private _closed: boolean;
24

25
  constructor() {
26
    this._values = []; // enqueues > dequeues
2✔
27
    this._settlers = []; // dequeues > enqueues
2✔
28
    this._closed = false;
2✔
29
  }
30

31
  /** Return an async iterator for this queue */
32
  [Symbol.asyncIterator](): AsyncIterator<T> {
33
    return this;
2✔
34
  }
35

36
  /** Push a new value - the async iterator will yield a promise resolved to this value */
37
  push(value: T): void {
38
    return this.enqueue(value);
2✔
39
  }
40

41
  /**
42
   * Push a new value - the async iterator will yield a promise resolved to this value
43
   * Add an error - the async iterator will yield a promise rejected with this value
44
   */
45
  enqueue(value: T | Error): void {
46
    if (this._closed) {
4!
47
      throw new Error('Closed');
×
48
    }
49

50
    if (this._settlers.length > 0) {
4✔
51
      if (this._values.length > 0) {
2!
UNCOV
52
        throw new Error('Illegal internal state');
×
53
      }
54
      const settler = this._settlers.shift();
2✔
55
      if (value instanceof Error) {
2!
UNCOV
56
        settler.reject(value);
×
57
      } else {
58
        settler.resolve({value});
2✔
59
      }
60
    } else {
61
      this._values.push(value);
2✔
62
    }
63
  }
64

65
  /** Indicate that we not waiting for more values - The async iterator will be done */
66
  close(): void {
67
    while (this._settlers.length > 0) {
1✔
68
      const settler = this._settlers.shift();
×
69
      settler.resolve({done: true});
×
70
    }
71
    this._closed = true;
1✔
72
  }
73

74
  // ITERATOR IMPLEMENTATION
75

76
  /** @returns a Promise for an IteratorResult */
77
  next(): Promise<IteratorResult<T, any>> {
78
    // If values in queue, yield the first value
79
    if (this._values.length > 0) {
5✔
80
      const value = this._values.shift();
2✔
81
      if (value instanceof Error) {
2✔
82
        return Promise.reject(value);
1✔
83
      }
84
      return Promise.resolve({done: false, value});
1✔
85
    }
86

87
    // If queue is closed, the iterator is done
88
    if (this._closed) {
3✔
89
      if (this._settlers.length > 0) {
1!
90
        throw new Error('Illegal internal state');
×
91
      }
92
      return Promise.resolve({done: true, value: undefined});
1✔
93
    }
94

95
    // Yield a promise that waits for new values to be enqueued
96
    return new Promise((resolve, reject) => {
2✔
97
      this._settlers.push({resolve, reject});
2✔
98
    });
99
  }
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc