All files / packages/fsm/src queue.ts

97.4% Statements 188/193
95.87% Branches 93/97
97.5% Functions 39/40
97.39% Lines 187/192

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448                  7x 7x         201748x 201748x 201748x         201748x   201748x 201748x     8x               160488x       581x       223979x 7x     223972x 223972x       8x       8x       6x 2x     4x 4x 4x 4x       139849x 138x     139711x 139711x 139711x 139711x 139711x                   10x 1x     9x 9x       10x 2x     8x 8x 8x 8x 8x 8x       2x 2x 2x 2x 2x 2x 2x       190897x   363501x 352619x 128995x 5x     128990x     223624x                       718x   718x 718x       718x     2x 1x     716x 716x 13x   703x 2x 1x   1x       701x     201x 1x   200x                       703x 703x 703x   703x 502x 502x 501x   502x   703x   703x 703x   703x         2x       363501x 2x     363499x 6x 6x 2x       363497x 363497x 69967x     293530x 56428x     237102x 237102x         6x       6x 6x         139725x 7x 7x         363687x 5x     363682x 153847x 153847x       153847x 153847x         139715x 139540x         139550x 72574x 72574x                     139944x 139944x 139944x     150520x       150516x 150516x       371294x 220919x     150375x 150375x 150375x   150375x   150375x       6x 6x 6x       231041x       150375x 150375x   150375x 150374x     1x 1x 1x                     61804x 61804x     73459x       73456x           73456x 73456x 73456x       146766x 73314x     73452x 73452x   73452x 14x 14x     73452x       4x       135099x       73456x   73456x 15x 15x 15x   15x 11x     4x 4x 4x         14x 14x   14x 19x 19x 19x   19x 5x     19x 1x     19x 14x     5x 5x 5x 5x         26x 15x     11x      
type ShiftResolver<T> = (value: IteratorResult<T>) => void
// The void parameter (rather than none) lets take()'s generic gate racer park in
// #pendingResume and #pendingShift through one signature.
type ResumeResolver = (value: void) => void
 
type DequeueResult<T> = { hasValue: true; value: T } | { hasValue: false }
 
export class QueueClosedError extends Error {
	constructor(message = 'Queue closed') {
		super(message)
		this.name = 'QueueClosedError'
	}
}
 
export abstract class AbstractAsyncQueue<T, P> implements AsyncIterable<T>, Disposable {
	#paused = false
	#closed = false
	#destroyed = false
 
	// Set by fail() so the iterator throws this error once the buffer drains,
	// instead of ending silently. Lets a producer terminate the stream with a
	// reason without wrapping the iterator (which would tax every happy-path item).
	#failure: { error: unknown } | null = null
 
	#pendingShift: ShiftResolver<T>[] = []
	#pendingResume: ResumeResolver[] = []
 
	get size(): number {
		return this.getBufferSize()
	}
 
	get isPaused(): boolean {
		return this.#paused
	}
 
	get isClosed(): boolean {
		return this.#closed
	}
 
	get isDestroyed(): boolean {
		return this.#destroyed
	}
 
	protected pushInternal(value: T, options: P): void {
		if (this.#closed || this.#destroyed) {
			throw new QueueClosedError()
		}
 
		this.enqueue(value, options)
		this.#resolvePendingShift()
	}
 
	pause(): void {
		Iif (this.#paused || this.#destroyed) {
			return
		}
 
		this.#paused = true
	}
 
	resume(): void {
		if (!this.#paused || this.#destroyed) {
			return
		}
 
		this.#paused = false
		this.#resolvePendingResume()
		this.#resolvePendingShift()
		this.#resolvePendingDoneIfNeeded()
	}
 
	close(): void {
		if (this.#closed || this.#destroyed) {
			return
		}
 
		this.#closed = true
		this.#paused = false
		this.#resolvePendingResume()
		this.#resolvePendingShift()
		this.#resolvePendingDoneIfNeeded()
	}
 
	// Seal the queue like close(), but make the iterator throw `error` after the
	// buffered items drain. Buffered values are delivered first because they are
	// already-committed facts (e.g. server acks the consumer must not miss) —
	// dropping them would misreport what happened before the failure. The tail is
	// bounded: push() after fail() throws, so only items buffered at failure time
	// remain. For an immediate stop that discards the buffer, use destroy().
	fail(error: unknown): void {
		if (this.#closed || this.#destroyed) {
			return
		}
 
		this.#failure = { error }
		this.close()
	}
 
