@laravel/stream-react 0.2.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 +63 -41
- package/dist/index.d.ts +3 -2
- package/dist/index.es.js +34 -24
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,9 +23,9 @@ Provide your stream URL and the hook will automatically update the `message` wit
|
|
|
23
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
|
|
|
@@ -35,15 +35,36 @@ You also have access to the array of message parts:
|
|
|
35
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
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
If you'd like to listen to multiple events:
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { useEventStream } from "@laravel/stream-react";
|
|
54
|
+
|
|
55
|
+
function App() {
|
|
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;
|
|
47
68
|
}
|
|
48
69
|
```
|
|
49
70
|
|
|
@@ -53,22 +74,23 @@ The second parameter is an options object where all properties are optional (def
|
|
|
53
74
|
import { useEventStream } from "@laravel/stream-react";
|
|
54
75
|
|
|
55
76
|
function App() {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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>;
|
|
72
94
|
}
|
|
73
95
|
```
|
|
74
96
|
|
|
@@ -79,15 +101,15 @@ import { useEventStream } from "@laravel/stream-react";
|
|
|
79
101
|
import { useEffect } from "react";
|
|
80
102
|
|
|
81
103
|
function App() {
|
|
82
|
-
|
|
104
|
+
const { message, close } = useEventStream("/stream");
|
|
83
105
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
close();
|
|
109
|
+
}, 3000);
|
|
110
|
+
}, []);
|
|
89
111
|
|
|
90
|
-
|
|
112
|
+
return <div>{message}</div>;
|
|
91
113
|
}
|
|
92
114
|
```
|
|
93
115
|
|
|
@@ -98,15 +120,15 @@ import { useEventStream } from "@laravel/stream-react";
|
|
|
98
120
|
import { useEffect } from "react";
|
|
99
121
|
|
|
100
122
|
function App() {
|
|
101
|
-
|
|
123
|
+
const { message, clearMessage } = useEventStream("/stream");
|
|
102
124
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
setTimeout(() => {
|
|
127
|
+
clearMessage();
|
|
128
|
+
}, 3000);
|
|
129
|
+
}, []);
|
|
108
130
|
|
|
109
|
-
|
|
131
|
+
return <div>{message}</div>;
|
|
110
132
|
}
|
|
111
133
|
```
|
|
112
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 useEventStream: (url: string, { eventName, endSignal, glue, onMessage, onComplete, onError, }?: Options) => StreamResult;
|
|
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(
|
|
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"})});
|