Skip to content

Commit 5a3bf3e

Browse files
author
Shubham
committed
Notify Sync client when underlying Store is closed
When Store is closed, the Sync client should be closed too
1 parent 7aacc1a commit 5a3bf3e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

objectbox/store.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def __init__(self,
127127
"""
128128

129129
self._c_store = None
130+
self._close_listeners: list[Callable[[], None]] = []
130131
if not c_store:
131132
options = StoreOptions()
132133
try:
@@ -272,6 +273,9 @@ def write_tx(self):
272273

273274
def close(self):
274275
"""Close database."""
276+
for listener in self._close_listeners.values():
277+
listener()
278+
self._close_listeners.clear()
275279
c_store_to_close = self._c_store
276280
if c_store_to_close:
277281
self._c_store = None
@@ -288,3 +292,6 @@ def remove_db_files(db_dir: str):
288292

289293
def c_store(self):
290294
return self._c_store
295+
296+
def add_store_close_listener(self, on_store_close: Callable[[], None]):
297+
self._close_listeners.append(on_store_close)

objectbox/sync.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ def __init__(self, store: Store, server_urls: list[str],
156156
c.c_array_pointer(self.__server_urls, ctypes.c_char_p),
157157
len(self.__server_urls))
158158

159+
self.__store.add_store_close_listener(on_store_close=self.__close_sync_client_func())
160+
161+
def __close_sync_client_func(self):
162+
def close_sync_client():
163+
self.close()
164+
165+
return close_sync_client
166+
159167
def set_credentials(self, credentials: SyncCredentials):
160168
self.__credentials = credentials
161169
if isinstance(credentials, SyncCredentialsNone):

tests/test_sync.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,12 @@ def test_multiple_credentials(test_store):
9595
SyncCredentials.jwt_refresh_token("token3"),
9696
SyncCredentials.jwt_custom_token("token4")
9797
])
98+
99+
100+
def test_client_closed_when_store_closed(test_store):
101+
server_urls = ["ws://localhost:9999"]
102+
client = SyncClient(test_store, server_urls)
103+
104+
assert not client.is_closed()
105+
test_store.close()
106+
assert client.is_closed()

0 commit comments

Comments
 (0)