Skip to content

Commit c3be31e

Browse files
committed
more safely don't read drived state internally in queuers
1 parent 589b83d commit c3be31e

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

packages/pacer/src/async-queuer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export class AsyncQueuer<TValue> {
359359
const activeItems = this.store.state.activeItems
360360
while (
361361
activeItems.length < this.#getConcurrency() &&
362-
!this.store.state.isEmpty
362+
this.store.state.items.length > 0
363363
) {
364364
const nextItem = this.peekNextItem()
365365
if (!nextItem) {
@@ -402,7 +402,7 @@ export class AsyncQueuer<TValue> {
402402
position: QueuePosition = this.options.addItemsTo ?? 'back',
403403
runOnItemsChange: boolean = true,
404404
): boolean => {
405-
if (this.store.state.isFull) {
405+
if (this.store.state.items.length >= (this.options.maxSize ?? Infinity)) {
406406
this.#setState({
407407
rejectionCount: this.store.state.rejectionCount + 1,
408408
})
@@ -578,7 +578,7 @@ export class AsyncQueuer<TValue> {
578578
const expiredIndices: Array<number> = []
579579

580580
// Find indices of expired items
581-
for (let i = 0; i < this.store.state.size; i++) {
581+
for (let i = 0; i < this.store.state.items.length; i++) {
582582
const timestamp = this.store.state.itemTimestamps[i]
583583
if (timestamp === undefined) continue
584584

@@ -633,7 +633,7 @@ export class AsyncQueuer<TValue> {
633633
if (position === 'front') {
634634
return this.store.state.items[0]
635635
}
636-
return this.store.state.items[this.store.state.size - 1]
636+
return this.store.state.items[this.store.state.items.length - 1]
637637
}
638638

639639
/**
@@ -662,7 +662,7 @@ export class AsyncQueuer<TValue> {
662662
*/
663663
start = (): void => {
664664
this.#setState({ isRunning: true })
665-
if (!this.store.state.pendingTick && !this.store.state.isEmpty) {
665+
if (!this.store.state.pendingTick && this.store.state.items.length > 0) {
666666
this.#tick()
667667
}
668668
}

packages/pacer/src/queuer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class Queuer<TValue> {
331331
// Check for expired items
332332
this.#checkExpiredItems()
333333

334-
while (!this.store.state.isEmpty) {
334+
while (this.store.state.items.length > 0) {
335335
const nextItem = this.execute(this.options.getItemsFrom ?? 'front')
336336
if (nextItem === undefined) {
337337
break
@@ -366,7 +366,7 @@ export class Queuer<TValue> {
366366
position: QueuePosition = this.options.addItemsTo ?? 'back',
367367
runOnItemsChange: boolean = true,
368368
): boolean => {
369-
if (this.store.state.isFull) {
369+
if (this.store.state.items.length >= (this.options.maxSize ?? Infinity)) {
370370
this.#setState({
371371
rejectionCount: this.store.state.rejectionCount + 1,
372372
})
@@ -579,7 +579,7 @@ export class Queuer<TValue> {
579579
if (position === 'front') {
580580
return this.store.state.items[0]
581581
}
582-
return this.store.state.items[this.store.state.size - 1]
582+
return this.store.state.items[this.store.state.items.length - 1]
583583
}
584584

585585
/**
@@ -594,7 +594,7 @@ export class Queuer<TValue> {
594594
*/
595595
start = () => {
596596
this.#setState({ isRunning: true })
597-
if (!this.store.state.pendingTick && !this.store.state.isEmpty) {
597+
if (!this.store.state.pendingTick && this.store.state.items.length > 0) {
598598
this.#tick()
599599
}
600600
}

0 commit comments

Comments
 (0)