diff --git a/.changeset/funny-maps-cough.md b/.changeset/funny-maps-cough.md new file mode 100644 index 0000000000..650b9746a1 --- /dev/null +++ b/.changeset/funny-maps-cough.md @@ -0,0 +1,5 @@ +--- +"effect": patch +--- + +ensure Scheduler tasks are added to a matching priority bucket diff --git a/packages/effect/src/Scheduler.ts b/packages/effect/src/Scheduler.ts index caf2aecea4..853d02b98a 100644 --- a/packages/effect/src/Scheduler.ts +++ b/packages/effect/src/Scheduler.ts @@ -37,27 +37,22 @@ export class PriorityBuckets { * @since 2.0.0 */ scheduleTask(task: T, priority: number) { + const length = this.buckets.length let bucket: [number, Array] | 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]> = [] - 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]]) } } }