Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RPC/CocoonService/FileSystem/
WatchFile.rs

1//! `watch_file` gRPC endpoint - Cocoon calls this when an extension uses
2//! `vscode.workspace.createFileSystemWatcher`. Routes to Mountain's
3//! `FileWatcherProvider::RegisterWatcher` so OS events (FSEvents on macOS,
4//! inotify on Linux) flow back to Cocoon as `$fileWatcher:event` gRPC
5//! notifications which Cocoon fans out to extension `onDidChangeFile`
6//! listeners.
7//!
8//! The proto `WatchFileRequest` only carries a `uri` field. We derive the
9//! watch handle from a hash of the URI so dedup-by-triple logic in
10//! `FileWatcherProvider` can collapse identical registrations from multiple
11//! extensions watching the same root.
12
13use std::{
14	path::PathBuf,
15	sync::atomic::{AtomicU64, Ordering},
16};
17
18use CommonLibrary::FileSystem::FileWatcherProvider::FileWatcherProvider;
19use tonic::{Response, Status};
20
21use crate::{
22	RPC::CocoonService::CocoonServiceImpl,
23	Vine::Generated::{Empty, WatchFileRequest},
24	dev_log,
25};
26
27static WATCH_SEQ:AtomicU64 = AtomicU64::new(1);
28
29pub async fn Fn(Service:&CocoonServiceImpl, Request:WatchFileRequest) -> Result<Response<Empty>, Status> {
30	let URI = Request.uri.as_ref().map(|U| U.value.as_str()).unwrap_or("").to_string();
31
32	if URI.is_empty() {
33		return Ok(Response::new(Empty {}));
34	}
35
36	let Handle = format!("grpc-watch-{}", WATCH_SEQ.fetch_add(1, Ordering::Relaxed));
37
38	dev_log!("filewatcher", "[CocoonService] watch_file handle={} uri={}", Handle, URI);
39
40	let Root = if let Ok(Url) = url::Url::parse(&URI) {
41		Url.to_file_path().unwrap_or_else(|_| PathBuf::from(&URI))
42	} else {
43		PathBuf::from(&URI)
44	};
45
46	// Register recursive with no pattern filter - Cocoon's FileSystemWatcher
47	// subscribers apply their own glob matching on the extension side.
48	Service
49		.environment
50		.RegisterWatcher(Handle, Root, true, None)
51		.await
52		.map_err(|E| Status::internal(format!("watch_file: {E}")))?;
53
54	Ok(Response::new(Empty {}))
55}