@@ -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 :
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
"""
@@ -190,15 +201,26 @@ async def maybe_notify(self) -> None:
190
201
Notifies subscribed rooms about out-of-band file changes.
191
202
"""
192
203
do_notify = False
204
+ filepath_change = False
193
205
async with self ._lock :
206
+ path = self .path
207
+ if self ._current_path != path :
208
+ self ._current_path = path
209
+ filepath_change = True
210
+
194
211
# Get model metadata; format and type are not need
195
- model = await ensure_async (self ._contents_manager .get (self . path , content = False ))
212
+ model = await ensure_async (self ._contents_manager .get (path , content = False ))
196
213
197
214
if self .last_modified is not None and self .last_modified < model ["last_modified" ]:
198
215
do_notify = True
199
216
200
217
self .last_modified = model ["last_modified" ]
201
218
219
+ if filepath_change :
220
+ # Notify filepath change
221
+ for callback in self ._filepath_subscriptions .values ():
222
+ await ensure_async (callback ())
223
+
202
224
if do_notify :
203
225
# Notify out-of-band change
204
226
# callbacks will load the file content, thus release the lock before calling them
0 commit comments