@laravel/stream-vue 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
@@ -21,13 +21,13 @@ Provide your stream URL and the hook will automatically update the `message` wit
21
21
 
22
22
  ```vue
23
23
  <script setup lang="ts">
24
- import { useStream } from "@laravel/stream-vue";
24
+ import { useEventStream } from "@laravel/stream-vue";
25
25
 
26
- const { message } = useStream("/stream");
26
+ const { message } = useEventStream("/stream");
27
27
  </script>
28
28
 
29
29
  <template>
30
- <div>{{ message }}</div>
30
+ <div>{{ message }}</div>
31
31
  </template>
32
32
  ```
33
33
 
@@ -35,43 +35,105 @@ You also have access to the array of message parts:
35
35
 
36
36
  ```vue
37
37
  <script setup lang="ts">
38
- import { useStream } from "@laravel/stream-vue";
38
+ import { useEventStream } from "@laravel/stream-vue";
39
39
 
40
- const { messageParts } = useStream("/stream");
40
+ const { messageParts } = useEventStream("/stream");
41
41
  </script>
42
42
 
43
43
  <template>
44
- <ul>
45
- <li v-for="message in messageParts">
46
- {{ message }}
47
- </li>
48
- </ul>
44
+ <ul>
45
+ <li v-for="message in messageParts">
46
+ {{ message }}
47
+ </li>
48
+ </ul>
49
49
  </template>
50
50
  ```
51
51
 
52
- The second parameter is options object, all properties are optional (defaults are shown here):
52
+ If you'd like to listen to multiple events:
53
53
 
54
54
  ```vue
55
55
  <script setup lang="ts">
56
- import { useStream } from "@laravel/stream-vue";
57
-
58
- const { message } = useStream("/stream", {
59
- event: "update",
60
- endSignal: "</stream>",
61
- glue: " ",
62
- onMessage: (message) => {
63
- //
64
- },
65
- onError: (error) => {
66
- //
67
- },
68
- onComplete: () => {
69
- //
70
- },
56
+ import { useEventStream } from "@laravel/stream-vue";
57
+
58
+ useEventStream("/stream", {
59
+ eventName: ["update", "create"],
60
+ onMessage: (event) => {
61
+ if (event.type === "update") {
62
+ // Handle update
63
+ } else {
64
+ // Handle create
65
+ }
66
+ },
71
67
  });
72
68
  </script>
73
69
  ```
74
70
 
71
+ The second parameter is an options object where all properties are optional (defaults are shown below):
72
+
73
+ ```vue
74
+ <script setup lang="ts">
75
+ import { useEventStream } from "@laravel/stream-vue";
76
+
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
+ </script>
93
+ ```
94
+
95
+ You can close the connection manually by using the returned `close` function:
96
+
97
+ ```vue
98
+ <script setup lang="ts">
99
+ import { useEventStream } from "@laravel/stream-vue";
100
+ import { onMounted } from "vue";
101
+
102
+ const { message, close } = useEventStream("/stream");
103
+
104
+ onMounted(() => {
105
+ setTimeout(() => {
106
+ close();
107
+ }, 3000);
108
+ });
109
+ </script>
110
+
111
+ <template>
112
+ <div>{{ message }}</div>
113
+ </template>
114
+ ```
115
+
116
+ The `clearMessage` function may be used to clear the message content that has been received so far:
117
+
118
+ ```vue
119
+ <script setup lang="ts">
120
+ import { useEventStream } from "@laravel/stream-vue";
121
+ import { onMounted } from "vue";
122
+
123
+ const { message, clearMessage } = useEventStream("/stream");
124
+
125
+ onMounted(() => {
126
+ setTimeout(() => {
127
+ clearMessage();
128
+ }, 3000);
129
+ });
130
+ </script>
131
+
132
+ <template>
133
+ <div>{{ message }}</div>
134
+ </template>
135
+ ```
136
+
75
137
  ## License
76
138
 
77
139
  Laravel Stream is open-sourced software licensed under the [MIT license](LICENSE.md).
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { Ref } from 'vue';
2
2
 
