Update COMMUNITY_INTEGRATIONS.md _update_settings examples
Simplify the reconnect example to show a common pattern (reconnect on any change) and improve the _warn_unhandled_updated_settings example to show selective handling of specific fields.
This commit is contained in:
@@ -266,17 +266,18 @@ class MySTTService(STTService):
|
|||||||
self._sync_model_name_to_metrics()
|
self._sync_model_name_to_metrics()
|
||||||
```
|
```
|
||||||
|
|
||||||
To react to runtime setting changes, override `_update_settings`. The base implementation applies the delta to `self._settings` and returns a `dict` mapping each changed field name to its **pre-update** value. Your override should call `super()` first, then act on the changed fields:
|
To react to runtime setting changes, override `_update_settings`. The base implementation applies the delta to `self._settings` and returns a `dict` mapping each changed field name to its **pre-update** value. Your override should call `super()` first, then act on the changed fields. A common implementation might look like:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
async def _update_settings(self, update: STTSettings) -> dict[str, Any]:
|
async def _update_settings(self, update: STTSettings) -> dict[str, Any]:
|
||||||
"""Apply a settings update, reconfiguring the recognizer if needed."""
|
"""Apply a settings update, reconfiguring the recognizer if needed."""
|
||||||
changed = await super()._update_settings(update)
|
changed = await super()._update_settings(update)
|
||||||
|
|
||||||
if "language" in changed:
|
if not changed:
|
||||||
# Restart the recognizer with the new language.
|
return changed
|
||||||
await self._disconnect()
|
|
||||||
await self._connect()
|
await self._disconnect()
|
||||||
|
await self._connect()
|
||||||
|
|
||||||
return changed
|
return changed
|
||||||
```
|
```
|
||||||
@@ -285,7 +286,7 @@ The dict keys work like a set for membership tests (`"language" in changed`) and
|
|||||||
|
|
||||||
Note that, in this example, the service requires a reconnect to apply the new language. Consider, for each setting, whether your service requires reconnection or can apply changes in-place.
|
Note that, in this example, the service requires a reconnect to apply the new language. Consider, for each setting, whether your service requires reconnection or can apply changes in-place.
|
||||||
|
|
||||||
If your service can't yet apply certain settings at runtime, call `self._warn_unhandled_updated_settings(changed)` with the unhandled field names so users get a clear log message:
|
If your service can't yet apply certain settings at runtime, call `self._warn_unhandled_updated_settings(changed)` with any unhandled field names so users get a clear log message:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
async def _update_settings(self, update: STTSettings) -> dict[str, Any]:
|
async def _update_settings(self, update: STTSettings) -> dict[str, Any]:
|
||||||
@@ -294,8 +295,11 @@ async def _update_settings(self, update: STTSettings) -> dict[str, Any]:
|
|||||||
if not changed:
|
if not changed:
|
||||||
return changed
|
return changed
|
||||||
|
|
||||||
# TODO: someday we could reconnect here to apply updated settings.
|
if "language" in changed:
|
||||||
self._warn_unhandled_updated_settings(changed)
|
await self._update_language()
|
||||||
|
else:
|
||||||
|
# TODO: handle changes to other settings soon!
|
||||||
|
self._warn_unhandled_updated_settings(changed.keys() - {"language"})
|
||||||
|
|
||||||
return changed
|
return changed
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user