Skip to content

Commit

Permalink
#320 fixed missing observable notifications in case of an exception +…
Browse files Browse the repository at this point in the history
… fixed swallowing a websocket future exception
  • Loading branch information
bugy committed Mar 18, 2023
1 parent c30ff9c commit 58ac291
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/model/script_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import logging
import os
from collections import OrderedDict
from dataclasses import dataclass, field
Expand Down
16 changes: 11 additions & 5 deletions src/react/properties.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
from collections import UserList, UserDict

from typing import Optional, Iterable as Iterable, Mapping as Mapping, TypeVar

_T = TypeVar('_T')
_KT = TypeVar('_KT')
_VT = TypeVar('_VT')

LOGGER = logging.getLogger('script_server.properties')


class Property:
def __init__(self, value=None):
Expand Down Expand Up @@ -152,9 +154,7 @@ def __setitem__(self, key: _KT, item: _VT) -> None:

super().__setitem__(key, item)

if self._observers:
for observer in self._observers:
observer(key, old_value, item)
self._notify_observers(key, old_value, item)

def __delitem__(self, key: _KT) -> None:
old_value = self.get(key)
Expand All @@ -164,9 +164,15 @@ def __delitem__(self, key: _KT) -> None:
if old_value is None:
return

self._notify_observers(key, old_value, None)

def _notify_observers(self, key, old_value, new_value):
if self._observers:
for observer in self._observers:
observer(key, old_value, None)
try:
observer(key, old_value, new_value)
except:
LOGGER.exception('Failed to notify observer ' + repr(observer))


def observable_fields(*fields):
Expand Down
11 changes: 9 additions & 2 deletions src/web/script_config_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,20 @@ def on_message(self, text):
data = message.get('data')

if type == 'parameterValue':
param = data.get('parameter')
set_parameter_func = functools.partial(
self._set_parameter_value,
data.get('parameter'),
param,
data.get('value'),
data.get('clientStateVersion'))

self._start_task(set_parameter_func)
def handle_future_exception(result):
if result.exception():
LOGGER.exception(f'Failed to set parameter {param} value', exc_info=result.exception())

future = self._start_task(set_parameter_func)
future.add_done_callback(handle_future_exception)

return
elif type == 'reloadModelValues':
parameter_values = data.get('parameterValues')
Expand Down

0 comments on commit 58ac291

Please sign in to comment.