Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/NativeHost/
Reload.rs

1//! `nativeHost:reload` - reload the webview without restarting the process.
2//! VS Code calls this from `ILifecycleMainService.reload()` for "Reload
3//! Window" (Developer menu / Cmd+Shift+P → Reload Window).
4//!
5//! Before triggering the renderer reload we ask Cocoon to serialize every
6//! webview panel that has a registered serializer via
7//! `vscode.window.registerWebviewPanelSerializer`. The returned
8//! `(viewType, state)` pairs land in the global memento under
9//! `__webview_panel_state__`, where Sky's webview bridge picks them up
10//! after the reload completes and asks Cocoon to deserialize each entry.
11//!
12//! Errors / timeouts on the serialize path are intentionally swallowed:
13//! reload must remain instant for the operator; a missing panel state
14//! is a survivable degradation, a frozen reload button is not.
15
16use std::{sync::Arc, time::Duration};
17
18use CommonLibrary::Storage::StorageProvider::StorageProvider;
19use serde_json::Value;
20use tauri::{AppHandle, Manager};
21
22use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
23
24const PANEL_STATE_KEY:&str = "__webview_panel_state__";
25
26pub async fn Fn(ApplicationHandle:AppHandle, _Arguments:Vec<Value>) -> Result<Value, String> {
27	dev_log!("lifecycle", "nativeHost:reload - reloading webview");
28
29	// Best-effort webview panel snapshot before the renderer reload wipes
30	// in-memory state. 1.5s budget: a slow serializer can hold up the
31	// reload, but only briefly - past the budget we proceed without state.
32	if crate::Vine::Client::IsClientConnected::Fn("cocoon-main") {
33		let SerializeMethod = "ExtHostWebviewPanels$serializeAllWebviewPanels".to_string();
34
35		let SerializeCall =
36			crate::Vine::Client::SendRequest::Fn("cocoon-main", SerializeMethod, Value::Array(Vec::new()), 1500);
37
38		match tokio::time::timeout(Duration::from_millis(1700), SerializeCall).await {
39			Ok(Ok(Snapshot)) if !Snapshot.is_null() => {
40				if let Some(Runtime) = ApplicationHandle.try_state::<Arc<ApplicationRunTime>>() {
41					if let Err(StoreError) = Runtime
42						.inner()
43						.Environment
44						.UpdateStorageValue(true, PANEL_STATE_KEY.to_string(), Some(Snapshot))
45						.await
46					{
47						dev_log!(
48							"lifecycle",
49							"warn: [Reload] Failed to persist webview panel snapshot: {:?}",
50							StoreError
51						);
52					}
53				}
54			},
55			Ok(Ok(_)) => {
56				// Empty / null snapshot - no panels needed serialization.
57			},
58			Ok(Err(GrpcError)) => {
59				dev_log!("lifecycle", "warn: [Reload] serializeAllWebviewPanels failed: {:?}", GrpcError);
60			},
61			Err(_) => {
62				dev_log!(
63					"lifecycle",
64					"warn: [Reload] serializeAllWebviewPanels timed out - proceeding without panel state"
65				);
66			},
67		}
68	}
69
70	if let Some(Window) = ApplicationHandle.get_webview_window("main") {
71		Window
72			.eval("location.reload()")
73			.map_err(|E| format!("reload eval failed: {E}"))?;
74	}
75
76	Ok(Value::Null)
77}