diff --git a/packages/context/src/interceptor.ts b/packages/context/src/interceptor.ts index 9e242ae58c48..cdc5ab9f7100 100644 --- a/packages/context/src/interceptor.ts +++ b/packages/context/src/interceptor.ts @@ -16,6 +16,7 @@ import * as debugFactory from 'debug'; import {Binding, BindingTemplate} from './binding'; import {filterByTag} from './binding-filter'; import {BindingAddress} from './binding-key'; +import {sortBindingsByGroup} from './binding-sorter'; import {Context} from './context'; import {ContextBindings, ContextTags} from './keys'; import {transformValueOrPromise, ValueOrPromise} from './value-promise'; @@ -87,21 +88,11 @@ export class InvocationContext extends Context { this.getSync(ContextBindings.GLOBAL_INTERCEPTOR_ORDERED_GROUPS, { optional: true, }) || []; - bindings.sort((a, b) => { - const g1: string = a.tagMap[ContextTags.GLOBAL_INTERCEPTOR_GROUP] || ''; - const g2: string = b.tagMap[ContextTags.GLOBAL_INTERCEPTOR_GROUP] || ''; - const i1 = orderedGroups.indexOf(g1); - const i2 = orderedGroups.indexOf(g2); - if (i1 !== -1 || i2 !== -1) { - // Honor the group order - return i1 - i2; - } else { - // Neither group is in the pre-defined order - // Use alphabetical order instead so that `1-group` is invoked before - // `2-group` - return g1 < g2 ? -1 : g1 > g2 ? 1 : 0; - } - }); + return sortBindingsByGroup( + bindings, + ContextTags.GLOBAL_INTERCEPTOR_GROUP, + orderedGroups, + ); } /** diff --git a/packages/core/src/lifecycle-registry.ts b/packages/core/src/lifecycle-registry.ts index 9e89ee928bd8..6880159ed18b 100644 --- a/packages/core/src/lifecycle-registry.ts +++ b/packages/core/src/lifecycle-registry.ts @@ -3,7 +3,12 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {Binding, ContextView, inject} from '@loopback/context'; +import { + Binding, + ContextView, + inject, + sortBindingsByGroup, +} from '@loopback/context'; import {CoreBindings, CoreTags} from './keys'; import {LifeCycleObserver, lifeCycleObserverFilter} from './lifecycle'; import debugFactory = require('debug'); @@ -111,6 +116,11 @@ export class LifeCycleObserverRegistry implements LifeCycleObserver { string, Readonly>[] > = new Map(); + sortBindingsByGroup( + bindings, + CoreTags.LIFE_CYCLE_OBSERVER_GROUP, + this.options.orderedGroups, + ); for (const binding of bindings) { const group = this.getObserverGroup(binding); let bindingsInGroup = groupMap.get(group); @@ -125,20 +135,7 @@ export class LifeCycleObserverRegistry implements LifeCycleObserver { for (const [group, bindingsInGroup] of groupMap) { groups.push({group, bindings: bindingsInGroup}); } - // Sort the groups - return groups.sort((g1, g2) => { - const i1 = this.options.orderedGroups.indexOf(g1.group); - const i2 = this.options.orderedGroups.indexOf(g2.group); - if (i1 !== -1 || i2 !== -1) { - // Honor the group order - return i1 - i2; - } else { - // Neither group is in the pre-defined order - // Use alphabetical order instead so that `1-group` is invoked before - // `2-group` - return g1.group < g2.group ? -1 : g1.group > g2.group ? 1 : 0; - } - }); + return groups; } /** diff --git a/packages/rest/src/body-parsers/body-parser.ts b/packages/rest/src/body-parsers/body-parser.ts index e6d7cc6e5205..95b47448fe10 100644 --- a/packages/rest/src/body-parsers/body-parser.ts +++ b/packages/rest/src/body-parsers/body-parser.ts @@ -4,8 +4,10 @@ // License text available at https://opensource.org/licenses/MIT import { + compareByOrder, Constructor, Context, + filterByTag, inject, instantiateClass, } from '@loopback/context'; @@ -32,7 +34,7 @@ export class RequestBodyParser { readonly parsers: BodyParser[]; constructor( - @inject.tag(REQUEST_BODY_PARSER_TAG, {optional: true}) + @inject(filterByTag(REQUEST_BODY_PARSER_TAG), {optional: true}) parsers?: BodyParser[], @inject.context() private readonly ctx?: Context, ) { @@ -197,9 +199,7 @@ function isBodyParserClass( * @param parsers */ function sortParsers(parsers: BodyParser[]) { - return parsers.sort((a, b) => { - const i1 = builtinParsers.names.indexOf(a.name); - const i2 = builtinParsers.names.indexOf(b.name); - return i1 - i2; - }); + return parsers.sort((a, b) => + compareByOrder(a.name, b.name, builtinParsers.names), + ); }