feat: sort interceptors by priority, ensure myItemsInterceptor runs first (#5965)

* feat: sort interceptors by priority

* fix(#5914): high priority for MyItemsInterceptor

* fix: create myItems if object is falsy

* test(e2e): update snapshots

Co-authored-by: Scott Bell <scott@traclabs.com>
This commit is contained in:
Jesse Mazzella 2022-11-10 08:00:29 -08:00 committed by GitHub
parent fabfecdb3e
commit 8b5daad65c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 4 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -19,6 +19,7 @@
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
const DEFAULT_INTERCEPTOR_PRIORITY = 0;
export default class InterceptorRegistry {
/**
* A InterceptorRegistry maintains the definitions for different interceptors that may be invoked on domain objects.
@ -45,7 +46,6 @@ export default class InterceptorRegistry {
* @memberof module:openmct.InterceptorRegistry#
*/
addInterceptor(interceptorDef) {
//TODO: sort by priority
this.interceptors.push(interceptorDef);
}
@ -56,10 +56,18 @@ export default class InterceptorRegistry {
* @memberof module:openmct.InterceptorRegistry#
*/
getInterceptors(identifier, object) {
function byPriority(interceptorA, interceptorB) {
let priorityA = interceptorA.priority ?? DEFAULT_INTERCEPTOR_PRIORITY;
let priorityB = interceptorB.priority ?? DEFAULT_INTERCEPTOR_PRIORITY;
return priorityB - priorityA;
}
return this.interceptors.filter(interceptor => {
return typeof interceptor.appliesTo === 'function'
&& interceptor.appliesTo(identifier, object);
});
}).sort(byPriority);
}
}

View File

@ -37,14 +37,15 @@ function myItemsInterceptor(openmct, identifierObject, name) {
return identifier.key === MY_ITEMS_KEY;
},
invoke: (identifier, object) => {
if (openmct.objects.isMissing(object)) {
if (!object || openmct.objects.isMissing(object)) {
openmct.objects.save(myItemsModel);
return myItemsModel;
}
return object;
}
},
priority: openmct.priority.HIGH
};
}