From 429098fc104b1e120a05247cc3a31ef49637706b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kwa=C5=9Bniak?= Date: Sun, 9 Jan 2022 10:52:10 +0100 Subject: [PATCH] Return a getter instead of service from useService --- src/service.ts | 4 ++-- tests/src/service.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/service.ts b/src/service.ts index f5c25c5..68e1bd7 100644 --- a/src/service.ts +++ b/src/service.ts @@ -1,7 +1,7 @@ import { useRegistry } from "./context"; import type { ServiceInitializer } from "./registry"; -export function useService(initializer: ServiceInitializer): T { +export function useService(initializer: ServiceInitializer): () => T { const registry = useRegistry(); - return registry.get(initializer) || registry.register(initializer); + return () => registry.get(initializer) || registry.register(initializer); } diff --git a/tests/src/service.test.ts b/tests/src/service.test.ts index e4bbe28..4471832 100644 --- a/tests/src/service.test.ts +++ b/tests/src/service.test.ts @@ -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;