	destroy(): void {
		if (this.#destroyed) {
			return
		}
 
		this.#destroyed = true
		this.#closed = true
		this.#paused = false
		this.clearBuffer()
		this.#resolvePendingResume()
		this.#resolvePendingDone()
	}
 
	reset(): void {
		this.#paused = false
		this.#closed = false
		this.#destroyed = false
		this.#failure = null
		this.clearBuffer()
		this.#resolvePendingResume()
		this.#resolvePendingDone()
	}
 
	async *[Symbol.asyncIterator](): AsyncIterator<T> {
		while (true) {
			// oxlint-disable-next-line no-await-in-loop
			let next = await this.#next()
			if (next.done) {
				if (this.#failure) {
					throw this.#failure.error
				}
 
				return
			}
 
			yield next.value
		}
	}
 
	// One cancellable dequeue step with the iterator's exact contract — pause and the
	// drain-then-throw of fail() — plus abort: throws signal.reason if the signal fires
	// while waiting. Unlike racing iterator.next() with abortable() (a plain
	// Promise.race that leaves the underlying next() pending), losing the race removes
	// the parked waiter synchronously, so a cancelled take() can never be handed (and
	// thus swallow) an item. For a bounded wait compose the deadline at the call site:
	// linkSignals(signal, AbortSignal.timeout(ms)).
	async take(signal?: AbortSignal): Promise<IteratorResult<T>> {
		signal?.throwIfAborted()
 
		for (;;) {
			Iif (this.#destroyed) {
				return { value: undefined as never, done: true }
			}
 
			if (this.#paused) {
				// Wait for resume(), then re-evaluate from the top.
				// oxlint-disable-next-line no-await-in-loop
				await this.#parkCancellable(this.#pendingResume, signal)
				continue
			}
 
			let item = this.dequeue()
			if (item.hasValue) {
				return { value: item.value, done: false }
			}
			if (this.#closed) {
				if (this.#failure) {
					throw this.#failure.error
				}
				return { value: undefined as never, done: true }
			}
 
			// oxlint-disable-next-line no-await-in-loop
			let result = await this.#parkCancellable(this.#pendingShift, signal)
			// close()/destroy() settles parked waiters with done — mirror the
			// iterator's drain-then-throw before reporting the end.
			if (result.done && this.#failure) {
				throw this.#failure.error
			}
			return result
		}
	}
 
	// Park a resolver in `gate` and race it against the caller's abort. On abort the
	// waiter is spliced out BEFORE the reject, so `indexOf !== -1` proves it was not
	// and will never be served — unparking is atomic, no item can be handed to a
	// cancelled take().
	async #parkCancellable<V>(
		gate: Array<(value: V) => void>,
		signal: AbortSignal | undefined
	): Promise<V> {
		let parked = Promise.withResolvers<V>()
		let waiter = (value: V) => parked.resolve(value)
		gate.push(waiter)
 
		let onAbort = () => {
			let index = gate.indexOf(waiter)
			if (index !== -1) {
				gate.splice(index, 1)
			}
			parked.reject(signal!.reason)
		}
		signal?.addEventListener('abort', onAbort, { once: true })
 
		try {
			return await parked.promise
		} finally {
			signal?.removeEventListener('abort', onAbort)
		}
	}
 
	[Symbol.dispose](): void {
		this.destroy()
	}
 
	async #next(): Promise<IteratorResult<T>> {
		if (this.#destroyed) {
			return { value: undefined as never, done: true }
		}
 
		if (this.#paused) {
			await this.#waitForResume()
			if (this.#destroyed) {
				return { value: undefined as never, done: true }
			}
		}
 
		let item = this.dequeue()
		if (item.hasValue) {
			return { value: item.value, done: false }
		}
 
		if (this.#closed || this.#destroyed) {
			return { value: undefined as never, done: true }
		}
 
		return new Promise<IteratorResult<T>>((resolve) => {
			this.#pendingShift.push(resolve)
		})
	}
 
	#waitForResume(): Promise<void> {
		Iif (!this.#paused || this.#destroyed) {
			return Promise.resolve()
		}
 
		return new Promise<void>((resolve) => {
			this.#pendingResume.push(resolve)
		})
	}
 
	#resolvePendingResume(): void {
		while (this.#pendingResume.length > 0) {
			let resolve = this.#pendingResume.shift()!
			resolve()
		}
	}
 
	#resolvePendingShift(): void {
		if (this.#paused || this.#destroyed) {
			return
		}
 
		while (this.#pendingShift.length > 0 && this.getBufferSize() > 0) {
			let item = this.dequeue()
			Iif (!item.hasValue) {
				break
			}
 
			let resolve = this.#pendingShift.shift()!
			resolve({ value: item.value, done: false })
		}
	}
 
	#resolvePendingDoneIfNeeded(): void {
		if ((this.#closed || this.#destroyed) && this.getBufferSize() === 0) {
			this.#resolvePendingDone()
		}
	}
 
	#resolvePendingDone(): void {
		while (this.#pendingShift.length > 0) {
			let resolve = this.#pendingShift.shift()!
			resolve({ value: undefined as never, done: true })
		}
	}
 
	protected abstract enqueue(value: T, options: P): void
	protected abstract dequeue(): DequeueResult<T>
	protected abstract clearBuffer(): void
	protected abstract getBufferSize(): number
}
 
