Simplify Iris app initialization and task updates
This commit is contained in:
1 parent
8218e84b62
commit
ecf74055c7
32 files changed
+409
-377
No files matched your search
+45
-26
@@ -45,7 +45,7 @@ struct Client {
|
||||
ui_state: DesktopUiState,
|
||||
api: Arc<ApiClient<UreqTransport>>,
|
||||
stream_transport: Arc<UreqTransport>,
|
||||
proxy: Proxy<AppEvent>,
|
||||
updates: TaskCtx<StdRsc<Self>>,
|
||||
sessions: Vec<SessionSummary>,
|
||||
selected: Option<String>,
|
||||
items: Vec<TranscriptItem>,
|
||||
@@ -56,9 +56,7 @@ struct Client {
|
||||
}
|
||||
|
||||
impl DesktopAppState for Client {
|
||||
type Event = AppEvent;
|
||||
|
||||
fn new(mut ui_state: DesktopUiState, rsc: &mut StdRsc<Self>, proxy: Proxy<AppEvent>) -> Self {
|
||||
fn new(mut ui_state: DesktopUiState, rsc: &mut StdRsc<Self>) -> Self {
|
||||
let (server, ca_pem) = super::startup::load_startup_config().unwrap_or_else(|e| {
|
||||
eprintln!("desktop-app: {e}");
|
||||
process::exit(2);
|
||||
@@ -90,7 +88,7 @@ impl DesktopAppState for Client {
|
||||
ui_state,
|
||||
api,
|
||||
stream_transport,
|
||||
proxy,
|
||||
updates: rsc.tasks.context(),
|
||||
sessions: Vec::new(),
|
||||
selected: None,
|
||||
items: Vec::new(),
|
||||
@@ -102,8 +100,10 @@ impl DesktopAppState for Client {
|
||||
client.spawn_fetch_sessions();
|
||||
client
|
||||
}
|
||||
}
|
||||
|
||||
fn event(&mut self, event: AppEvent, rsc: &mut StdRsc<Self>) {
|
||||
impl Client {
|
||||
fn apply_event(&mut self, event: AppEvent, rsc: &mut StdRsc<Self>) {
|
||||
match event {
|
||||
AppEvent::Sessions(Ok(sessions)) => {
|
||||
self.sessions = sessions;
|
||||
@@ -163,11 +163,8 @@ impl DesktopAppState for Client {
|
||||
eprintln!("desktop-app: couldn't send: {message}");
|
||||
}
|
||||
}
|
||||
self.ui_state.window.request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
impl Client {
|
||||
fn current(&self, session_id: &str, generation: u64) -> bool {
|
||||
self.selected.as_deref() == Some(session_id)
|
||||
&& self.generation.load(Ordering::SeqCst) == generation
|
||||
@@ -180,10 +177,12 @@ impl Client {
|
||||
|
||||
fn spawn_fetch_sessions(&self) {
|
||||
let api = self.api.clone();
|
||||
let proxy = self.proxy.clone();
|
||||
let mut updates = self.updates.clone();
|
||||
thread::spawn(move || {
|
||||
let result = api.fetch_sessions().map_err(|e| e.to_string());
|
||||
let _ = proxy.send_event(AppEvent::Sessions(result));
|
||||
updates.update(move |state: &mut Client, rsc| {
|
||||
state.apply_event(AppEvent::Sessions(result), rsc);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -211,7 +210,7 @@ impl Client {
|
||||
|
||||
let api = self.api.clone();
|
||||
let stream_transport = self.stream_transport.clone();
|
||||
let proxy = self.proxy.clone();
|
||||
let mut updates = self.updates.clone();
|
||||
let live_generation = self.generation.clone();
|
||||
thread::spawn(move || {
|
||||
let page: Result<Vec<serde_json::Value>, String> = api
|
||||
@@ -223,10 +222,16 @@ impl Client {
|
||||
.and_then(|values| raw_seq(values.last()?))
|
||||
.unwrap_or(0);
|
||||
let result = page.and_then(|values| fold_page(&values));
|
||||
let _ = proxy.send_event(AppEvent::TranscriptLoaded {
|
||||
session_id: session_id.clone(),
|
||||
generation,
|
||||
result,
|
||||
let loaded_session_id = session_id.clone();
|
||||
updates.update(move |state: &mut Client, rsc| {
|
||||
state.apply_event(
|
||||
AppEvent::TranscriptLoaded {
|
||||
session_id: loaded_session_id,
|
||||
generation,
|
||||
result,
|
||||
},
|
||||
rsc,
|
||||
);
|
||||
});
|
||||
|
||||
let stop = || live_generation.load(Ordering::SeqCst) != generation;
|
||||
@@ -240,28 +245,42 @@ impl Client {
|
||||
if stop() {
|
||||
return false;
|
||||
}
|
||||
let _ = proxy.send_event(AppEvent::StreamEvent {
|
||||
session_id: session_id.clone(),
|
||||
generation,
|
||||
event,
|
||||
let event_session_id = session_id.clone();
|
||||
updates.update(move |state: &mut Client, rsc| {
|
||||
state.apply_event(
|
||||
AppEvent::StreamEvent {
|
||||
session_id: event_session_id,
|
||||
generation,
|
||||
event,
|
||||
},
|
||||
rsc,
|
||||
);
|
||||
});
|
||||
true
|
||||
}
|
||||
});
|
||||
let _ = proxy.send_event(AppEvent::StreamEnded {
|
||||
session_id,
|
||||
generation,
|
||||
message: outcome.err().map(|e| e.to_string()),
|
||||
updates.update(move |state: &mut Client, rsc| {
|
||||
state.apply_event(
|
||||
AppEvent::StreamEnded {
|
||||
session_id,
|
||||
generation,
|
||||
message: outcome.err().map(|e| e.to_string()),
|
||||
},
|
||||
rsc,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn send_message(&mut self, session_id: String, text: String) {
|
||||
let api = self.api.clone();
|
||||
let proxy = self.proxy.clone();
|
||||
let mut updates = self.updates.clone();
|
||||
thread::spawn(move || {
|
||||
if let Err(e) = api.send_message(&session_id, &text, &[]) {
|
||||
let _ = proxy.send_event(AppEvent::SendFailed(e.to_string()));
|
||||
let message = e.to_string();
|
||||
updates.update(move |state: &mut Client, rsc| {
|
||||
state.apply_event(AppEvent::SendFailed(message), rsc);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user