Skip to content

Commit 44a72e5

Browse files
committed
Call listeners with the same priority later
Resolves #2643
1 parent 977dd19 commit 44a72e5

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Constructor parameters which share a name with a property on a parent class will no longer inherit the comment on the parent class, #2636.
1010
- Packages mode will now attempt to use the comment declared in the comment class for inherited members, #2622.
1111
- TypeDoc no longer crashes when `@document` includes an empty file, #2638.
12+
- API: Event listeners added later with the same priority will be called later, #2643.
1213

1314
### Thanks!
1415

src/lib/utils/array.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ export const emptyArray: readonly [] = [];
22

33
/**
44
* Inserts an item into an array sorted by priority. If two items have the same priority,
5-
* the item will be inserted later will be placed earlier in the array.
5+
* the item will be inserted later will be placed later in the array.
66
* @param arr modified by inserting item.
77
* @param item
88
*/
99
export function insertPrioritySorted<T extends { priority: number }>(
1010
arr: T[],
1111
item: T,
1212
): T[] {
13-
const index = binaryFindPartition(arr, (v) => v.priority <= item.priority);
13+
const index = binaryFindPartition(arr, (v) => v.priority < item.priority);
1414
arr.splice(index === -1 ? arr.length : index, 0, item);
1515
return arr;
1616
}

src/test/events.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ describe("EventDispatcher", () => {
6666
equal(calls, 3);
6767
});
6868

69-
it("Calls listeners according to their order", () => {
69+
it("Calls listeners according to their priority", () => {
7070
const emitter = new EventDispatcher<{ a: [] }>();
7171

7272
const calls: number[] = [];
7373
emitter.on("a", () => calls.push(3), 25);
74-
emitter.on("a", () => calls.push(2), 50);
7574
emitter.on("a", () => calls.push(1), 50);
75+
emitter.on("a", () => calls.push(2), 50);
7676

7777
emitter.trigger("a");
7878
equal(calls, [1, 2, 3]);

src/test/utils/array.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ describe("Array utils", () => {
3737
]);
3838
});
3939

40-
it("inserts new items first", () => {
41-
const item0 = { priority: 1, first: true };
42-
equal(insertPrioritySorted([item1], item0), [item0, item1]);
40+
it("inserts new items last", () => {
41+
const item0 = { priority: 1, first: false };
42+
equal(insertPrioritySorted([item1], item0), [item1, item0]);
4343
});
4444
});
4545

0 commit comments

Comments
 (0)