@laravel/stream-vue 0.1.0 → 0.2.0
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 +51 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.es.js +17 -17
- package/dist/index.umd.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -21,9 +21,9 @@ 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 {
|
|
24
|
+
import { useEventStream } from "@laravel/stream-vue";
|
|
25
25
|
|
|
26
|
-
const { message } =
|
|
26
|
+
const { message } = useEventStream("/stream");
|
|
27
27
|
</script>
|
|
28
28
|
|
|
29
29
|
<template>
|
|
@@ -35,9 +35,9 @@ You also have access to the array of message parts:
|
|
|
35
35
|
|
|
36
36
|
```vue
|
|
37
37
|
<script setup lang="ts">
|
|
38
|
-
import {
|
|
38
|
+
import { useEventStream } from "@laravel/stream-vue";
|
|
39
39
|
|
|
40
|
-
const { messageParts } =
|
|
40
|
+
const { messageParts } = useEventStream("/stream");
|
|
41
41
|
</script>
|
|
42
42
|
|
|
43
43
|
<template>
|
|
@@ -49,16 +49,14 @@ const { messageParts } = useStream("/stream");
|
|
|
49
49
|
</template>
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
The second parameter is options object
|
|
52
|
+
The second parameter is an options object where all properties are optional (defaults are shown below):
|
|
53
53
|
|
|
54
54
|
```vue
|
|
55
55
|
<script setup lang="ts">
|
|
56
|
-
import {
|
|
56
|
+
import { useEventStream } from "@laravel/stream-vue";
|
|
57
57
|
|
|
58
|
-
const { message } =
|
|
58
|
+
const { message } = useEventStream("/stream", {
|
|
59
59
|
event: "update",
|
|
60
|
-
endSignal: "</stream>",
|
|
61
|
-
glue: " ",
|
|
62
60
|
onMessage: (message) => {
|
|
63
61
|
//
|
|
64
62
|
},
|
|
@@ -68,8 +66,52 @@ const { message } = useStream("/stream", {
|
|
|
68
66
|
onComplete: () => {
|
|
69
67
|
//
|
|
70
68
|
},
|
|
69
|
+
endSignal: "</stream>",
|
|
70
|
+
glue: " ",
|
|
71
|
+
});
|
|
72
|
+
</script>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
You can close the connection manually by using the returned `close` function:
|
|
76
|
+
|
|
77
|
+
```vue
|
|
78
|
+
<script setup lang="ts">
|
|
79
|
+
import { useEventStream } from "@laravel/stream-vue";
|
|
80
|
+
import { onMounted } from "vue";
|
|
81
|
+
|
|
82
|
+
const { message, close } = useEventStream("/stream");
|
|
83
|
+
|
|
84
|
+
onMounted(() => {
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
close();
|
|
87
|
+
}, 3000);
|
|
88
|
+
});
|
|
89
|
+
</script>
|
|
90
|
+
|
|
91
|
+
<template>
|
|
92
|
+
<div>{{ message }}</div>
|
|
93
|
+
</template>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The `clearMessage` function may be used to clear the message content that has been received so far:
|
|
97
|
+
|
|
98
|
+
```vue
|
|
99
|
+
<script setup lang="ts">
|
|
100
|
+
import { useEventStream } from "@laravel/stream-vue";
|
|
101
|
+
import { onMounted } from "vue";
|
|
102
|
+
|
|
103
|
+
const { message, clearMessage } = useEventStream("/stream");
|
|
104
|
+
|
|
105
|
+
onMounted(() => {
|
|
106
|
+
setTimeout(() => {
|
|
107
|
+
clearMessage();
|
|
108
|
+
}, 3000);
|
|
71
109
|
});
|
|
72
110
|
</script>
|
|
111
|
+
|
|
112
|
+
<template>
|
|
113
|
+
<div>{{ message }}</div>
|
|
114
|
+
</template>
|
|
73
115
|
```
|
|
74
116
|
|
|
75
117
|
## License
|
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,6 @@ declare type StreamResult = {
|
|
|
26
26
|
*
|
|
27
27
|
* @returns StreamResult object containing the accumulated response, close, and reset functions
|
|
28
28
|
*/
|
|
29
|
-
export declare const
|
|
29
|
+
export declare const useEventStream: (url: string, { eventName, endSignal, glue, onMessage, onComplete, onError, }?: Options) => StreamResult;
|
|
30
30
|
|
|
31
31
|
export { }
|
package/dist/index.es.js
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import { ref as g, onMounted as M, onUnmounted as P, watch as w, readonly as
|
|
1
|
+
import { ref as g, onMounted as M, onUnmounted as P, watch as w, readonly as v } from "vue";
|
|
2
2
|
const o = "data: ", C = (l, {
|
|
3
3
|
eventName: d = "update",
|
|
4
4
|
endSignal: u = "</stream>",
|
|
5
|
-
glue:
|
|
6
|
-
onMessage:
|
|
7
|
-
onComplete:
|
|
5
|
+
glue: f = " ",
|
|
6
|
+
onMessage: E = () => null,
|
|
7
|
+
onComplete: h = () => null,
|
|
8
8
|
onError: p = () => null
|
|
9
9
|
} = {}) => {
|
|
10
|
-
const
|
|
10
|
+
const a = g(""), s = g([]);
|
|
11
11
|
let e = null;
|
|
12
12
|
const r = () => {
|
|
13
|
-
|
|
14
|
-
},
|
|
13
|
+
a.value = "", s.value = [];
|
|
14
|
+
}, n = (t = !1) => {
|
|
15
15
|
e == null || e.removeEventListener(d, i), e == null || e.removeEventListener("error", c), e == null || e.close(), e = null, t && r();
|
|
16
16
|
}, i = (t) => {
|
|
17
17
|
if ([u, `${o}${u}`].includes(t.data)) {
|
|
18
|
-
|
|
18
|
+
n(), h();
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
s.value.push(
|
|
22
22
|
t.data.startsWith(o) ? t.data.substring(o.length) : t.data
|
|
23
|
-
),
|
|
23
|
+
), a.value = s.value.join(f), E(t);
|
|
24
24
|
}, c = (t) => {
|
|
25
|
-
p(t),
|
|
25
|
+
p(t), n();
|
|
26
26
|
}, m = () => {
|
|
27
27
|
r(), e = new EventSource(l), e.addEventListener(d, i), e.addEventListener("error", c);
|
|
28
28
|
};
|
|
29
29
|
return M(() => {
|
|
30
30
|
m();
|
|
31
31
|
}), P(() => {
|
|
32
|
-
|
|
32
|
+
n();
|
|
33
33
|
}), w(
|
|
34
34
|
() => l,
|
|
35
35
|
(t, L) => {
|
|
36
|
-
t !== L && (
|
|
36
|
+
t !== L && (n(), m());
|
|
37
37
|
}
|
|
38
38
|
), {
|
|
39
|
-
message:
|
|
40
|
-
messageParts:
|
|
41
|
-
close:
|
|
39
|
+
message: v(a),
|
|
40
|
+
messageParts: v(s),
|
|
41
|
+
close: n,
|
|
42
42
|
clearMessage: r
|
|
43
43
|
};
|
|
44
44
|
};
|
|
45
45
|
export {
|
|
46
|
-
C as
|
|
46
|
+
C as useEventStream
|
|
47
47
|
};
|
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:
|
|
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"})});
|