@@ -39,9 +39,11 @@ def __init__(
39
39
40
40
self ._log = log or getLogger (__name__ )
41
41
self ._subscriptions : dict [str , Callable [[], Coroutine [Any , Any , None ]]] = {}
42
+ self ._filepath_subscriptions : dict [str , Callable [[], Coroutine [Any , Any , None ] | None ]] = {}
42
43
43
44
self ._watcher = asyncio .create_task (self ._watch_file ()) if self ._poll_interval else None
44
45
self .last_modified = None
46
+ self ._current_path = self .path
45
47
46
48
@property
47
49
def file_id (self ) -> str :
@@ -79,7 +81,12 @@ async def clean(self) -> None:
79
81
except asyncio .CancelledError :
80
82
self ._log .info (f"file watcher for '{ self .file_id } ' is cancelled now" )
81
83
82
- def observe (self , id : str , callback : Callable [[], Coroutine [Any , Any , None ]]) -> None :
84
+ def observe (
85
+ self ,
86
+ id : str ,
87
+ callback : Callable [[], Coroutine [Any , Any , None ]],
88
+ filepath_callback : Callable [[], Coroutine [Any , Any , None ] | None ] | None = None ,
89
+ ) -> None :
83
90
"""
84
91
Subscribe to the file to get notified about out-of-band file changes.
85
92
@@ -88,6 +95,8 @@ def observe(self, id: str, callback: Callable[[], Coroutine[Any, Any, None]]) ->
88
95
callback (Callable): Callback for notifying the room.
89
96
"""
90
97
self ._subscriptions [id ] = callback
98
+ if filepath_callback is not None :
99
+ self ._filepath_subscriptions [id ] = filepath_callback
91
100
92
101
def unobserve (self , id : str ) -> None :
93
102
"""
@@ -97,6 +106,8 @@ def unobserve(self, id: str) -> None:
97
106
id (str): Room ID
98
107
"""
99
108
del self ._subscriptions [id ]
109
+ if id in self ._filepath_subscriptions .keys ():
110
+ del self ._filepath_subscriptions [id ]
100
111
101
112
async def load_content (self , format : str , file_type : str ) -> dict [str , Any ]:
102
113
"""
@@ -204,15 +215,26 @@ async def maybe_notify(self) -> None:
204
215
Notifies subscribed rooms about out-of-band file changes.
205
216
"""
206
217
do_notify = False
218
+ filepath_change = False
207
219
async with self ._lock :
220
+ path = self .path
221
+ if self ._current_path != path :
222
+ self ._current_path = path
223
+ filepath_change = True
224
+
208
225
# Get model metadata; format and type are not need
209
- model = await ensure_async (self ._contents_manager .get (self . path , content = False ))
226
+ model = await ensure_async (self ._contents_manager .get (path , content = False ))
210
227
211
228
if self .last_modified is not None and self .last_modified < model ["last_modified" ]:
212
229
do_notify = True
213
230
214
231
self .last_modified = model ["last_modified" ]
215
232
233
+ if filepath_change :
234
+ # Notify filepath change
235
+ for callback in self ._filepath_subscriptions .values ():
236
+ await ensure_async (callback ())
237
+
216
238
if do_notify :
217
239
# Notify out-of-band change
218
240
# callbacks will load the file content, thus release the lock before calling them
0 commit comments