Update Coordinator: Only schedule a refresh if listenerrs (#34317)

This commit is contained in:
Paulus Schoutsen 2020-04-16 16:44:14 -07:00 committed by GitHub
parent c9cc024e9b
commit ff469cb592
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View file

@ -170,7 +170,8 @@ class DataUpdateCoordinator:
self.name, self.name,
monotonic() - start, monotonic() - start,
) )
self._schedule_refresh() if self._listeners:
self._schedule_refresh()
for update_callback in self._listeners: for update_callback in self._listeners:
update_callback() update_callback()

View file

@ -41,6 +41,8 @@ async def test_async_refresh(crd):
await crd.async_refresh() await crd.async_refresh()
assert crd.data == 1 assert crd.data == 1
assert crd.last_update_success is True assert crd.last_update_success is True
# Make sure we didn't schedule a refresh because we have 0 listeners
assert crd._unsub_refresh is None
updates = [] updates = []
@ -50,6 +52,7 @@ async def test_async_refresh(crd):
unsub = crd.async_add_listener(update_callback) unsub = crd.async_add_listener(update_callback)
await crd.async_refresh() await crd.async_refresh()
assert updates == [2] assert updates == [2]
assert crd._unsub_refresh is not None
# Test unsubscribing through function # Test unsubscribing through function
unsub() unsub()