Skip to content

Commit

Permalink
load_available_volts: 1. Add proxy and retry count. (lapce#3401)
Browse files Browse the repository at this point in the history
2. Display error message after request failure.
  • Loading branch information
jm-observer authored Aug 5, 2024
1 parent 9dec920 commit 41a40a1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 40 deletions.
12 changes: 8 additions & 4 deletions lapce-app/src/panel/plugin_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use floem::{
IntoView, View,
};
use indexmap::IndexMap;
use lapce_rpc::plugin::{VoltID, VoltInfo};
use lapce_rpc::{
core::CoreRpcHandler,
plugin::{VoltID, VoltInfo},
};

use super::{
data::PanelSection, kind::PanelKind, position::PanelPosition, view::PanelBuilder,
Expand Down Expand Up @@ -68,6 +71,7 @@ pub fn plugin_panel(
) -> impl View {
let config = window_tab_data.common.config;
let plugin = window_tab_data.plugin.clone();
let core_rpc = window_tab_data.proxy.core_rpc.clone();

PanelBuilder::new(config, position)
.add(
Expand All @@ -77,7 +81,7 @@ pub fn plugin_panel(
)
.add(
"Available",
available_view(plugin.clone()),
available_view(plugin.clone(), core_rpc),
window_tab_data.panel.section_open(PanelSection::Available),
)
.build()
Expand Down Expand Up @@ -210,7 +214,7 @@ fn installed_view(plugin: PluginData) -> impl View {
})
}

fn available_view(plugin: PluginData) -> impl View {
fn available_view(plugin: PluginData, core_rpc: CoreRpcHandler) -> impl View {
let ui_line_height = plugin.common.ui_line_height;
let volts = plugin.available.volts;
let installed = plugin.installed;
Expand Down Expand Up @@ -390,7 +394,7 @@ fn available_view(plugin: PluginData) -> impl View {
})
.on_scroll(move |rect| {
if rect.y1 + 30.0 > content_rect.get_untracked().y1 {
plugin.load_more_available();
plugin.load_more_available(core_rpc.clone());
}
})
.style(|s| s.absolute().size_pct(100.0, 100.0))
Expand Down
117 changes: 81 additions & 36 deletions lapce-app/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
collections::HashSet,
rc::Rc,
sync::{atomic::AtomicU64, Arc},
time::Duration,
};

use anyhow::Result;
Expand All @@ -24,7 +25,11 @@ use floem::{
use indexmap::IndexMap;
use lapce_core::{command::EditCommand, directory::Directory, mode::Mode};
use lapce_proxy::plugin::{download_volt, volt_icon, wasi::find_all_volts};
use lapce_rpc::plugin::{VoltID, VoltInfo, VoltMetadata};
use lapce_rpc::{
core::{CoreNotification, CoreRpcHandler},
plugin::{VoltID, VoltInfo, VoltMetadata},
};
use lsp_types::MessageType;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

Expand Down Expand Up @@ -156,6 +161,7 @@ impl PluginData {
workspace_disabled: HashSet<VoltID>,
editors: Editors,
common: Rc<CommonData>,
core_rpc: CoreRpcHandler,
) -> Self {
let installed = cx.create_rw_signal(IndexMap::new());
let available = AvailableVoltList {
Expand All @@ -177,7 +183,7 @@ impl PluginData {
common,
};

plugin.load_available_volts("", 0);
plugin.load_available_volts("", 0, core_rpc.clone());

{
let plugin = plugin.clone();
Expand Down Expand Up @@ -224,7 +230,7 @@ impl PluginData {
plugin.available.query_id.update(|id| *id += 1);
plugin.available.loading.set(false);
plugin.available.volts.update(|v| v.clear());
plugin.load_available_volts(&query, 0);
plugin.load_available_volts(&query, 0, core_rpc.clone());
query
});
}
Expand Down Expand Up @@ -324,7 +330,12 @@ impl PluginData {
}
}

fn load_available_volts(&self, query: &str, offset: usize) {
fn load_available_volts(
&self,
query: &str,
offset: usize,
core_rpc: CoreRpcHandler,
) {
if self.available.loading.get_untracked() {
return;
}
Expand All @@ -344,43 +355,66 @@ impl PluginData {
return;
}

if let Ok(new) = new {
volts.update(|volts| {
volts.extend(new.plugins.into_iter().map(|volt| {
let icon = cx.create_rw_signal(None);
let send = create_ext_action(cx, move |result| {
if let Ok(i) = result {
icon.set(Some(i));
match new {
Ok(new) => {
volts.update(|volts| {
volts.extend(new.plugins.into_iter().map(|volt| {
let icon = cx.create_rw_signal(None);
let send = create_ext_action(cx, move |result| {
if let Ok(i) = result {
icon.set(Some(i));
}
});
{
let volt = volt.clone();
std::thread::spawn(move || {
let result = Self::load_icon(&volt);
send(result);
});
}
});
{
let volt = volt.clone();
std::thread::spawn(move || {
let result = Self::load_icon(&volt);
send(result);

let data = AvailableVoltData {
info: cx.create_rw_signal(volt.clone()),
icon,
installing: cx.create_rw_signal(false),
};
all.update(|all| {
all.insert(volt.id(), data.clone());
});
}

let data = AvailableVoltData {
info: cx.create_rw_signal(volt.clone()),
icon,
installing: cx.create_rw_signal(false),
};
all.update(|all| {
all.insert(volt.id(), data.clone());
});

(volt.id(), data)
}));
});
volts_total.set(new.total);
(volt.id(), data)
}));
});
volts_total.set(new.total);
}
Err(err) => {
tracing::error!("{:?}", err);
core_rpc.notification(CoreNotification::ShowMessage {
title: "Request Available Plugins".to_string(),
message: lsp_types::ShowMessageParams {
typ: MessageType::ERROR,
message: err.to_string(),
},
});
}
}
});

let query = query.to_string();
std::thread::spawn(move || {
let volts = Self::query_volts(&query, offset);
send(volts);
let mut try_time = 0;
loop {
let volts = Self::query_volts(&query, offset);
if volts.is_ok() {
send(volts);
break;
} else if try_time > 5 {
send(volts);
break;
} else {
try_time += 1;
}
}
});
}

Expand Down Expand Up @@ -439,10 +473,21 @@ impl PluginData {
}

fn query_volts(query: &str, offset: usize) -> Result<VoltsInfo> {
let client = if let Ok(proxy) = std::env::var("https_proxy") {
let proxy = reqwest::Proxy::all(proxy)?;
reqwest::blocking::Client::builder()
.proxy(proxy)
.timeout(Duration::from_secs(10))
.build()?
} else {
reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
.build()?
};
let url = format!(
"https://plugins.lapce.dev/api/v1/plugins?q={query}&offset={offset}"
);
let plugins: VoltsInfo = reqwest::blocking::get(url)?.json()?;
let plugins: VoltsInfo = client.get(url).send()?.json()?;
Ok(plugins)
}

Expand All @@ -451,7 +496,7 @@ impl PluginData {
>= self.available.total.get_untracked()
}

pub fn load_more_available(&self) {
pub fn load_more_available(&self, core_rpc: CoreRpcHandler) {
if self.all_loaded() {
return;
}
Expand All @@ -463,7 +508,7 @@ impl PluginData {
.buffer
.with_untracked(|buffer| buffer.to_string());
let offset = self.available.volts.with_untracked(|v| v.len());
self.load_available_volts(&query, offset);
self.load_available_volts(&query, offset, core_rpc);
}

pub fn install_volt(&self, info: VoltInfo) {
Expand Down
1 change: 1 addition & 0 deletions lapce-app/src/window_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ impl WindowTabData {
HashSet::from_iter(workspace_disabled_volts),
main_split.editors,
common.clone(),
proxy.core_rpc.clone(),
);

{
Expand Down

0 comments on commit 41a40a1

Please sign in to comment.