Skip to content

Commit 8d6b455

Browse files
committed
docs: ✏️ improvements
1 parent 31631bc commit 8d6b455

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

documentation/docs/react/05-sockets/use-emitter.mdx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,49 @@ const MessageComponent: React.FC = () => {
9090
To handle side effects such as notifications, logging, or triggering other actions, `useEmitter` provides a set of event
9191
handler hooks. This keeps your component's rendering logic clean and separates side effects from state management.
9292

93-
1. `onEvent`: Fires before we send event message.
94-
2. `onError`: Fires when an error occurs.
95-
3. `onReconnecting`: Fires when the socket is attempting to reconnect.
93+
1. `onEmit`: Fires before we send event message.
94+
2. `onEmitError`: Fires when an error occurs during the emission.
95+
3. `onConnected`: Fires when the socket connection is successfully opened.
96+
4. `onDisconnected`: Fires when the socket connection is closed.
97+
5. `onConnecting`: Fires when the socket is connecting.
98+
6. `onReconnecting`: Fires when the socket is attempting to reconnect.
99+
7. `onReconnectingFailed`: Fires when the socket fails to reconnect.
96100

97101
```tsx
98102
import { useEmitter } from "@hyper-fetch/react";
99103
import { emitMessage } from "./api";
100104

101105
const MessageComponent: React.FC = () => {
102-
const { emit, onEvent, onError, onReconnecting } = useEmitter(emitMessage);
106+
const { emit, onEmit, onEmitError, onConnected, onDisconnected, onConnecting, onReconnecting, onReconnectingFailed } =
107+
useEmitter(emitMessage);
103108

104-
onEvent((emitter) => {
109+
onEmit((emitter) => {
105110
// Event before we send event message
106111
console.log(emitter); // Emitter instance
107112
});
108113

109-
onError((error) => {
114+
onEmitError((error) => {
110115
console.log(error); // Error Event
111116
});
112117

113-
onReconnecting((reconnectingAttempt) => {
114-
console.log(reconnectingAttempt); // 1
118+
onConnected(() => {
119+
toast({ type: "info", message: "Socket connection opened." });
120+
});
121+
122+
onDisconnected(() => {
123+
toast({ type: "warning", message: "Socket connection closed." });
124+
});
125+
126+
onConnecting(() => {
127+
toast({ type: "info", message: "Socket is connecting." });
128+
});
129+
130+
onReconnecting(({ attempts }) => {
131+
toast({ type: "warning", message: `Reconnecting... (attempt ${attempts})` });
132+
});
133+
134+
onReconnectingFailed(({ attempts }) => {
135+
toast({ type: "error", message: `Reconnecting failed after ${attempts} attempts.` });
115136
});
116137

117138
// ...

documentation/docs/react/05-sockets/use-listener.mdx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const onChatMessage = socketInstance.createListener<ChatMessageType>()({
4444
Now, use it in your component to listen for new messages. The `onEvent` helper hook lets you react to incoming data and
4545
update your component's state.
4646

47-
```tsx live title="Listening to chat messages"
47+
```tsx title="Listening to chat messages"
4848
import { useListener } from "@hyper-fetch/react";
4949
import { onChatMessage } from "./api/socket";
5050

@@ -87,17 +87,20 @@ cluttering your component's rendering logic.
8787

8888
1. **`onEvent`**: Fires whenever a new event is received from the server.
8989
2. **`onError`**: Fires when a connection or subscription error occurs.
90-
3. **`onOpen`**: Fires when the socket connection is successfully opened.
91-
4. **`onClose`**: Fires when the socket connection is closed.
92-
5. **`onReconnecting`**: Fires when the socket is attempting to reconnect.
90+
3. **`onConnected`**: Fires when the socket connection is successfully opened.
91+
4. **`onDisconnected`**: Fires when the socket connection is closed.
92+
5. **`onConnecting`**: Fires when the socket is connecting.
93+
6. **`onReconnecting`**: Fires when the socket is attempting to reconnect.
94+
7. **`onReconnectingFailed`**: Fires when the socket fails to reconnect.
9395

94-
```tsx live title="Using event handlers"
96+
```tsx title="Using event handlers"
9597
import { useListener } from "@hyper-fetch/react";
9698
import { onChatMessage } from "./api/socket";
9799

98100
function App() {
99101
const [messages, setMessages] = useState([]);
100-
const { onEvent, onError, onOpen, onClose, onReconnecting } = useListener(onChatMessage);
102+
const { onEvent, onError, onConnected, onDisconnected, onConnecting, onReconnecting, onReconnectingFailed } =
103+
useListener(onChatMessage);
101104

102105
onEvent(({ data: message }) => {
103106
toast({ type: "success", message: `New message: ${message.text}` });
@@ -108,16 +111,24 @@ function App() {
108111
toast({ type: "error", message: `Connection error: ${error.message}` });
109112
});
110113

111-
onOpen(() => {
114+
onConnected(() => {
112115
toast({ type: "info", message: "Socket connection opened." });
113116
});
114117

115-
onClose(() => {
118+
onDisconnected(() => {
116119
toast({ type: "warning", message: "Socket connection closed." });
117120
});
118121

119-
onReconnecting((attempt) => {
120-
toast({ type: "warning", message: `Reconnecting... (attempt ${attempt})` });
122+
onConnecting(() => {
123+
toast({ type: "info", message: "Socket is connecting." });
124+
});
125+
126+
onReconnecting(({ attempts }) => {
127+
toast({ type: "warning", message: `Reconnecting... (attempt ${attempts})` });
128+
});
129+
130+
onReconnectingFailed(({ attempts }) => {
131+
toast({ type: "error", message: `Reconnecting failed after ${attempts} attempts.` });
121132
});
122133

123134
return (

0 commit comments

Comments
 (0)