@@ -52,74 +52,80 @@ pub async fn watch<'e, P: Pf, REQ: Into<P::Req<'e>> + Clone + Send + UnwrapAft<'
5252 // NOTE: We keep the lock acquired for the entire lifetime of the query
5353 let mut rt = rt. lock ( ) . await ;
5454
55- let run = async {
56- // Read the new project while we're at it. This will likely have already been computed.
57- let new_project = watch_query (
58- rt. deref_mut ( ) ,
59- GetProject {
60- working_directory : working_directory. clone ( ) ,
61- } ,
62- QueryMode :: Continue ,
63- TimeoutAt ( Instant :: now ( ) + Duration :: from_millis ( 50 ) ) ,
64- )
65- . await ;
66-
67- // Determine if the watch config was changed as part of the project config.
68- let new_watch_config = match new_project {
69- Ok ( BlockOn :: Complete ( Ok ( project) ) ) => {
70- let config = WatchConfig {
71- interval_ms : project. interval_ms . unwrap_or ( DEFAULTS . interval_ms ) ,
72- cache_to_disk : project. cache_to_disk . unwrap_or ( DEFAULTS . cache_to_disk ) ,
73- max_idle_time_ms : project. max_idle_time_ms ,
74- } ;
75-
76- ( config != watch_config) . then_some ( config)
77- }
78- Ok ( BlockOn :: Complete ( Err ( errors) ) )
79- if !errors
80- . iter_unordered ( )
81- . any ( |error| error. is_invalid_project_config_syntax ( ) ) =>
82- {
83- // Failed to parse project file, don't change the configuration
84- None
85- }
86- _ => {
87- // Failed to read project file
88- ( watch_config != DEFAULTS ) . then_some ( DEFAULTS )
89- }
90- } ;
55+ // Read the new project while we're at it.
56+ let new_project = watch_query (
57+ rt. deref_mut ( ) ,
58+ GetProject {
59+ working_directory : working_directory. clone ( ) ,
60+ } ,
61+ QueryMode :: New ,
62+ TimeoutAt ( Instant :: now ( ) + Duration :: from_millis ( 50 ) ) ,
63+ )
64+ . await ;
65+
66+ // Determine if the watch config was changed as part of the project config.
67+ let new_watch_config = match new_project {
68+ Ok ( BlockOn :: Complete ( Ok ( project) ) ) => {
69+ let config = WatchConfig {
70+ interval_ms : project. interval_ms . unwrap_or ( DEFAULTS . interval_ms ) ,
71+ cache_to_disk : project. cache_to_disk . unwrap_or ( DEFAULTS . cache_to_disk ) ,
72+ max_idle_time_ms : project. max_idle_time_ms ,
73+ } ;
74+
75+ ( config != watch_config) . then_some ( config)
76+ }
77+ Ok ( BlockOn :: Complete ( Err ( errors) ) )
78+ if !errors
79+ . iter_unordered ( )
80+ . any ( |error| error. is_invalid_project_config_syntax ( ) ) =>
81+ {
82+ // Failed to parse project file, don't change the configuration
83+ None
84+ }
85+ _ => {
86+ // Failed to read project file
87+ ( watch_config != DEFAULTS ) . then_some ( DEFAULTS )
88+ }
89+ } ;
9190
92- // If we have a new watch config, update it as well as the idle tracker.
93- if let Some ( new_watch_config) = new_watch_config {
94- watch_config = new_watch_config;
91+ // If we have a new watch config, update it as well as the idle tracker.
92+ if let Some ( new_watch_config) = new_watch_config {
93+ watch_config = new_watch_config;
9594
96- let mut idle_tracker = server. idle_tracker . lock ( ) . await ;
97- idle_tracker. last_active = Instant :: now ( ) ;
98- idle_tracker
99- . set_max_idle_time ( watch_config. max_idle_time_ms . map ( Duration :: from_millis) ) ;
100- }
95+ let mut idle_tracker = server. idle_tracker . lock ( ) . await ;
96+ idle_tracker. last_active = Instant :: now ( ) ;
97+ idle_tracker
98+ . set_max_idle_time ( watch_config. max_idle_time_ms . map ( Duration :: from_millis) ) ;
99+ rt. cache_to_disk = watch_config. cache_to_disk ;
100+ }
101101
102+ let run = async {
102103 let timeout = TimeoutAt ( Instant :: now ( ) + Duration :: from_secs ( 2 ) ) ;
103- Ok ( watch_query ( rt. deref_mut ( ) , watchee. clone ( ) , QueryMode :: New , timeout) . await )
104+ Ok ( watch_query (
105+ rt. deref_mut ( ) ,
106+ watchee. clone ( ) ,
107+ QueryMode :: Continue ,
108+ timeout,
109+ )
110+ . await )
104111 } ;
105112
106113 let timeout = async {
107114 Timer :: after ( Duration :: from_millis ( 500 ) ) . await ;
108115 Err ( ( ) )
109116 } ;
110117
118+ if server. idle_tracker . lock ( ) . await . shutdown_if_idle ( ) {
119+ break ;
120+ }
121+
111122 log ! ( "Watch Result is {:?}" , run. or( timeout) . await ) ;
112123 Timer :: after ( Duration :: from_millis ( watch_config. interval_ms ) ) . await ;
113124
114125 if server. idle_tracker . lock ( ) . await . shutdown_if_idle ( ) {
115126 break ;
116127 }
117128 }
118-
119- // Try to save the cache to disk before we exit if possible
120- if watch_config. cache_to_disk {
121- let _ = rt. lock ( ) . await . cache ( ) . save ( "adept.cache" ) ;
122- }
123129}
124130
125131async fn watch_query <
0 commit comments