You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46-46Lines changed: 46 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,51 @@ npm install solid-services
20
20
21
21
[Open on CodeSandbox](https://codesandbox.io/s/solid-services-uqlnw)
22
22
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
+
exportfunctionAuthService() {
33
+
const [getUser, setUser] =createSignal();
34
+
35
+
return {
36
+
getuser() {
37
+
returngetUser();
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
+
importAuthServicefrom"./services/auth";
56
+
57
+
exportdefaultfunctionLogoutComponent() {
58
+
constgetAuthService=useService(AuthService);
59
+
60
+
functionlogout() {
61
+
getAuthService().logout();
62
+
}
63
+
64
+
return<button onClick={logout}>Logout</button>;
65
+
}
66
+
```
67
+
23
68
## Service Registry
24
69
25
70
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() {
41
86
```
42
87
43
88
> ## **Remember!**
44
-
>
89
+
>
45
90
> 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.
46
91
47
92
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() {
88
133
```
89
134
90
135
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
-
exportfunctionAuthService() {
102
-
const [getUser, setUser] =createSignal();
103
-
104
-
return {
105
-
getuser() {
106
-
returngetUser();
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:
0 commit comments