@laravel/stream-react 0.1.0 → 0.2.1
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 +91 -31
- package/dist/index.d.ts +3 -2
- package/dist/index.es.js +34 -24
- package/dist/index.umd.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -20,55 +20,115 @@ 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
|
-
|
|
26
|
+
const { message } = useEventStream("/stream");
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
return <div>{message}</div>;
|
|
29
29
|
}
|
|
30
30
|
```
|
|
31
31
|
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
38
|
+
const { messageParts } = useEventStream("/stream");
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<ul>
|
|
42
|
+
{messageParts.forEach((message) => (
|
|
43
|
+
<li>{message}</li>
|
|
44
|
+
))}
|
|
45
|
+
</ul>
|
|
46
|
+
);
|
|
47
47
|
}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
If you'd like to listen to multiple events:
|
|
51
51
|
|
|
52
52
|
```tsx
|
|
53
|
-
import {
|
|
53
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
54
54
|
|
|
55
55
|
function App() {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
56
|
+
useEventStream("/stream", {
|
|
57
|
+
eventName: ["update", "create"],
|
|
58
|
+
onMessage: (event) => {
|
|
59
|
+
if (event.type === "update") {
|
|
60
|
+
// Handle update
|
|
61
|
+
} else {
|
|
62
|
+
// Handle create
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The second parameter is an options object where all properties are optional (defaults are shown below):
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
75
|
+
|
|
76
|
+
function App() {
|
|
77
|
+
const { message } = useEventStream("/stream", {
|
|
78
|
+
event: "update",
|
|
79
|
+
onMessage: (message) => {
|
|
80
|
+
//
|
|
81
|
+
},
|
|
82
|
+
onError: (error) => {
|
|
83
|
+
//
|
|
84
|
+
},
|
|
85
|
+
onComplete: () => {
|
|
86
|
+
//
|
|
87
|
+
},
|
|
88
|
+
endSignal: "</stream>",
|
|
89
|
+
glue: " ",
|
|
90
|
+
replace: false,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return <div>{message}</div>;
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
You can close the connection manually by using the returned `close` function:
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
101
|
+
import { useEffect } from "react";
|
|
102
|
+
|
|
103
|
+
function App() {
|
|
104
|
+
const { message, close } = useEventStream("/stream");
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
close();
|
|
109
|
+
}, 3000);
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
return <div>{message}</div>;
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The `clearMessage` function may be used to clear the message content that has been received so far:
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
120
|
+
import { useEffect } from "react";
|
|
121
|
+
|
|
122
|
+
function App() {
|
|
123
|
+
const { message, clearMessage } = useEventStream("/stream");
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
setTimeout(() => {
|
|
127
|
+
clearMessage();
|
|
128
|
+
}, 3000);
|
|
129
|
+
}, []);
|
|
130
|
+
|
|
131
|
+
return <div>{message}</div>;
|
|
72
132
|
}
|
|
73
133
|
```
|
|
74
134
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
declare type Options = {
|
|
2
|
-
eventName?: string;
|
|
2
|
+
eventName?: string | string[];
|
|
3
3
|
endSignal?: string;
|
|
4
4
|
glue?: string;
|
|
5
|
+
replace?: boolean;
|
|
5
6
|
onMessage?: (event: MessageEvent) => void;
|
|
6
7
|
onComplete?: () => void;
|
|
7
8
|
onError?: (error: Event) => void;
|
|
@@ -24,6 +25,6 @@ declare type StreamResult = {
|
|
|
24
25
|
*
|
|
25
26
|
* @returns StreamResult object containing the accumulated response, close, and reset functions
|
|
26
27
|
*/
|
|
27
|
-
export declare const
|
|
28
|
+
export declare const useEventStream: (url: string, { eventName, endSignal, glue, replace, onMessage, onComplete, onError, }?: Options) => StreamResult;
|
|
28
29
|
|
|
29
30
|
export { }
|
package/dist/index.es.js
CHANGED
|
@@ -1,38 +1,48 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
const
|
|
3
|
-
eventName:
|
|
4
|
-
endSignal:
|
|
5
|
-
glue:
|
|
1
|
+
import { useRef as y, useMemo as $, useState as A, useCallback as u, useEffect as j } from "react";
|
|
2
|
+
const f = "data: ", w = (d, {
|
|
3
|
+
eventName: s = "update",
|
|
4
|
+
endSignal: E = "</stream>",
|
|
5
|
+
glue: g = " ",
|
|
6
|
+
replace: L = !1,
|
|
6
7
|
onMessage: P = () => null,
|
|
7
8
|
onComplete: R = () => null,
|
|
8
9
|
onError: S = () => null
|
|
9
10
|
} = {}) => {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
const e = y(null), a = y([]), c = $(
|
|
12
|
+
() => Array.isArray(s) ? s : [s],
|
|
13
|
+
Array.isArray(s) ? s : [s]
|
|
14
|
+
), [b, h] = A(""), [x, m] = A([]), t = u(() => {
|
|
15
|
+
a.current = [], h(""), m([]);
|
|
16
|
+
}, []), l = u(
|
|
17
|
+
(r) => {
|
|
18
|
+
if ([E, `${f}${E}`].includes(r.data)) {
|
|
19
|
+
o(), R();
|
|
16
20
|
return;
|
|
17
21
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
),
|
|
22
|
+
L && t(), a.current.push(
|
|
23
|
+
r.data.startsWith(f) ? r.data.substring(f.length) : r.data
|
|
24
|
+
), h(a.current.join(g)), m(a.current), P(r);
|
|
21
25
|
},
|
|
22
|
-
[
|
|
23
|
-
),
|
|
24
|
-
S(
|
|
25
|
-
}, []),
|
|
26
|
-
var
|
|
27
|
-
|
|
26
|
+
[c, g]
|
|
27
|
+
), i = u((r) => {
|
|
28
|
+
S(r), o();
|
|
29
|
+
}, []), o = u((r = !1) => {
|
|
30
|
+
var n, M;
|
|
31
|
+
c.forEach((C) => {
|
|
32
|
+
var p;
|
|
33
|
+
(p = e.current) == null || p.removeEventListener(C, l);
|
|
34
|
+
}), (n = e.current) == null || n.removeEventListener("error", i), (M = e.current) == null || M.close(), e.current = null, r && t();
|
|
28
35
|
}, []);
|
|
29
|
-
return
|
|
36
|
+
return j(() => (t(), e.current = new EventSource(d), c.forEach((r) => {
|
|
37
|
+
var n;
|
|
38
|
+
(n = e.current) == null || n.addEventListener(r, l);
|
|
39
|
+
}), e.current.addEventListener("error", i), o), [d, c, l, i, t]), {
|
|
30
40
|
message: b,
|
|
31
41
|
messageParts: x,
|
|
32
|
-
close:
|
|
33
|
-
clearMessage:
|
|
42
|
+
close: o,
|
|
43
|
+
clearMessage: t
|
|
34
44
|
};
|
|
35
45
|
};
|
|
36
46
|
export {
|
|
37
|
-
|
|
47
|
+
w as useEventStream
|
|
38
48
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(t,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],e):(t=typeof globalThis<"u"?globalThis:t||self,e(t.LaravelStreamReact={},t.React))})(this,function(t,e){"use strict";const
|
|
1
|
+
(function(t,e){typeof exports=="object"&&typeof module<"u"?e(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],e):(t=typeof globalThis<"u"?globalThis:t||self,e(t.LaravelStreamReact={},t.React))})(this,function(t,e){"use strict";const l="data: ",b=(E,{eventName:n="update",endSignal:h="</stream>",glue:g=" ",replace:M=!1,onMessage:R=()=>null,onComplete:C=()=>null,onError:L=()=>null}={})=>{const r=e.useRef(null),o=e.useRef([]),c=e.useMemo(()=>Array.isArray(n)?n:[n],Array.isArray(n)?n:[n]),[P,m]=e.useState(""),[k,p]=e.useState([]),a=e.useCallback(()=>{o.current=[],m(""),p([])},[]),f=e.useCallback(s=>{if([h,`${l}${h}`].includes(s.data)){i(),C();return}M&&a(),o.current.push(s.data.startsWith(l)?s.data.substring(l.length):s.data),m(o.current.join(g)),p(o.current),R(s)},[c,g]),d=e.useCallback(s=>{L(s),i()},[]),i=e.useCallback((s=!1)=>{var u,y;c.forEach(A=>{var S;(S=r.current)==null||S.removeEventListener(A,f)}),(u=r.current)==null||u.removeEventListener("error",d),(y=r.current)==null||y.close(),r.current=null,s&&a()},[]);return e.useEffect(()=>(a(),r.current=new EventSource(E),c.forEach(s=>{var u;(u=r.current)==null||u.addEventListener(s,f)}),r.current.addEventListener("error",d),i),[E,c,f,d,a]),{message:P,messageParts:k,close:i,clearMessage:a}};t.useEventStream=b,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});
|