Skip to content

Commit 2873bb0

Browse files
committed
Improve Readme
1 parent 34fa362 commit 2873bb0

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

README.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,51 @@ npm install solid-services
2020

2121
[Open on CodeSandbox](https://codesandbox.io/s/solid-services-uqlnw)
2222

23+
## Services
24+
25+
Service is an object that provides a particular function or set of functions. Services are designed to be "global" objects that can be accessed and used throughout an application, and are often used for features that require shared state or persistent connections.
26+
27+
Some common examples of services include user authentication, geolocation, WebSockets, server-sent events or notifications, server-backed API call libraries, third-party APIs, and logging. Services can be implemented as either a class or a plain object (POJO) and are usually defined as a function that returns an instance of the class or object. For example:
28+
29+
```js
30+
import { createSignal } from 'solid-js';
31+
32+
export function AuthService() {
33+
const [getUser, setUser] = createSignal();
34+
35+
return {
36+
get user() {
37+
return getUser();
38+
},
39+
40+
login(user) {
41+
setUser(user);
42+
},
43+
44+
logout() {
45+
setUser(undefined);
46+
},
47+
};
48+
}
49+
```
50+
51+
To access a service in your components or other services, you can use the `useService` hook. This hook registers the service or returns an existing object if it has already been used in the past. For example:
52+
53+
```jsx
54+
import { useService } from "solid-services";
55+
import AuthService from "./services/auth";
56+
57+
export default function LogoutComponent() {
58+
const getAuthService = useService(AuthService);
59+
60+
function logout() {
61+
getAuthService().logout();
62+
}
63+
64+
return <button onClick={logout}>Logout</button>;
65+
}
66+
```
67+
2368
## Service Registry
2469

2570
The `ServiceRegistry` is a component that creates a context around the components within an application, allowing developers to scope the services to specific parts of the application.
@@ -41,7 +86,7 @@ export default function App() {
4186
```
4287

4388
> ## **Remember!**
44-
>
89+
>
4590
> It is important to wrap your application with a top-level `<ServiceRegistry>` before using services in components. Otherwise, services won't be able to register and their usage will throw an error.
4691
4792
By default, the ServiceRegistry does not expose any services to sub-registries. This means that the components within a sub-registry will not have access to the services defined in the parent registry. However, you can configure this behavior using the `expose` property of the ServiceRegistry.
@@ -88,48 +133,3 @@ export default function App() {
88133
```
89134

90135
By using the ServiceRegistry and the expose property, you can control which services are available to different parts of your application, and manage the shared state and persistent connections within your Solid.js application.
91-
92-
## Services
93-
94-
Service is an object that provides a particular function or set of functions. Services are designed to be "global" objects that can be accessed and used throughout an application, and are often used for features that require shared state or persistent connections.
95-
96-
Some common examples of services include user authentication, geolocation, WebSockets, server-sent events or notifications, server-backed API call libraries, third-party APIs, and logging. Services can be implemented as either a class or a plain object (POJO) and are usually defined as a function that returns an instance of the class or object. For example:
97-
98-
```js
99-
import { createSignal } from 'solid-js';
100-
101-
export function AuthService() {
102-
const [getUser, setUser] = createSignal();
103-
104-
return {
105-
get user() {
106-
return getUser();
107-
},
108-
109-
login(user) {
110-
setUser(user);
111-
},
112-
113-
logout() {
114-
setUser(undefined);
115-
},
116-
};
117-
}
118-
```
119-
120-
To access a service in your components or other services, you can use the `useService` hook. This hook registers the service or returns an existing object if it has already been used in the past. For example:
121-
122-
```jsx
123-
import { useService } from "solid-services";
124-
import AuthService from "./services/auth";
125-
126-
export default function LogoutComponent() {
127-
const getAuthService = useService(AuthService);
128-
129-
function logout() {
130-
getAuthService().logout();
131-
}
132-
133-
return <button onClick={logout}>Logout</button>;
134-
}
135-
```

0 commit comments

Comments
 (0)