Skip to content

Commit

Permalink
Call listeners with the same priority later
Browse files Browse the repository at this point in the history
Resolves #2643
  • Loading branch information
Gerrit0 committed Jul 21, 2024
1 parent 977dd19 commit 44a72e5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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
*/
export function insertPrioritySorted<T extends { priority: number }>(
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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
6 changes: 3 additions & 3 deletions src/test/utils/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});

Expand Down

0 comments on commit 44a72e5

Please sign in to comment.