using stream_fetch in App

This commit is contained in:
lloydzhou 2024-09-29 20:32:36 +08:00
parent dd77ad5d74
commit 9e6ee50fa6
2 changed files with 52 additions and 62 deletions

View File

@ -12,30 +12,18 @@ type ResponseEvent = {
}; };
}; };
export function fetch(url: string, options?: RequestInit): Promise<any> { type StreamResponse = {
if (window.__TAURI__) {
const { signal, method = "GET", headers = {}, body = [] } = options || {};
return window.__TAURI__
.invoke("stream_fetch", {
method,
url,
headers,
// TODO FormData
body:
typeof body === "string"
? Array.from(new TextEncoder().encode(body))
: [],
})
.then(
(res: {
request_id: number; request_id: number;
status: number; status: number;
status_text: string; status_text: string;
headers: Record<string, string>; headers: Record<string, string>;
}) => { };
const { request_id, status, status_text: statusText, headers } = res;
console.log("send request_id", request_id, status, statusText); export function fetch(url: string, options?: RequestInit): Promise<any> {
if (window.__TAURI__) {
const { signal, method = "GET", headers = {}, body = [] } = options || {};
let unlisten: Function | undefined; let unlisten: Function | undefined;
let request_id = 0;
const ts = new TransformStream(); const ts = new TransformStream();
const writer = ts.writable.getWriter(); const writer = ts.writable.getWriter();
@ -51,19 +39,13 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
}); });
}; };
const response = new Response(ts.readable, {
status,
statusText,
headers,
});
if (signal) { if (signal) {
signal.addEventListener("abort", () => close()); signal.addEventListener("abort", () => close());
} }
// @ts-ignore 2. listen response multi times, and write to Response.body // @ts-ignore 2. listen response multi times, and write to Response.body
window.__TAURI__.event window.__TAURI__.event
.listen("stream-response", (e: ResponseEvent) => { .listen("stream-response", (e: ResponseEvent) => {
const { id, payload } = e; const { request_id: rid, chunk, status } = e?.payload || {};
const { request_id: rid, chunk, status } = payload;
if (request_id != rid) { if (request_id != rid) {
return; return;
} }
@ -78,9 +60,23 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
} }
}) })
.then((u: Function) => (unlisten = u)); .then((u: Function) => (unlisten = u));
return response;
}, return window.__TAURI__
) .invoke("stream_fetch", {
method,
url,
headers,
// TODO FormData
body:
typeof body === "string"
? Array.from(new TextEncoder().encode(body))
: [],
})
.then((res: StreamResponse) => {
request_id = res.request_id;
const { status, status_text: statusText, headers } = res;
return new Response(ts.readable, { status, statusText, headers });
})
.catch((e) => { .catch((e) => {
console.error("stream error", e); console.error("stream error", e);
throw e; throw e;
@ -88,7 +84,3 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
} }
return window.fetch(url, options); return window.fetch(url, options);
} }
if (undefined !== window) {
window.tauriFetch = fetch;
}

View File

@ -66,9 +66,7 @@ pub async fn stream_fetch(
let res = response_future.await; let res = response_future.await;
let response = match res { let response = match res {
Ok(res) => { Ok(res) => {
println!("Error: {:?}", res);
// get response and emit to client // get response and emit to client
// .register_uri_scheme_protocol("stream", move |app_handle, request| {
let mut headers = HashMap::new(); let mut headers = HashMap::new();
for (name, value) in res.headers() { for (name, value) in res.headers() {
headers.insert( headers.insert(
@ -84,7 +82,7 @@ pub async fn stream_fetch(
while let Some(chunk) = stream.next().await { while let Some(chunk) = stream.next().await {
match chunk { match chunk {
Ok(bytes) => { Ok(bytes) => {
println!("chunk: {:?}", bytes); // println!("chunk: {:?}", bytes);
window.emit(event_name, ChunkPayload{ request_id, chunk: bytes }).unwrap(); window.emit(event_name, ChunkPayload{ request_id, chunk: bytes }).unwrap();
} }
Err(err) => { Err(err) => {