refactor: reduce duplicate code in engine.py (#1246) · python-zeroconf/python-zeroconf@36ae505

Original file line numberDiff line numberDiff line change

@@ -80,8 +80,7 @@ def setup(self, loop: asyncio.AbstractEventLoop, loop_thread_ready: Optional[thr

8080
8181

async def _async_setup(self, loop_thread_ready: Optional[threading.Event]) -> None:

8282

"""Set up the instance."""

83-

assert self.loop is not None

84-

self._cleanup_timer = self.loop.call_later(_CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup)

83+

self._async_schedule_next_cache_cleanup()

8584

await self._async_create_endpoints()

8685

assert self.running_event is not None

8786

self.running_event.set()

@@ -118,8 +117,13 @@ def _async_cache_cleanup(self) -> None:

118117

now, [RecordUpdate(record, record) for record in self.zc.cache.async_expire(now)]

119118

)

120119

self.zc.record_manager.async_updates_complete(False)

121-

assert self.loop is not None

122-

self._cleanup_timer = self.loop.call_later(_CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup)

120+

self._async_schedule_next_cache_cleanup()

121+
122+

def _async_schedule_next_cache_cleanup(self) -> None:

123+

"""Schedule the next cache cleanup."""

124+

loop = self.loop

125+

assert loop is not None

126+

self._cleanup_timer = loop.call_at(loop.time() + _CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup)

123127
124128

async def _async_close(self) -> None:

125129

"""Cancel and wait for the cleanup task to finish."""