Skip to content

Commit

Permalink
Always require ServiceRegistryProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed Apr 10, 2022
1 parent 14451c1 commit 5d3ee1c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ npm i solid-services

`ServiceRegistry` will create a context around your components allowing you to scope the services to specific part of the application.

By default, you don't need to do anything as your application will use a global registry. However, if you plan to run a few apps on one page it might be good idea to isolate their services states.

```tsx
// app.tsx
import { ServiceRegistry } from 'solid-services';
Expand Down
12 changes: 9 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type RegistryProviderProps = {
registry?: Registry;
};

const ServiceRegistryContext = createContext(createRegistry());
const ServiceRegistryContext = createContext<Registry>();

export const ServiceRegistry: Component<RegistryProviderProps> = (props) => {
const registry = createMemo(() => props.registry || createRegistry());
Expand All @@ -29,6 +29,12 @@ export const ServiceRegistry: Component<RegistryProviderProps> = (props) => {
});
};

export function useRegistry() {
return useContext(ServiceRegistryContext);
export function useRegistry(): Registry {
const registry = useContext(ServiceRegistryContext);

if (registry) return registry;

throw new Error(
"Your app needs to be wrapped with <ServiceRegistry> context in order to use services!"
);
}
25 changes: 23 additions & 2 deletions tests/src/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, test, expect } from "vitest";
import { createComponent } from "solid-js";
import { ServiceRegistry, useRegistry } from "../../src/context";
import { Registry } from "../../src/registry";

describe("ServiceRegistry", () => {
test("creates a context", () => {
Expand All @@ -25,13 +26,33 @@ describe("ServiceRegistry", () => {
return undefined;
};

const globalRegistry = useRegistry();
const globalRegistry = new Registry();
globalRegistry.register(GlobalService);

createComponent(ServiceRegistry, {
get registry() {
return globalRegistry;
},

get children() {
return createComponent(MyComponent, {});
return createComponent(ServiceRegistry, {
get children() {
return createComponent(MyComponent, {});
},
});
},
});
});

test("throws error when used without the registry provider", () => {
const MyComponent = () => {
expect(() => useRegistry()).toThrowError(
"Your app needs to be wrapped with <ServiceRegistry> context in order to use services!"
);

return undefined;
};

createComponent(MyComponent, {});
});
});
7 changes: 6 additions & 1 deletion tests/src/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, test, expect, vi } from "vitest";
import { createComponent } from "solid-js";
import { useService } from "../../src/service";
import { ServiceRegistry } from "../../src/context";

describe("useService", () => {
test("registers a service if it does not exist", () => {
Expand All @@ -25,6 +26,10 @@ describe("useService", () => {
return undefined;
};

createComponent(MyComponent, {});
createComponent(ServiceRegistry, {
get children() {
return createComponent(MyComponent, {});
},
});
});
});

0 comments on commit 5d3ee1c

Please sign in to comment.