diff --git a/CHANGELOG.md b/CHANGELOG.md index 2113ba77f..2fd68e0e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Constructor parameters which share a name with a property on a parent class will no longer inherit the comment on the parent class, #2636. - Packages mode will now attempt to use the comment declared in the comment class for inherited members, #2622. - TypeDoc no longer crashes when `@document` includes an empty file, #2638. +- API: Event listeners added later with the same priority will be called later, #2643. ### Thanks! diff --git a/src/lib/utils/array.ts b/src/lib/utils/array.ts index 128e8cb8b..3d13f332f 100644 --- a/src/lib/utils/array.ts +++ b/src/lib/utils/array.ts @@ -2,7 +2,7 @@ export const emptyArray: readonly [] = []; /** * Inserts an item into an array sorted by priority. If two items have the same priority, - * the item will be inserted later will be placed earlier in the array. + * the item will be inserted later will be placed later in the array. * @param arr modified by inserting item. * @param item */ @@ -10,7 +10,7 @@ export function insertPrioritySorted( arr: T[], item: T, ): T[] { - const index = binaryFindPartition(arr, (v) => v.priority <= item.priority); + const index = binaryFindPartition(arr, (v) => v.priority < item.priority); arr.splice(index === -1 ? arr.length : index, 0, item); return arr; } diff --git a/src/test/events.test.ts b/src/test/events.test.ts index 5f3a1b107..899209047 100644 --- a/src/test/events.test.ts +++ b/src/test/events.test.ts @@ -66,13 +66,13 @@ describe("EventDispatcher", () => { equal(calls, 3); }); - it("Calls listeners according to their order", () => { + it("Calls listeners according to their priority", () => { const emitter = new EventDispatcher<{ a: [] }>(); const calls: number[] = []; emitter.on("a", () => calls.push(3), 25); - emitter.on("a", () => calls.push(2), 50); emitter.on("a", () => calls.push(1), 50); + emitter.on("a", () => calls.push(2), 50); emitter.trigger("a"); equal(calls, [1, 2, 3]); diff --git a/src/test/utils/array.test.ts b/src/test/utils/array.test.ts index e7bf939d4..bbee37eb7 100644 --- a/src/test/utils/array.test.ts +++ b/src/test/utils/array.test.ts @@ -37,9 +37,9 @@ describe("Array utils", () => { ]); }); - it("inserts new items first", () => { - const item0 = { priority: 1, first: true }; - equal(insertPrioritySorted([item1], item0), [item0, item1]); + it("inserts new items last", () => { + const item0 = { priority: 1, first: false }; + equal(insertPrioritySorted([item1], item0), [item1, item0]); }); });