Skip to content

Commit 2f793fe

Browse files
author
何嘉悦
committed
feat: 新增 useRecord useSend useXStream 钩子函数和 XRequest 工具类
1 parent c74988e commit 2f793fe

File tree

8 files changed

+24
-39
lines changed

8 files changed

+24
-39
lines changed

apps/playground/components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ declare module 'vue' {
1515
ElAvatar: typeof import('element-plus/es')['ElAvatar']
1616
ElButton: typeof import('element-plus/es')['ElButton']
1717
ElIcon: typeof import('element-plus/es')['ElIcon']
18+
ElInputNumber: typeof import('element-plus/es')['ElInputNumber']
1819
ElTabPane: typeof import('element-plus/es')['ElTabPane']
1920
ElTabs: typeof import('element-plus/es')['ElTabs']
2021
HooksUseXStreamScenarioSIP: typeof import('./src/components/hooks-useXStream-scenarioSIP.vue')['default']

apps/playground/src/App.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import UseBubble from './components/demo-useBubble.vue'
3-
// import UseBubbleList from './components/demo-useBubbleList.vue'
3+
import UseBubbleList from './components/demo-useBubbleList.vue'
44
import UseSender from './components/demo-useSender.vue'
55
import ScenarioSIP from './components/hooks-useXStream-scenarioSIP.vue'
66
import ScenarioSSE from './components/hooks-useXStream-scenarioSSE.vue'
@@ -23,9 +23,10 @@ import UseRecord from './components/useRecord.vue'
2323
<el-tab-pane label="Bubble">
2424
<UseBubble />
2525
</el-tab-pane>
26-
<!-- <el-tab-pane label="BubbleList">
26+
27+
<el-tab-pane label="BubbleList">
2728
<UseBubbleList />
28-
</el-tab-pane> -->
29+
</el-tab-pane>
2930

3031
<el-tab-pane label="Sender">
3132
<UseSender />

apps/playground/src/components/hooks-useXStream-scenarioSIP.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { startStream, cancel, data, error, isLoading } = useXStream()
55
66
async function startSIPStream() {
77
try {
8-
const response = await fetch('http://localhost:3000/api/sip', {
8+
const response = await fetch('https://node-test.element-plus-x.com/api/sip', {
99
headers: { 'Content-Type': 'application/sip' },
1010
})
1111
const readableStream = response.body!

apps/playground/src/components/hooks-useXStream-scenarioSSE.vue

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<script setup lang="ts">
2-
import { useXStream } from 'vue-element-plus-x'
2+
import { useXStream } from 'vue-element-plus-x/src/hooks/useXStream'
33
44
const { startStream, cancel, data, error, isLoading } = useXStream()
55
66
async function startSSE() {
77
try {
8-
const response = await fetch('http://localhost:3000/api/sse', {
8+
const response = await fetch('https://node-test.element-plus-x.com/api/sse', {
99
headers: { 'Content-Type': 'text/event-stream' },
1010
})
1111
const readableStream = response.body!
@@ -16,7 +16,7 @@ async function startSSE() {
1616
}
1717
}
1818
19-
// 计算属性
19+
// 机器人的 content 计算属性
2020
const content = computed(() => {
2121
if (!data.value.length)
2222
return ''
@@ -41,10 +41,6 @@ const content = computed(() => {
4141
console.log('Text:', text)
4242
return text
4343
})
44-
45-
watch(() => data.value, () => {
46-
console.log('New data:', data.value)
47-
}, { deep: true })
4844
</script>
4945

5046
<template>

apps/playground/src/components/useRecord.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ watch(text, (val) => {
109109
display: flex;
110110
flex-direction: column;
111111
gap: 12px;
112+
padding: 12px;
113+
background: white;
114+
border-radius: 15px;
115+
112116
.btn-list {
113117
display: flex;
114118
gap: 12px;

apps/playground/src/components/useSend.vue

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,12 @@ const sse = new XRequest({
3535
3636
function startFn() {
3737
str.value = ''
38-
sse.send('/api/sip')
39-
// es.value = new EventSource('https://sse.dev/test')
40-
// es.value.onopen = () => {
41-
// console.log('onOpen')
42-
// }
43-
// es.value.onerror = () => {
44-
// clearEs()
45-
// }
46-
// es.value.onmessage = (e) => {
47-
// console.log('getData:', e.data)
48-
// const r = JSON.parse(e.data)
49-
// str.value += `\n${r.msg}`
50-
// }
38+
sse.send('/test')
5139
}
5240
41+
// useSend 的 abort 和 finish 是一样的方法。
42+
// 为了体现 这边 xrequest 请求,支持手动中断,和结束回调。
43+
// 所以 也在 useSend 中,也暴露了一个名字叫 finish 的方法。
5344
const { send, loading, abort, finish: _finish } = useSend({
5445
sendHandler: startFn,
5546
abortHandler: sse.abort,
@@ -63,11 +54,11 @@ setTimeout(() => {
6354
</script>
6455

6556
<template>
66-
<button v-if="!loading" @click="send">
67-
开始发送
68-
</button>
69-
<button v-else @click="abort">
70-
终止发送
71-
</button>
57+
<el-button v-if="!loading" @click="send">
58+
开始请求流式接口
59+
</el-button>
60+
<el-button v-else @click="abort">
61+
终止请求
62+
</el-button>
7263
<div>{{ str }}</div>
7364
</template>

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-element-plus-x",
33
"type": "module",
4-
"version": "1.0.6",
4+
"version": "1.0.7",
55
"license": "MIT",
66
"homepage": "https://element-plus-x.com",
77
"repository": {

packages/components/src/hooks/useXStream.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// 常量定义
2-
import { onUnmounted, ref, shallowRef } from 'vue'
3-
41
const DEFAULT_STREAM_SEPARATOR = '\n\n'
52
const DEFAULT_PART_SEPARATOR = '\n'
63
const DEFAULT_KV_SEPARATOR = ':'
@@ -152,11 +149,6 @@ export function useXStream() {
152149
}
153150
}
154151

155-
// 组件卸载时自动中断
156-
onUnmounted(() => {
157-
cancel()
158-
})
159-
160152
return {
161153
startStream,
162154
cancel, // 新增中断方法

0 commit comments

Comments
 (0)