mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-08 16:07:50 +02:00
refactor(http): migrate to tauri's new resource table (#834)
* refactor(http): migrate to tauri's new resource table * fmt * change file
This commit is contained in:
@@ -6,12 +6,7 @@
|
||||
//!
|
||||
//! Access the HTTP client written in Rust.
|
||||
|
||||
use std::sync::atomic::AtomicU32;
|
||||
use std::{collections::HashMap, future::Future, pin::Pin};
|
||||
|
||||
pub use reqwest;
|
||||
use reqwest::Response;
|
||||
use tauri::async_runtime::Mutex;
|
||||
use tauri::{
|
||||
plugin::{Builder, TauriPlugin},
|
||||
AppHandle, Manager, Runtime,
|
||||
@@ -25,34 +20,10 @@ mod config;
|
||||
mod error;
|
||||
mod scope;
|
||||
|
||||
type RequestId = u32;
|
||||
type CancelableResponseResult = Result<Result<reqwest::Response>>;
|
||||
type CancelableResponseFuture =
|
||||
Pin<Box<dyn Future<Output = CancelableResponseResult> + Send + Sync>>;
|
||||
type RequestTable = HashMap<RequestId, FetchRequest>;
|
||||
type ResponseTable = HashMap<RequestId, Response>;
|
||||
|
||||
struct FetchRequest(Mutex<CancelableResponseFuture>);
|
||||
impl FetchRequest {
|
||||
fn new(f: CancelableResponseFuture) -> Self {
|
||||
Self(Mutex::new(f))
|
||||
}
|
||||
}
|
||||
|
||||
struct Http<R: Runtime> {
|
||||
#[allow(dead_code)]
|
||||
app: AppHandle<R>,
|
||||
scope: scope::Scope,
|
||||
current_id: AtomicU32,
|
||||
requests: Mutex<RequestTable>,
|
||||
responses: Mutex<ResponseTable>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Http<R> {
|
||||
fn next_id(&self) -> RequestId {
|
||||
self.current_id
|
||||
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
trait HttpExt<R: Runtime> {
|
||||
@@ -78,9 +49,6 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<Config>> {
|
||||
let default_scope = HttpAllowlistScope::default();
|
||||
app.manage(Http {
|
||||
app: app.clone(),
|
||||
current_id: 0.into(),
|
||||
requests: Default::default(),
|
||||
responses: Default::default(),
|
||||
scope: scope::Scope::new(
|
||||
api.config()
|
||||
.as_ref()
|
||||
|
||||
Reference in New Issue
Block a user