-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partial container API improvements #6
Conversation
4142cb5
to
18fab13
Compare
token: Token, | ||
cls: InjectableClass<Services, Service, Tokens> | ||
): Container<AddService<Services, Token, Service>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was almost complete duplicate of providesService method, which we can now reuse.
const factories = { ...this.factories, [token]: factory }; | ||
return new Container(factories as unknown as MaybeMemoizedFactories<AddService<Services, Token, Service>>); | ||
} | ||
providesValue = <Token extends TokenType, Service>(token: Token, value: Service) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-using providesService
appendValue<Token extends TokenType, Service>(token: Token, value: Service): Container<any> { | ||
return this.providesService(ConcatInjectable(token, () => value)); | ||
} | ||
) => this.providesService(ConcatInjectable(token, () => value)) as Container<Services>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-using providesService
@@ -407,13 +407,6 @@ export class Container<Services = {}> { | |||
return this.providesService(fnOrContainer); | |||
} | |||
|
|||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice change! Left few comments.
/** | ||
* Creates an Injectable factory function for an InjectableClass. | ||
* | ||
* @param token Token identifying the Service. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's provide an example how this can be used similarly how we do that for other exported stuff here
constructor(public testService: string) {} | ||
} | ||
|
||
describe("and the new Service does not override", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please replace describe
with test
here and in other case. the expectation should be inside test
block.
|
||
describe("and the new Service does not override", () => { | ||
const partialContainer = new PartialContainer({}).providesClass("NewTestService", NewTestService); | ||
expect(dependenciesContainer.provides(partialContainer).get("NewTestService")).toBeInstanceOf(NewTestService); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need an expectation that a dependency is missing, something line:
const partialContainer = new PartialContainer({}).providesClass("NewTestService", NewTestService);
// @ts-expect-error
expect(() => Container.provides(partialContainer).get("NewTestService")).toThrow(
'[Container::get] Could not find Service for Token "TestService". ' +
"This should've caused a compile-time error. If the Token is 'undefined', " +
"check all your calls to the Injectable function. Make sure you define dependencies " +
"using string literals or string constants that are definitely initialized before the call to Injectable."
);
Would be nice to have that for other provide*
methods on partial container.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGRM!
providesValue
andprovidesClass
to thePartialContainer
API