@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 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 { useStream } from "@laravel/stream-react";
23
+ import { useEventStream } from "@laravel/stream-react";
24
24
 
25
25
  function App() {
26
- const { message } = useStream("/stream");
26
+ const { message } = useEventStream("/stream");
27
27
 
28
- return <div>{message}</div>;
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 { useStream } from "@laravel/stream-react";
35
+ import { useEventStream } from "@laravel/stream-react";
36
36
 
37
37
  function App() {
38
- const { messageParts } = useStream("/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
47
  }
48
48
  ```
49
49
 
50
- The second parameter is options object, all properties are optional (defaults are shown here):
50
+ If you'd like to listen to multiple events:
51
51
 
52
52
  ```tsx
53
- import { useStream } from "@laravel/stream-react";
53
+ import { useEventStream } from "@laravel/stream-react";
54
54
 
55
55
  function App() {
56
- const { message } = useStream("/stream", {
57
- event: "update",
58
- endSignal: "</stream>",
59
- glue: " ",
60
- onMessage: (message) => {
61
- //
62
- },
63
- onError: (error) => {
64
- //
65
- },
66
- onComplete: () => {
67
- //
68
- },
69
- });
70
-
71
- return <div>{message}</div>;
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 useStream: (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 useStream
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 c="data: ",E=(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(s=>{if([f,`${c}${f}`].includes(s.data)){o(),R();return}u.current.push(s.data.startsWith(c)?s.data.substring(c.length):s.data),m(u.current.join(g)),p(u.current),M(s)},[n,g]),i=e.useCallback(s=>{C(s),o()},[]),o=e.useCallback((s=!1)=>{var h,S,b;(h=r.current)==null||h.removeEventListener(n,l),(S=r.current)==null||S.removeEventListener("error",i),(b=r.current)==null||b.close(),r.current=null,s&&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}};t.useStream=E,Object.defineProperty(t,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,7 +1,7 @@
1
1
  {
2
2
  "name": "@laravel/stream-react",
3
- "version": "0.1.0",
4
- "description": "The Laravel useStream hook for React",
3
+ "version": "0.2.1",
4
+ "description": "Laravel streaming hooks for React",
5
5
  "keywords": [
6
6
  "laravel",
7
7
  "stream",