Skip to content

Commit 64414f4

Browse files
committed
refactor: deduplicate PriorityQueue scheduling to reduce bundle size
Extract shared scheduling logic into private scheduleItem() method, reused by both pushWithBackoff and pushWithDelay. Brings browser bundle back under the 29.7 kB size limit.
1 parent fbb7aa4 commit 64414f4

1 file changed

Lines changed: 7 additions & 18 deletions

File tree

  • packages/core/src/priority-queue

packages/core/src/priority-queue/index.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,19 @@ export class PriorityQueue<Item extends QueueItem = QueueItem> extends Emitter {
5252
return this.push(item)[0]
5353
}
5454

55-
const attempt = this.updateAttempts(item)
56-
57-
if (attempt > this.maxAttempts || this.includes(item)) {
58-
return false
59-
}
60-
61-
let timeout = backoff({ attempt: attempt - 1 })
55+
let timeout = backoff({ attempt: this.getAttempts(item) })
6256
if (minTimeout > 0 && timeout < minTimeout) {
6357
timeout = minTimeout
6458
}
6559

66-
setTimeout(() => {
67-
this.queue.push(item)
68-
// remove from future list
69-
this.future = this.future.filter((f) => f.id !== item.id)
70-
// Lets listeners know that a 'future' message is now available in the queue
71-
this.emit(ON_REMOVE_FROM_FUTURE)
72-
}, timeout)
73-
74-
this.future.push(item)
75-
return true
60+
return this.scheduleItem(item, timeout)
7661
}
7762

7863
pushWithDelay(item: Item, delay: number): boolean {
64+
return this.scheduleItem(item, delay)
65+
}
66+
67+
private scheduleItem(item: Item, timeout: number): boolean {
7968
const attempt = this.updateAttempts(item)
8069

8170
if (attempt > this.maxAttempts || this.includes(item)) {
@@ -86,7 +75,7 @@ export class PriorityQueue<Item extends QueueItem = QueueItem> extends Emitter {
8675
this.queue.push(item)
8776
this.future = this.future.filter((f) => f.id !== item.id)
8877
this.emit(ON_REMOVE_FROM_FUTURE)
89-
}, delay)
78+
}, timeout)
9079

9180
this.future.push(item)
9281
return true

0 commit comments

Comments
 (0)