use-next-sse 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -69,3 +69,70 @@ export default function Home() {
|
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
This example demonstrates a simple counter that updates every second using Server-Sent Events. The server sends updates for 10 seconds before closing the connection.
|
|
72
|
+
|
|
73
|
+
### Destructor in `createSSEHandler`
|
|
74
|
+
|
|
75
|
+
When using the `createSSEHandler` function in the `use-next-sse` library, it is important to understand the role of the destructor. The destructor is a cleanup function that is called when the SSE connection is closed. This allows you to perform any necessary cleanup tasks, such as closing database connections, stopping intervals, or clearing resources.
|
|
76
|
+
|
|
77
|
+
#### Example Usage
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { createSSEHandler } from 'use-next-sse';
|
|
81
|
+
|
|
82
|
+
const handler = createSSEHandler((send, close) => {
|
|
83
|
+
// Your SSE logic here
|
|
84
|
+
|
|
85
|
+
// Return a destructor function
|
|
86
|
+
return () => {
|
|
87
|
+
// Perform cleanup tasks here
|
|
88
|
+
console.log('SSE connection closed, performing cleanup');
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
export default handler;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Global Example
|
|
96
|
+
|
|
97
|
+
This Destructor will be called even though the handler callback is not called yet.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { createSSEHandler } from 'use-next-sse';
|
|
101
|
+
|
|
102
|
+
const handler = createSSEHandler(
|
|
103
|
+
(send, close) => {
|
|
104
|
+
// Your SSE logic here
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
onClose: () => {
|
|
108
|
+
console.log('SSE connection has been closed and cleaned up.');
|
|
109
|
+
// Perform additional cleanup tasks here
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
export default handler;
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### Context Example
|
|
118
|
+
|
|
119
|
+
This Destructor will be called if the SSECallback call is not done yet.
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import { createSSEHandler } from 'use-next-sse';
|
|
123
|
+
|
|
124
|
+
const handler = createSSEHandler(async(send, close, { onClose }) => {
|
|
125
|
+
// Your SSE logic here
|
|
126
|
+
|
|
127
|
+
onClose(() => {
|
|
128
|
+
console.log('SSE connection closed, performing cleanup.');
|
|
129
|
+
// Perform additional cleanup tasks here
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
// long running task
|
|
134
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
export default handler;
|
|
138
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-next-sse",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "A lightweight Server-Sent Events (SSE) library for Next.js, enabling real-time, unidirectional data streaming from server to client",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"author": "Alexander Kasten",
|
|
34
34
|
"license": "MIT",
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"next": "
|
|
36
|
+
"next": ">=13.5.9 >=14.2.25 >=15.2.3",
|
|
37
37
|
"react": ">=18.0.0",
|
|
38
38
|
"react-dom": ">=18.0.0"
|
|
39
39
|
},
|