Sorted extension point entries with supplied extensionName

This commit is contained in:
Florian Scholdei
2020-02-24 15:02:03 +01:00
parent 1c681ea588
commit a016710c35
4 changed files with 65 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ type Predicate = (props: any) => boolean;
type ExtensionRegistration = {
predicate: Predicate;
extension: any;
extensionName: string;
};
/**
@@ -25,13 +26,14 @@ export class Binder {
* @param extension provided extension
* @param predicate to decide if the extension gets rendered for the given props
*/
bind(extensionPoint: string, extension: any, predicate?: Predicate) {
bind(extensionPoint: string, extension: any, predicate?: Predicate, extensionName?: string) {
if (!this.extensionPoints[extensionPoint]) {
this.extensionPoints[extensionPoint] = [];
}
const registration = {
predicate: predicate ? predicate : () => true,
extension
extension,
extensionName: extensionName ? extensionName : ""
};
this.extensionPoints[extensionPoint].push(registration);
}
@@ -61,6 +63,7 @@ export class Binder {
if (props) {
registrations = registrations.filter(reg => reg.predicate(props || {}));
}
registrations.sort(this.sortExtensions);
return registrations.map(reg => reg.extension);
}
@@ -70,6 +73,27 @@ export class Binder {
hasExtension(extensionPoint: string, props?: object): boolean {
return this.getExtensions(extensionPoint, props).length > 0;
}
/**
* Sort extensions in ascending order.
*/
sortExtensions = (a: ExtensionRegistration, b: ExtensionRegistration) => {
const regA = a.extensionName ? a.extensionName.toUpperCase() : "";
const regB = b.extensionName ? b.extensionName.toUpperCase() : "";
if (regA === "" && regB === "") {
return 0;
} else if (regA === "") {
return 1;
} else if (regB === "") {
return -1;
} else if (regA > regB) {
return 1;
} else if (regA < regB) {
return -1;
}
return 0;
};
}
// singleton binder