Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loose extensions where not getting prioritized correctly #8961

Merged
merged 3 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 =>
SuperSodaSea marked this conversation as resolved.
Show resolved Hide resolved
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