@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 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 { 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>
@@ -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 { 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>
@@ -49,16 +49,14 @@ const { messageParts } = useStream("/stream");
49
49
  </template>
50
50
  ```
51
51
 
52
- The second parameter is options object, all properties are optional (defaults are shown here):
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 { useStream } from "@laravel/stream-vue";
56
+ import { useEventStream } from "@laravel/stream-vue";
57
57
 
58
- const { message } = useStream("/stream", {
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 useStream: (url: string, { eventName, endSignal, glue, onMessage, onComplete, onError, }?: Options) => StreamResult;
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 f } from "vue";
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: v = " ",
6
- onMessage: h = () => null,
7
- onComplete: E = () => null,
5
+ glue: f = " ",
6
+ onMessage: E = () => null,
7
+ onComplete: h = () => null,
8
8
  onError: p = () => null
9
9
  } = {}) => {
10
- const n = g(""), a = g([]);
10
+ const a = g(""), s = g([]);
11
11
  let e = null;
12
12
  const r = () => {
13
- n.value = "", a.value = [];
14
- }, s = (t = !1) => {
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
- s(), E();
18
+ n(), h();
19
19
  return;
20
20
  }
21
- a.value.push(
21
+ s.value.push(
22
22
  t.data.startsWith(o) ? t.data.substring(o.length) : t.data
23
- ), n.value = a.value.join(v), h(t);
23
+ ), a.value = s.value.join(f), E(t);
24
24
  }, c = (t) => {
25
- p(t), s();
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
- s();
32
+ n();
33
33
  }), w(
34
34
  () => l,
35
35
  (t, L) => {
36
- t !== L && (s(), m());
36
+ t !== L && (n(), m());
37
37
  }
38
38
  ), {
39
- message: f(n),
40
- messageParts: f(a),
41
- close: s,
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 useStream
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: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 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"})});
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.0",
4
+ "description": "Laravel streaming hooks for Vue",
5
5
  "keywords": [
6
6
  "laravel",
7
7
  "stream",