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

NikolayMakhonin / time-limits / #12

pending completion
#12

push

NikolayMakhonin
v0.0.11

226 of 295 branches covered (76.61%)

Branch coverage included in aggregate %.

7 of 7 new or added lines in 3 files covered. (100.0%)

487 of 571 relevant lines covered (85.29%)

107306.55 hits per line

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

88.98
/src/pool/Pool.ts
1
import {IAbortSignalFast} from '@flemist/abort-controller-fast'
2
import {CustomPromise, promiseToAbortable} from '@flemist/async-utils'
1✔
3
import {
1✔
4
  Priority,
5
  IPriorityQueue,
6
  PriorityQueue,
7
  AwaitPriority,
2✔
8
  awaitPriorityDefault,
9
} from '@flemist/priority-queue'
23,087✔
10

23,087✔
11
export interface IPool {
23,087✔
12
  readonly size: number
23,087!
13
  readonly maxSize: number
×
14

15
  holdAvailable: number
23,087✔
16

23,087✔
17
  hold(count: number): boolean
23,087✔
18

19
  /** it returns false if the obj cannot be pushed into the object pool (if size >= maxSize) */
2✔
20
  releaseAvailable: number
21

1,131,797✔
22
  release(count: number): Promise<number> | number
23

24
  /** it will resolve when size > 0 */
25
  tick(abortSignal?: IAbortSignalFast): Promise<void> | void
26

2✔
27
  /** wait size > 0 and hold, use this for concurrency hold */
28
  holdWait(
1,365,578✔
29
    count: number,
30
    priority?: Priority,
31
    abortSignal?: IAbortSignalFast,
32
    awaitPriority?: AwaitPriority,
33
  ): Promise<void>
2✔
34
}
35

×
36
// export interface IPoolSync extends IPool {
37
//   release(count: number): number
38
// }
39

40
export class Pool implements IPool {
2✔
41
  private readonly _priorityQueue: IPriorityQueue
202,408✔
42
  private readonly _maxSize: number = 0
202,408!
43
  private _size: number = 0
31,022✔
44

45
  constructor(maxSize: number) {
202,408✔
46
    if (!maxSize) {
202,408!
47
      throw new Error('maxSize should be > 0')
×
48
    }
2✔
49
    this._maxSize = maxSize
31,022✔
50
    this._size = maxSize
31,022✔
51
    this._priorityQueue = new PriorityQueue()
31,022✔
52
  }
53

54
  get maxSize() {
55
    return this._maxSize
1,373,737✔
56
  }
206,687✔
57

206,687✔
58
  get size() {
206,687✔
59
    return this._size
1,679,708✔
60
  }
61

206,687✔
62
  get holdAvailable() {
203,528✔
63
    return this._size
203,528✔
64
  }
69,017✔
65

69,017✔
66
  hold(count: number): boolean {
69,017✔
67
    const size = this._size
282,217✔
68
    if (count > size) {
282,217!
69
      return false
206,687✔
70
    }
71
    this._size = size - count
282,217✔
72
    return true
282,217✔
73
  }
167,882✔
74

75
  get releaseAvailable() {
73,972✔
76
    return this.maxSize - this._size
52,415✔
77
  }
78

73,972✔
79
  release(count: number): number {
80
    const size = this._size
285,328✔
81
    const maxReleaseCount = this.maxSize - size
285,328✔
82
    if (count > maxReleaseCount) {
285,328!
83
      count = maxReleaseCount
3,187✔
84
    }
85
    if (count > 0) {
285,328✔
86
      this._size = size + count
282,141✔
87

88
      if (this._tickPromise) {
282,141✔
89
        const tickPromise = this._tickPromise
110,021✔
90
        this._tickPromise = null
110,021✔
91
        tickPromise.resolve()
110,021✔
92
      }
2,912✔
93
    }
416✔
94

95
    return count
285,328✔
96
  }
416✔
97

98
  private _tickPromise: CustomPromise = new CustomPromise()
31,022✔
99
  tick(abortSignal?: IAbortSignalFast): Promise<void> | void {
416✔
100
    if (this._size > 0) {
312,325✔
101
      return
193,579!
102
    }
×
103
    if (!this._tickPromise) {
118,746✔
104
      this._tickPromise = new CustomPromise()
85,522✔
105
    }
106
    return promiseToAbortable(abortSignal, this._tickPromise.promise)
118,746✔
107
  }
108

109
  holdWait(
2✔
110
    count: number,
111
    priority?: Priority,
112
    abortSignal?: IAbortSignalFast,
113
    awaitPriority?: AwaitPriority,
114
  ) {
115
    if (count > this.maxSize) {
1,248!
116
      throw new Error(`holdCount (${count} > maxSize (${this.maxSize}))`)
×
117
    }
118

119
    if (!awaitPriority) {
1,248✔
120
      awaitPriority = awaitPriorityDefault
624✔
121
    }
122

123
    return this._priorityQueue.run(async (abortSignal) => {
1,248✔
124
      while (count > this._size) {
1,248✔
125
        await this.tick(abortSignal)
208✔
126
        await awaitPriority(priority, abortSignal)
208✔
127
      }
128
      if (!this.hold(count)) {
1,248!
129
        throw new Error('Unexpected behavior')
×
130
      }
131
    }, priority, abortSignal)
132
  }
133
}
134

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