@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 +46 -26
- package/dist/index.d.ts +3 -2
- package/dist/index.es.js +39 -34
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ const { message } = useEventStream("/stream");
|
|
|
27
27
|
</script>
|
|
28
28
|
|
|
29
29
|
<template>
|
|
30
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
105
|
+
setTimeout(() => {
|
|
106
|
+
close();
|
|
107
|
+
}, 3000);
|
|
88
108
|
});
|
|
89
109
|
</script>
|
|
90
110
|
|
|
91
111
|
<template>
|
|
92
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
126
|
+
setTimeout(() => {
|
|
127
|
+
clearMessage();
|
|
128
|
+
}, 3000);
|
|
109
129
|
});
|
|
110
130
|
</script>
|
|
111
131
|
|
|
112
132
|
<template>
|
|
113
|
-
|
|
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
|
|
2
|
-
const
|
|
3
|
-
eventName:
|
|
4
|
-
endSignal:
|
|
5
|
-
glue:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
11
|
-
let
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
),
|
|
24
|
-
},
|
|
25
|
-
|
|
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
|
-
|
|
30
|
+
n(), s = new EventSource(u), i.forEach((e) => {
|
|
31
|
+
s.addEventListener(e, f);
|
|
32
|
+
}), s.addEventListener("error", L);
|
|
28
33
|
};
|
|
29
|
-
return
|
|
34
|
+
return P(() => {
|
|
30
35
|
m();
|
|
31
|
-
}),
|
|
32
|
-
|
|
33
|
-
}),
|
|
34
|
-
() =>
|
|
35
|
-
(
|
|
36
|
-
|
|
36
|
+
}), w(() => {
|
|
37
|
+
t();
|
|
38
|
+
}), x(
|
|
39
|
+
() => u,
|
|
40
|
+
(e, l) => {
|
|
41
|
+
e !== l && (t(), m());
|
|
37
42
|
}
|
|
38
43
|
), {
|
|
39
|
-
message:
|
|
40
|
-
messageParts:
|
|
41
|
-
close:
|
|
42
|
-
clearMessage:
|
|
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
|
|
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"})});
|