Skip to content

Commit

Permalink
ensure Scheduler tasks are added to a matching priority bucket (#3452)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Aug 14, 2024
1 parent 5dcb401 commit c940df6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-maps-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

ensure Scheduler tasks are added to a matching priority bucket
19 changes: 7 additions & 12 deletions packages/effect/src/Scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,22 @@ export class PriorityBuckets<in out T = Task> {
* @since 2.0.0
*/
scheduleTask(task: T, priority: number) {
const length = this.buckets.length
let bucket: [number, Array<T>] | undefined = undefined
let index: number
for (index = 0; index < this.buckets.length; index++) {
let index = 0
for (; index < length; index++) {
if (this.buckets[index][0] <= priority) {
bucket = this.buckets[index]
} else {
break
}
}
if (bucket) {
if (bucket && bucket[0] === priority) {
bucket[1].push(task)
} else if (index === length) {
this.buckets.push([priority, [task]])
} else {
const newBuckets: Array<[number, Array<T>]> = []
for (let i = 0; i < index; i++) {
newBuckets.push(this.buckets[i])
}
newBuckets.push([priority, [task]])
for (let i = index; i < this.buckets.length; i++) {
newBuckets.push(this.buckets[i])
}
this.buckets = newBuckets
this.buckets.splice(index, 0, [priority, [task]])
}
}
}
Expand Down

0 comments on commit c940df6

Please sign in to comment.