@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 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
- const { message } = useEventStream("/stream");
26
+ const { message } = useEventStream("/stream");
27
27
 
28
- return <div>{message}</div>;
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
- const { messageParts } = useEventStream("/stream");
39
-
40
- return (
41
- <ul>
42
- {messageParts.forEach((message) => (
43
- <li>{message}</li>
44
- ))}
45
- </ul>
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
- const { message } = useEventStream("/stream", {
57
- event: "update",
58
- onMessage: (message) => {
59
- //
60
- },
61
- onError: (error) => {
62
- //
63
- },
64
- onComplete: () => {
65
- //
66
- },
67
- endSignal: "</stream>",
68
- glue: " ",
69
- });
70
-
71
- return <div>{message}</div>;
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
- const { message, close } = useEventStream("/stream");
104
+ const { message, close } = useEventStream("/stream");
83
105
 
84
- useEffect(() => {
85
- setTimeout(() => {
86
- close();
87
- }, 3000);
88
- }, []);
106
+ useEffect(() => {
107
+ setTimeout(() => {
108
+ close();
109
+ }, 3000);
110
+ }, []);
89
111
 
90
- return <div>{message}</div>;
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
- const { message, clearMessage } = useEventStream("/stream");
123
+ const { message, clearMessage } = useEventStream("/stream");
102
124
 
103
- useEffect(() => {
104
- setTimeout(() => {
105
- clearMessage();
106
- }, 3000);
107
- }, []);
125
+ useEffect(() => {
126
+ setTimeout(() => {
127
+ clearMessage();
128
+ }, 3000);
129
+ }, []);
108
130
 
109
- return <div>{message}</div>;
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 p, useState as L, useCallback as c, useEffect as C } from "react";
2
- const l = "data: ", j = (d, {
3
- eventName: t = "update",
4
- endSignal: i = "</stream>",
5
- glue: f = " ",
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 r = p(null), s = p([]), [b, g] = L(""), [x, E] = L([]), n = c(() => {
11
- s.current = [], g(""), E([]);
12
- }, []), u = c(
13
- (e) => {
14
- if ([i, `${l}${i}`].includes(e.data)) {
15
- a(), R();
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
- s.current.push(
19
- e.data.startsWith(l) ? e.data.substring(l.length) : e.data
20
- ), g(s.current.join(f)), E(s.current), P(e);
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
- [t, f]
23
- ), o = c((e) => {
24
- S(e), a();
25
- }, []), a = c((e = !1) => {
26
- var m, h, M;
27
- (m = r.current) == null || m.removeEventListener(t, u), (h = r.current) == null || h.removeEventListener("error", o), (M = r.current) == null || M.close(), r.current = null, e && n();
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 C(() => (n(), r.current = new EventSource(d), r.current.addEventListener(t, u), r.current.addEventListener("error", o), a), [d, t, u, o, n]), {
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: a,
33
- clearMessage: n
42
+ close: o,
43
+ clearMessage: t
34
44
  };
35
45
  };
36
46
  export {
37
- j as useEventStream
47
+ w as useEventStream
38
48
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
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"})});
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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@laravel/stream-react",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Laravel streaming hooks for React",
5
5
  "keywords": [
6
6
  "laravel",