@laravel/stream-vue 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
@@ -27,7 +27,7 @@ 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
 
@@ -41,14 +41,33 @@ 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
+ If you'd like to listen to multiple events:
53
+
54
+ ```vue
55
+ <script setup lang="ts">
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
+ },
67
+ });
68
+ </script>
69
+ ```
70
+
52
71
  The second parameter is an options object where all properties are optional (defaults are shown below):
53
72
 
54
73
  ```vue
@@ -56,18 +75,19 @@ The second parameter is an options object where all properties are optional (def
56
75
  import { useEventStream } from "@laravel/stream-vue";
57
76
 
58
77
  const { message } = useEventStream("/stream", {
59
- event: "update",
60
- onMessage: (message) => {
61
- //
62
- },
63
- onError: (error) => {
64
- //
65
- },
66
- onComplete: () => {
67
- //
68
- },
69
- endSignal: "</stream>",
70
- glue: " ",
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,
71
91
  });
72
92
  </script>
73
93
  ```
@@ -82,14 +102,14 @@ import { onMounted } from "vue";
82
102
  const { message, close } = useEventStream("/stream");
83
103
 
84
104
  onMounted(() => {
85
- setTimeout(() => {
86
- close();
87
- }, 3000);
105
+ setTimeout(() => {
106
+ close();
107
+ }, 3000);
88
108
  });
89
109
  </script>
90
110
 
91
111
  <template>
92
- <div>{{ message }}</div>
112
+ <div>{{ message }}</div>
93
113
  </template>
94
114
  ```
95
115
 
@@ -103,14 +123,14 @@ import { onMounted } from "vue";
103
123
  const { message, clearMessage } = useEventStream("/stream");
104
124
 
105
125
  onMounted(() => {
106
- setTimeout(() => {
107
- clearMessage();
108
- }, 3000);
126
+ setTimeout(() => {
127
+ clearMessage();
128
+ }, 3000);
109
129
  });
110
130
  </script>
111
131
 
112
132
  <template>
113
- <div>{{ message }}</div>
133
+ <div>{{ message }}</div>
114
134
  </template>
115
135
  ```
116
136
 
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 useEventStream: (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,45 +1,50 @@
1
- import { ref as g, onMounted as M, onUnmounted as P, watch as w, readonly as v } from "vue";
2
- const o = "data: ", C = (l, {
3
- eventName: d = "update",
4
- endSignal: u = "</stream>",
5
- glue: f = " ",
6
- onMessage: E = () => null,
7
- onComplete: h = () => 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 a = g(""), s = g([]);
11
- let e = null;
12
- const r = () => {
13
- a.value = "", s.value = [];
14
- }, n = (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
- n(), h();
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
- s.value.push(
22
- t.data.startsWith(o) ? t.data.substring(o.length) : t.data
23
- ), a.value = s.value.join(f), E(t);
24
- }, c = (t) => {
25
- p(t), n();
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
- n();
33
- }), w(
34
- () => l,
35
- (t, L) => {
36
- t !== L && (n(), m());
36
+ }), w(() => {
37
+ t();
38
+ }), x(
39
+ () => u,
40
+ (e, l) => {
41
+ e !== l && (t(), m());
37
42
  }
38
43
  ), {
39
- message: v(a),
40
- messageParts: v(s),
41
- close: n,
42
- clearMessage: r
44
+ message: h(o),
45
+ messageParts: h(a),
46
+ close: t,
47
+ clearMessage: n
43
48
  };
44
49
  };
45
50
  export {
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:E=()=>null,onComplete:v=()=>null,onError:y=()=>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(),v();return}o.value.push(n.data.startsWith(r)?n.data.substring(r.length):n.data),l.value=o.value.join(h),E(n)},m=n=>{y(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,S)=>{n!==S&&(a(),p())}),{message:t.readonly(l),messageParts:t.readonly(o),close:a,clearMessage:d}};s.useEventStream=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,6 +1,6 @@
1
1
  {
2
2
  "name": "@laravel/stream-vue",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Laravel streaming hooks for Vue",
5
5
  "keywords": [
6
6
  "laravel",