wangzhibo
6 天以前 da0230346106bc810664488a2b152bc24f853a44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { useBase } from '/$/base';
import { config } from '/@/config';
 
export function useStream() {
    const { user } = useBase();
    let abortController: AbortController | null = null;
 
    // 调用
    async function invoke({
        url,
        method = 'POST',
        data,
        cb
    }: {
        url: string;
        method?: string;
        data?: any;
        cb?: (result: any) => void;
    }) {
        abortController = new AbortController();
 
        let cacheText = '';
 
        return fetch(config.baseUrl + url, {
            method,
            headers: {
                Authorization: user.token,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data),
            signal: abortController?.signal
        })
            .then(res => {
                if (res.body) {
                    const reader = res.body.getReader();
                    const decoder = new TextDecoder('utf-8');
                    const stream = new ReadableStream({
                        start(controller) {
                            function push() {
                                reader.read().then(({ done, value }) => {
                                    if (done) {
                                        controller.close();
                                        return;
                                    }
 
                                    let text = decoder.decode(value, { stream: true });
 
                                    if (cb) {
                                        if (cacheText) {
                                            text = cacheText + text;
                                        }
 
                                        if (text.indexOf('data:') == 0) {
                                            text = '\n\n' + text;
                                        }
 
                                        try {
                                            const arr = text
                                                .split(/\n\ndata:/g)
                                                .filter(Boolean)
                                                .map(e => JSON.parse(e));
 
                                            arr.forEach(cb);
 
                                            cacheText = '';
                                        } catch (err) {
                                            console.error('[parse text]', text);
                                            cacheText = text;
                                        }
                                    }
 
                                    controller.enqueue(text);
                                    push();
                                });
                            }
                            push();
                        }
                    });
 
                    return new Response(stream);
                }
 
                return res;
            })
            .catch(err => {
                console.error(err);
                throw err;
            });
    }
 
    // 取消
    function cancel() {
        if (abortController) {
            abortController.abort();
            abortController = null;
        }
    }
 
    return {
        invoke,
        cancel
    };
}