Skip to content

Commit

Permalink
Merge pull request #3 from Exelord/lazy-use-service
Browse files Browse the repository at this point in the history
Return a getter instead of a service from useService
  • Loading branch information
Exelord authored Jan 9, 2022
2 parents 0dd4d8c + 429098f commit df4c0d2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRegistry } from "./context";
import type { ServiceInitializer } from "./registry";

export function useService<T>(initializer: ServiceInitializer<T>): T {
export function useService<T>(initializer: ServiceInitializer<T>): () => T {
const registry = useRegistry();
return registry.get(initializer) || registry.register(initializer);
return () => registry.get(initializer) || registry.register(initializer);
}
4 changes: 2 additions & 2 deletions tests/src/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ describe("useService", () => {
const MyComponent = () => {
const myService = useService(MyService);

expect(myService).toMatchObject({ service: "my" });
expect(myService()).toMatchObject({ service: "my" });
expect(spy).toBeCalledTimes(1);

const myServiceAgain = useService(MyService);

expect(myServiceAgain).toBe(myService);
expect(myServiceAgain()).toBe(myService());
expect(spy).toBeCalledTimes(1);

return undefined;
Expand Down

0 comments on commit df4c0d2

Please sign in to comment.