export class AsyncQueue<T> extends AbstractAsyncQueue<T, void> {
	#values: Array<T | undefined> = []
	#head = 0
	#tail = 0
 
	push(value: T): void {
		this.pushInternal(value, undefined)
	}
 
	protected enqueue(value: T): void {
		this.#values[this.#tail] = value
		this.#tail += 1
	}
 
	protected dequeue(): DequeueResult<T> {
		if (this.#head >= this.#tail) {
			return { hasValue: false }
		}
 
		let value = this.#values[this.#head]!
		this.#values[this.#head] = undefined
		this.#head += 1
 
		this.#compactIfNeeded()
 
		return { hasValue: true, value }
	}
 
	protected clearBuffer(): void {
		this.#values.length = 0
		this.#head = 0
		this.#tail = 0
	}
 
	protected getBufferSize(): number {
		return this.#tail - this.#head
	}
 
	#compactIfNeeded(): void {
		let consumed = this.#head
		let remaining = this.#tail - this.#head
 
		if (consumed < 1024 || consumed < remaining) {
			return
		}
 
		this.#values = this.#values.slice(this.#head, this.#tail)
		this.#tail = remaining
		this.#head = 0
	}
}
 
type PriorityNode<T> = {
	value: T
	priority: number
	sequence: number
}
 
export class AsyncPriorityQueue<T> extends AbstractAsyncQueue<T, number> {
	#heap: PriorityNode<T>[] = []
	#sequence = 0
 
	push(value: T, priority = 0): void {
		this.pushInternal(value, priority)
	}
 
	protected enqueue(value: T, priority: number): void {
		let node: PriorityNode<T> = {
			value,
			priority,
			sequence: this.#sequence,
		}
 
		this.#sequence += 1
		this.#heap.push(node)
		this.#siftUp(this.#heap.length - 1)
	}
 
	protected dequeue(): DequeueResult<T> {
		if (this.#heap.length === 0) {
			return { hasValue: false }
		}
 
		let root = this.#heap[0]!
		let last = this.#heap.pop()!
 
		if (this.#heap.length > 0) {
			this.#heap[0] = last
			this.#siftDown(0)
		}
 
		return { hasValue: true, value: root.value }
	}
 
	protected clearBuffer(): void {
		this.#heap.length = 0
	}
 
	protected getBufferSize(): number {
		return this.#heap.length
	}
 
	#siftUp(index: number): void {
		let child = index
 
		while (child > 0) {
			let parent = (child - 1) >> 1
			let childNode = this.#heap[child]!
			let parentNode = this.#heap[parent]!
 
			if (!this.#isHigherPriority(childNode, parentNode)) {
				break
			}
 
			this.#heap[child] = parentNode
			this.#heap[parent] = childNode
			child = parent
		}
	}
 
	#siftDown(index: number): void {
		let parent = index
		let size = this.#heap.length
 
		while (true) {
			let left = parent * 2 + 1
			let right = left + 1
			let highest = parent
 
			if (left < size && this.#isHigherPriority(this.#heap[left]!, this.#heap[highest]!)) {
				highest = left
			}
 
			if (right < size && this.#isHigherPriority(this.#heap[right]!, this.#heap[highest]!)) {
				highest = right
			}
 
			if (highest === parent) {
				return
			}
 
			let tmp = this.#heap[parent]!
			this.#heap[parent] = this.#heap[highest]!
			this.#heap[highest] = tmp
			parent = highest
		}
	}
 
	#isHigherPriority(left: PriorityNode<T>, right: PriorityNode<T>): boolean {
		if (left.priority !== right.priority) {
			return left.priority > right.priority
		}
 
		return left.sequence < right.sequence
	}
}