|
| 1 | +import dataclasses |
| 2 | +import json |
| 3 | +import threading |
| 4 | +from multiprocessing import Event |
| 5 | +from typing import Any, Dict, List, Optional |
| 6 | +from unittest.main import main |
| 7 | + |
| 8 | +from beartype import beartype |
| 9 | + |
| 10 | +from flet.control import Control |
| 11 | +from flet.ref import Ref |
| 12 | + |
| 13 | + |
| 14 | +@dataclasses.dataclass |
| 15 | +class ClientStorageMethodCall: |
| 16 | + i: int |
| 17 | + n: str |
| 18 | + p: List[str] |
| 19 | + |
| 20 | + |
| 21 | +@dataclasses.dataclass |
| 22 | +class ClientStorageMethodResults: |
| 23 | + i: int |
| 24 | + r: Optional[str] |
| 25 | + e: Optional[str] |
| 26 | + |
| 27 | + |
| 28 | +class ClientStorage(Control): |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + ref: Optional[Ref] = None, |
| 32 | + data: Any = None, |
| 33 | + ): |
| 34 | + |
| 35 | + Control.__init__( |
| 36 | + self, |
| 37 | + ref=ref, |
| 38 | + data=data, |
| 39 | + ) |
| 40 | + |
| 41 | + self.__call_counter = 0 |
| 42 | + self.__calls: Dict[int, threading.Event] = {} |
| 43 | + self.__results: Dict[threading.Event, tuple[Optional[str], Optional[str]]] = {} |
| 44 | + self._add_event_handler("result", self._on_result) |
| 45 | + |
| 46 | + def _get_control_name(self): |
| 47 | + return "clientstorage" |
| 48 | + |
| 49 | + def _is_isolated(self): |
| 50 | + return True |
| 51 | + |
| 52 | + def set(self, key: str, value: Any): |
| 53 | + jv = self._convert_attr_json(value) |
| 54 | + assert jv is not None |
| 55 | + self._call_method("set", [key, jv]) |
| 56 | + |
| 57 | + def get(self, key: str): |
| 58 | + jv = self._call_method("get", [key]) |
| 59 | + if jv != None: |
| 60 | + return json.loads(jv) |
| 61 | + return None |
| 62 | + |
| 63 | + def contains_key(self, key: str) -> bool: |
| 64 | + return self._call_method("containskey", [key]) |
| 65 | + |
| 66 | + def remove(self, key: str) -> bool: |
| 67 | + return self._call_method("remove", [key]) |
| 68 | + |
| 69 | + def get_keys(self, key_prefix: str) -> List[str]: |
| 70 | + return self._call_method("getkeys", [key_prefix]) |
| 71 | + |
| 72 | + def clear(self) -> bool: |
| 73 | + return self._call_method("clear", []) |
| 74 | + |
| 75 | + def _call_method(self, name: str, params: List[str]) -> Any: |
| 76 | + m = ClientStorageMethodCall(i=self.__call_counter, n=name, p=params) |
| 77 | + self.__call_counter += 1 |
| 78 | + self._set_attr_json("method", m) |
| 79 | + evt = threading.Event() |
| 80 | + self.__calls[m.i] = evt |
| 81 | + self.update() |
| 82 | + if not evt.wait(5): |
| 83 | + del self.__calls[m.i] |
| 84 | + raise Exception( |
| 85 | + f"Timeout waiting for ClientStorage.{name}({params}) method call" |
| 86 | + ) |
| 87 | + result, err = self.__results.pop(evt) |
| 88 | + if err != None: |
| 89 | + raise Exception(err) |
| 90 | + if result == None: |
| 91 | + return None |
| 92 | + return json.loads(result) |
| 93 | + |
| 94 | + def _on_result(self, e): |
| 95 | + d = json.loads(e.data) |
| 96 | + result = ClientStorageMethodResults(**d) |
| 97 | + evt = self.__calls.pop(result.i, None) |
| 98 | + if evt == None: |
| 99 | + return |
| 100 | + self.__results[evt] = (result.r, result.e) |
| 101 | + evt.set() |
0 commit comments