Skip to content

Commit

Permalink
chore: refactor code to use binding sorter
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 10, 2019
1 parent cb6d0b7 commit 5b0407f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
21 changes: 6 additions & 15 deletions packages/context/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {sortBindingsByPhase} from './binding-sorter';
import {Context} from './context';
import {ContextBindings, ContextTags} from './keys';
import {transformValueOrPromise, ValueOrPromise} from './value-promise';
Expand Down Expand Up @@ -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 sortBindingsByPhase(
bindings,
ContextTags.GLOBAL_INTERCEPTOR_GROUP,
orderedGroups,
);
}

/**
Expand Down
27 changes: 12 additions & 15 deletions packages/core/src/lifecycle-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
sortBindingsByPhase,
} from '@loopback/context';
import {CoreBindings, CoreTags} from './keys';
import {LifeCycleObserver, lifeCycleObserverFilter} from './lifecycle';
import debugFactory = require('debug');
Expand Down Expand Up @@ -111,6 +116,11 @@ export class LifeCycleObserverRegistry implements LifeCycleObserver {
string,
Readonly<Binding<LifeCycleObserver>>[]
> = new Map();
sortBindingsByPhase(
bindings,
CoreTags.LIFE_CYCLE_OBSERVER_GROUP,
this.options.orderedGroups,
);
for (const binding of bindings) {
const group = this.getObserverGroup(binding);
let bindingsInGroup = groupMap.get(group);
Expand All @@ -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;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions packages/rest/src/body-parsers/body-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// License text available at https://opensource.org/licenses/MIT

import {
compareByOrder,
Constructor,
Context,
filterByTag,
inject,
instantiateClass,
} from '@loopback/context';
Expand All @@ -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,
) {
Expand Down Expand Up @@ -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),
);
}

0 comments on commit 5b0407f

Please sign in to comment.