refactor: make StateCache descriptor-free
This commit is contained in:
@@ -8,26 +8,26 @@ from __future__ import annotations
|
||||
|
||||
import threading
|
||||
import time
|
||||
from typing import Callable, Optional, TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .appliances.base import ApplianceDescriptor
|
||||
from typing import Callable, Optional
|
||||
|
||||
|
||||
class StateCache:
|
||||
|
||||
def __init__(self, descriptor: 'ApplianceDescriptor'):
|
||||
self.descriptor = descriptor
|
||||
def __init__(self):
|
||||
self.links: dict[str, dict] = {}
|
||||
self.last_updated: dict[str, float] = {}
|
||||
self.source: dict[str, str] = {}
|
||||
self.descriptor_state: dict = {}
|
||||
self._on_change: Optional[Callable[[bool, str], None]] = None
|
||||
self._on_observation: Optional[Callable[[dict, str, dict], None]] = None
|
||||
self._lock = threading.RLock()
|
||||
|
||||
def set_on_change(self, cb: Callable[[bool, str], None]) -> None:
|
||||
self._on_change = cb
|
||||
|
||||
def set_on_observation(self, hook: Optional[Callable[[dict, str, dict], None]]) -> None:
|
||||
self._on_observation = hook
|
||||
|
||||
def apply_rep(self, href: str, rep: dict, source: str) -> bool:
|
||||
if not isinstance(rep, dict):
|
||||
return False
|
||||
@@ -37,7 +37,7 @@ class StateCache:
|
||||
self.links[href] = rep
|
||||
self.last_updated[href] = time.time()
|
||||
self.source[href] = source
|
||||
hook = self.descriptor.on_observation
|
||||
hook = self._on_observation
|
||||
if hook is not None:
|
||||
try:
|
||||
hook(self.descriptor_state, href, rep)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
from samsung_appliance.state_cache import StateCache
|
||||
|
||||
|
||||
def test_state_cache_constructs_without_descriptor():
|
||||
c = StateCache()
|
||||
assert c.links == {}
|
||||
assert c.descriptor_state == {}
|
||||
|
||||
|
||||
def test_on_observation_hook_fires():
|
||||
c = StateCache()
|
||||
seen = []
|
||||
c.set_on_observation(lambda st, href, rep: seen.append((href, rep)))
|
||||
c.apply_rep('/x/vs/0', {'a': 1}, source='seed')
|
||||
assert seen == [('/x/vs/0', {'a': 1})]
|
||||
Reference in New Issue
Block a user