Skip to content

Commit

Permalink
chore(context): review - 7
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jan 18, 2019
1 parent b9518c1 commit 7ae8a44
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
26 changes: 18 additions & 8 deletions packages/context/src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface InjectionMetadata {
*/
export type BindingSelector<ValueType = unknown> =
| BindingAddress<ValueType>
| BindingFilter;
| BindingFilter<ValueType>;

/**
* Descriptor for an injection point
Expand Down Expand Up @@ -311,6 +311,12 @@ export namespace inject {
};
}

function isBindingAddress(
bindingSelector: BindingSelector,
): bindingSelector is BindingAddress {
return typeof bindingSelector !== 'function';
}

function resolveAsGetter(
ctx: Context,
injection: Readonly<Injection>,
Expand All @@ -324,14 +330,14 @@ function resolveAsGetter(
`The type of ${targetName} (${targetType.name}) is not a Getter function`,
);
}
if (typeof injection.bindingSelector === 'function') {
const bindingSelector = injection.bindingSelector;
if (!isBindingAddress(bindingSelector)) {
return resolveByFilter(ctx, injection, session);
}
// We need to clone the session for the getter as it will be resolved later
session = ResolutionSession.fork(session);
return function getter() {
const key = injection.bindingSelector as BindingAddress;
return ctx.get(key, {
return ctx.get(bindingSelector, {
session,
optional: injection.metadata && injection.metadata.optional,
});
Expand All @@ -340,17 +346,21 @@ function resolveAsGetter(

function resolveAsSetter(ctx: Context, injection: Injection) {
const targetType = inspectTargetType(injection);
const targetName = ResolutionSession.describeInjection(injection)!.targetName;
if (targetType && targetType !== Function) {
const targetName = ResolutionSession.describeInjection(injection)!
.targetName;
throw new Error(
`The type of ${targetName} (${targetType.name}) is not a Setter function`,
);
}
const bindingSelector = injection.bindingSelector;
if (!isBindingAddress(bindingSelector)) {
throw new Error(
`@inject.setter for (${targetType.name}) does not allow BindingFilter`,
);
}
// No resolution session should be propagated into the setter
return function setter(value: unknown) {
const key = injection.bindingSelector as BindingAddress;
ctx.bind(key).to(value);
ctx.bind(bindingSelector).to(value);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ describe('ContextEventListener - listens on matching bindings', () => {
server.subscribe(contextListener);
givenController(server, '1');
givenController(server.parent!, '2');
return contextListener;
}

function givenController(_ctx: Context, _name: string) {
Expand Down
1 change: 0 additions & 1 deletion packages/context/test/unit/context.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,6 @@ describe('Context', () => {
filter: binding => false,
listen: (event, binding) => {},
};
return listener;
}
});

Expand Down
8 changes: 6 additions & 2 deletions packages/context/test/unit/inject-view.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {

describe('@inject.view', async () => {
let ctx: Context;
beforeEach(() => (ctx = givenContext()));
beforeEach(() => {
ctx = givenContext();
});

class MyControllerWithGetter {
@inject.view(filterByTag('foo'), {watch: true})
Expand Down Expand Up @@ -70,7 +72,9 @@ describe('@inject.view', async () => {

describe('@inject with filter function', async () => {
let ctx: Context;
beforeEach(() => (ctx = givenContext()));
beforeEach(() => {
ctx = givenContext();
});

class MyControllerWithGetter {
@inject.getter(filterByTag('foo'), {watch: true})
Expand Down

0 comments on commit 7ae8a44

Please sign in to comment.