Skip to content

Commit

Permalink
Loose extensions where not getting prioritized correctly (#8961)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtimebuddy committed Dec 12, 2022
1 parent 38e2fc3 commit 1462d72
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/extensions/src/index.ts
Expand Up @@ -102,6 +102,16 @@ const normalizeExtension = (ext: ExtensionFormatLoose | any): ExtensionFormat =>
return ext;
};

/**
* Get the priority for an extension.
* @ignore
* @param ext - Any extension
* @param defaultPriority - Fallback priority if none is defined.
* @returns The priority for the extension.
*/
const normalizePriority = (ext: ExtensionFormatLoose | any, defaultPriority: number): number =>
normalizeExtension(ext).priority ?? defaultPriority;

/**
* Global registration of all PixiJS extensions. One-stop-shop for extensibility.
* @memberof PIXI
Expand Down Expand Up @@ -222,9 +232,10 @@ const extensions = {
* Handle a type, but using a list of extensions.
* @param type - Type of extension to handle.
* @param list - The list of extensions.
* @param defaultPriority - The default priority to use if none is specified.
* @returns {PIXI.extensions} For chaining.
*/
handleByList(type: ExtensionType, list: any[])
handleByList(type: ExtensionType, list: any[], defaultPriority = -1)
{
return this.handle(
type,
Expand All @@ -236,7 +247,7 @@ const extensions = {
}

list.push(extension.ref);
list.sort((a, b) => (b.priority ?? -1) - (a.priority ?? -1));
list.sort((a, b) => normalizePriority(b, defaultPriority) - normalizePriority(a, defaultPriority));
},
(extension) =>
{
Expand Down
31 changes: 31 additions & 0 deletions packages/extensions/test/extensions.tests.ts
Expand Up @@ -74,6 +74,37 @@ describe('extensions', () =>
extensions.add(example);
expect(list.length).toBe(1);
});

it('should add extensions in order of priority', () =>
{
const ext1 = {
extension: {
priority: 1,
type: exampleType,
},
};
const ext2 = {
extension: {
priority: 2,
type: exampleType,
},
};
const ext3 = {
extension: {
priority: 0,
type: exampleType,
},
};
const list: any[] = [];

extensions.handleByList(exampleType, list);
extensions.add(ext1);
extensions.add(ext2);
extensions.add(ext3);
expect(list[0]).toBe(ext2);
expect(list[1]).toBe(ext1);
expect(list[2]).toBe(ext3);
});
});

describe('add', () =>
Expand Down

0 comments on commit 1462d72

Please sign in to comment.