3
3
  declare type Options = {
4
- eventName?: string;
4
+ eventName?: string | string[];
5
5
  endSignal?: string;
6
6
  glue?: string;
7
+ replace?: boolean;
7
8
  onMessage?: (event: MessageEvent) => void;
8
9
  onComplete?: () => void;
9
10
  onError?: (error: Event) => void;
@@ -26,6 +27,6 @@ declare type StreamResult = {
26
27
  *
27
28
  * @returns StreamResult object containing the accumulated response, close, and reset functions
28
29
  */
29
- export declare const useStream: (url: string, { eventName, endSignal, glue, onMessage, onComplete, onError, }?: Options) => StreamResult;
30
+ export declare const useEventStream: (url: string, { eventName, endSignal, glue, replace, onMessage, onComplete, onError, }?: Options) => StreamResult;
30
31
 
31
32
  export { }
package/dist/index.es.js CHANGED
@@ -1,47 +1,52 @@
1
- import { ref as g, onMounted as M, onUnmounted as P, watch as w, readonly as f } from "vue";
2
- const o = "data: ", C = (l, {
3
- eventName: d = "update",
4
- endSignal: u = "</stream>",
5
- glue: v = " ",
6
- onMessage: h = () => null,
7
- onComplete: E = () => null,
8
- onError: p = () => null
1
+ import { ref as g, onMounted as P, onUnmounted as w, watch as x, readonly as h } from "vue";
2
+ const c = "data: ", C = (u, {
3
+ eventName: r = "update",
4
+ endSignal: d = "</stream>",
5
+ glue: E = " ",
6
+ replace: v = !1,
7
+ onMessage: p = () => null,
8
+ onComplete: M = () => null,
9
+ onError: y = () => null
9
10
  } = {}) => {
10
- const n = g(""), a = g([]);
11
- let e = null;
12
- const r = () => {
13
- n.value = "", a.value = [];
14
- }, s = (t = !1) => {
15
- e == null || e.removeEventListener(d, i), e == null || e.removeEventListener("error", c), e == null || e.close(), e = null, t && r();
16
- }, i = (t) => {
17
- if ([u, `${o}${u}`].includes(t.data)) {
18
- s(), E();
11
+ const o = g(""), a = g([]), i = Array.isArray(r) ? r : [r];
12
+ let s = null;
13
+ const n = () => {
14
+ o.value = "", a.value = [];
15
+ }, t = (e = !1) => {
16
+ i.forEach((l) => {
17
+ s == null || s.removeEventListener(l, f);
18
+ }), s == null || s.close(), s = null, e && n();
19
+ }, f = (e) => {
20
+ if ([d, `${c}${d}`].includes(e.data)) {
21
+ t(), M();
19
22
  return;
20
23
  }
21
- a.value.push(
22
- t.data.startsWith(o) ? t.data.substring(o.length) : t.data
23
- ), n.value = a.value.join(v), h(t);
24
- }, c = (t) => {
25
- p(t), s();
24
+ v && n(), a.value.push(
25
+ e.data.startsWith(c) ? e.data.substring(c.length) : e.data
26
+ ), o.value = a.value.join(E), p(e);
27
+ }, L = (e) => {
28
+ y(e), t();
26
29
  }, m = () => {
27
- r(), e = new EventSource(l), e.addEventListener(d, i), e.addEventListener("error", c);
30
+ n(), s = new EventSource(u), i.forEach((e) => {
31
+ s.addEventListener(e, f);
32
+ }), s.addEventListener("error", L);
28
33
  };
29
- return M(() => {
34
+ return P(() => {
30
35
  m();
31
- }), P(() => {
32
- s();
33
- }), w(
34
- () => l,
35
- (t, L) => {
36
- t !== L && (s(), m());
36
+ }), w(() => {
37
+ t();
38
+ }), x(
39
+ () => u,
40
+ (e, l) => {
41
+ e !== l && (t(), m());
37
42
  }
38
43
  ), {
39
- message: f(n),
40
- messageParts: f(a),
41
- close: s,
42
- clearMessage: r
44
+ message: h(o),
45
+ messageParts: h(a),
46
+ close: t,
47
+ clearMessage: n
43
48
  };
44
49
  };
45
50
  export {
46
- C as useStream
51
+ C as useEventStream
47
52
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(s,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],t):(s=typeof globalThis<"u"?globalThis:s||self,t(s.LaravelStreamVue={},s.Vue))})(this,function(s,t){"use strict";const r="data: ",g=(i,{eventName:u="update",endSignal:f="</stream>",glue:h=" ",onMessage:y=()=>null,onComplete:E=()=>null,onError:S=()=>null}={})=>{const l=t.ref(""),o=t.ref([]);let e=null;const d=()=>{l.value="",o.value=[]},a=(n=!1)=>{e==null||e.removeEventListener(u,c),e==null||e.removeEventListener("error",m),e==null||e.close(),e=null,n&&d()},c=n=>{if([f,`${r}${f}`].includes(n.data)){a(),E();return}o.value.push(n.data.startsWith(r)?n.data.substring(r.length):n.data),l.value=o.value.join(h),y(n)},m=n=>{S(n),a()},p=()=>{d(),e=new EventSource(i),e.addEventListener(u,c),e.addEventListener("error",m)};return t.onMounted(()=>{p()}),t.onUnmounted(()=>{a()}),t.watch(()=>i,(n,v)=>{n!==v&&(a(),p())}),{message:t.readonly(l),messageParts:t.readonly(o),close:a,clearMessage:d}};s.useStream=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
1
+ (function(s,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],t):(s=typeof globalThis<"u"?globalThis:s||self,t(s.LaravelStreamVue={},s.Vue))})(this,function(s,t){"use strict";const l="data: ",g=(f,{eventName:i="update",endSignal:c="</stream>",glue:E=" ",replace:y=!1,onMessage:S=()=>null,onComplete:v=()=>null,onError:M=()=>null}={})=>{const d=t.ref(""),o=t.ref([]),m=Array.isArray(i)?i:[i];let n=null;const r=()=>{d.value="",o.value=[]},a=(e=!1)=>{m.forEach(u=>{n==null||n.removeEventListener(u,h)}),n==null||n.close(),n=null,e&&r()},h=e=>{if([c,`${l}${c}`].includes(e.data)){a(),v();return}y&&r(),o.value.push(e.data.startsWith(l)?e.data.substring(l.length):e.data),d.value=o.value.join(E),S(e)},L=e=>{M(e),a()},p=()=>{r(),n=new EventSource(f),m.forEach(e=>{n.addEventListener(e,h)}),n.addEventListener("error",L)};return t.onMounted(()=>{p()}),t.onUnmounted(()=>{a()}),t.watch(()=>f,(e,u)=>{e!==u&&(a(),p())}),{message:t.readonly(d),messageParts:t.readonly(o),close:a,clearMessage:r}};s.useEventStream=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@laravel/stream-vue",
3
- "version": "0.1.0",
4
- "description": "The Laravel useStream hook for Vue",
3
+ "version": "0.2.1",
4
+ "description": "Laravel streaming hooks for Vue",
5
5
  "keywords": [
6
6
  "laravel",
7
7
  "stream",