Skip to content
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

Merged
merged 7 commits into from
Sep 19, 2024
Merged

Partial container API improvements #6

merged 7 commits into from
Sep 19, 2024

Conversation

kburov-sc
Copy link
Collaborator

@kburov-sc kburov-sc commented Sep 8, 2024

  • Added providesValue and providesClass to the PartialContainer API
  • Extracted common logic for initialising class-based injectables

token: Token,
cls: InjectableClass<Services, Service, Tokens>
): Container<AddService<Services, Token, Service>> {
Copy link
Collaborator Author

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.

@kburov-sc kburov-sc marked this pull request as ready for review September 9, 2024 07:07
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) =>
Copy link
Collaborator Author

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>;
Copy link
Collaborator Author

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);
}

/**
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate comment

Copy link
Collaborator

@mikalai-snap mikalai-snap left a 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.
Copy link
Collaborator

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", () => {
Copy link
Collaborator

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);
Copy link
Collaborator

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.

mikalai-snap
mikalai-snap previously approved these changes Sep 18, 2024
Copy link
Collaborator

@mikalai-snap mikalai-snap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGRM!

@kburov-sc kburov-sc merged commit d716d2a into main Sep 19, 2024
1 check passed
@kburov-sc kburov-sc deleted the kb-partial-sugar branch September 19, 2024 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants