Skip to main content

Mountain/RPC/CocoonService/FileSystem/
WatchFile.rs

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