@laravel/stream-react 0.1.0 → 0.2.0
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 +47 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.es.js +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -20,10 +20,10 @@ npm install @laravel/stream-react
|
|
|
20
20
|
Provide your stream URL and the hook will automatically update the `message` with the concatenated response as messages are returned from your server:
|
|
21
21
|
|
|
22
22
|
```tsx
|
|
23
|
-
import {
|
|
23
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
24
24
|
|
|
25
25
|
function App() {
|
|
26
|
-
const { message } =
|
|
26
|
+
const { message } = useEventStream("/stream");
|
|
27
27
|
|
|
28
28
|
return <div>{message}</div>;
|
|
29
29
|
}
|
|
@@ -32,10 +32,10 @@ function App() {
|
|
|
32
32
|
You also have access to the array of message parts:
|
|
33
33
|
|
|
34
34
|
```tsx
|
|
35
|
-
import {
|
|
35
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
36
36
|
|
|
37
37
|
function App() {
|
|
38
|
-
const { messageParts } =
|
|
38
|
+
const { messageParts } = useEventStream("/stream");
|
|
39
39
|
|
|
40
40
|
return (
|
|
41
41
|
<ul>
|
|
@@ -47,16 +47,14 @@ function App() {
|
|
|
47
47
|
}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
The second parameter is options object
|
|
50
|
+
The second parameter is an options object where all properties are optional (defaults are shown below):
|
|
51
51
|
|
|
52
52
|
```tsx
|
|
53
|
-
import {
|
|
53
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
54
54
|
|
|
55
55
|
function App() {
|
|
56
|
-
const { message } =
|
|
56
|
+
const { message } = useEventStream("/stream", {
|
|
57
57
|
event: "update",
|
|
58
|
-
endSignal: "</stream>",
|
|
59
|
-
glue: " ",
|
|
60
58
|
onMessage: (message) => {
|
|
61
59
|
//
|
|
62
60
|
},
|
|
@@ -66,12 +64,52 @@ function App() {
|
|
|
66
64
|
onComplete: () => {
|
|
67
65
|
//
|
|
68
66
|
},
|
|
67
|
+
endSignal: "</stream>",
|
|
68
|
+
glue: " ",
|
|
69
69
|
});
|
|
70
70
|
|
|
71
71
|
return <div>{message}</div>;
|
|
72
72
|
}
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
+
You can close the connection manually by using the returned `close` function:
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
79
|
+
import { useEffect } from "react";
|
|
80
|
+
|
|
81
|
+
function App() {
|
|
82
|
+
const { message, close } = useEventStream("/stream");
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
close();
|
|
87
|
+
}, 3000);
|
|
88
|
+
}, []);
|
|
89
|
+
|
|
90
|
+
return <div>{message}</div>;
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The `clearMessage` function may be used to clear the message content that has been received so far:
|
|
95
|
+
|
|
96
|
+
```tsx
|
|
97
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
98
|
+
import { useEffect } from "react";
|
|
99
|
+
|
|
100
|
+
function App() {
|
|
101
|
+
const { message, clearMessage } = useEventStream("/stream");
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
setTimeout(() => {
|
|
105
|
+
clearMessage();
|
|
106
|
+
}, 3000);
|
|
107
|
+
}, []);
|
|
108
|
+
|
|
109
|
+
return <div>{message}</div>;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
75
113
|
## License
|
|
76
114
|
|
|
77
115
|
Laravel Stream is open-sourced software licensed under the [MIT license](LICENSE.md).
|
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,6 @@ declare type StreamResult = {
|
|
|
24
24
|
*
|
|
25
25
|
* @returns StreamResult object containing the accumulated response, close, and reset functions
|
|
26
26
|
*/
|
|
27
|
-
export declare const
|
|
27
|
+
export declare const useEventStream: (url: string, { eventName, endSignal, glue, onMessage, onComplete, onError, }?: Options) => StreamResult;
|
|
28
28
|
|
|
29
29
|
export { }
|
package/dist/index.es.js
CHANGED
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(s,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],e):(s=typeof globalThis<"u"?globalThis:s||self,e(s.LaravelStreamReact={},s.React))})(this,function(s,e){"use strict";const c="data: ",b=(d,{eventName:n="update",endSignal:f="</stream>",glue:g=" ",onMessage:M=()=>null,onComplete:R=()=>null,onError:C=()=>null}={})=>{const r=e.useRef(null),u=e.useRef([]),[L,m]=e.useState(""),[P,p]=e.useState([]),a=e.useCallback(()=>{u.current=[],m(""),p([])},[]),l=e.useCallback(t=>{if([f,`${c}${f}`].includes(t.data)){o(),R();return}u.current.push(t.data.startsWith(c)?t.data.substring(c.length):t.data),m(u.current.join(g)),p(u.current),M(t)},[n,g]),i=e.useCallback(t=>{C(t),o()},[]),o=e.useCallback((t=!1)=>{var E,h,S;(E=r.current)==null||E.removeEventListener(n,l),(h=r.current)==null||h.removeEventListener("error",i),(S=r.current)==null||S.close(),r.current=null,t&&a()},[]);return e.useEffect(()=>(a(),r.current=new EventSource(d),r.current.addEventListener(n,l),r.current.addEventListener("error",i),o),[d,n,l,i,a]),{message:L,messageParts:P,close:o,clearMessage:a}};s.useEventStream=b,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|