Fixed darkmode load and save
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import logging
|
||||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
from dbengine.utils import get_class
|
||||
|
||||
from myfasthtml.controls.helpers import Ids
|
||||
from myfasthtml.core.utils import pascal_to_snake
|
||||
from myfasthtml.core.utils import pascal_to_snake, snake_to_pascal
|
||||
|
||||
logger = logging.getLogger("InstancesManager")
|
||||
|
||||
special_session = {
|
||||
"user_info": {"id": "** SPECIAL SESSION **"}
|
||||
@@ -57,13 +62,14 @@ class BaseInstance:
|
||||
if not getattr(self, "_is_new_instance", False):
|
||||
# Skip __init__ if instance already existed
|
||||
return
|
||||
else:
|
||||
# make sure that it's no longer considered as a new instance
|
||||
elif not isinstance(self, UniqueInstance):
|
||||
# No more __init__ unless it's UniqueInstance
|
||||
self._is_new_instance = False
|
||||
|
||||
self._parent = parent
|
||||
self._session = session or (parent.get_session() if parent else None)
|
||||
self._id = _id or self.compute_id()
|
||||
self._prefix = self._id if isinstance(self, (UniqueInstance, SingleInstance)) else self.compute_prefix()
|
||||
|
||||
if auto_register:
|
||||
InstancesManager.register(self._session, self)
|
||||
@@ -77,13 +83,16 @@ class BaseInstance:
|
||||
def get_parent(self) -> Optional['BaseInstance']:
|
||||
return self._parent
|
||||
|
||||
def get_prefix(self) -> str:
|
||||
return self._prefix
|
||||
|
||||
@classmethod
|
||||
def get_prefix(cls):
|
||||
def compute_prefix(cls):
|
||||
return f"mf-{pascal_to_snake(cls.__name__)}"
|
||||
|
||||
@classmethod
|
||||
def compute_id(cls):
|
||||
prefix = cls.get_prefix()
|
||||
prefix = cls.compute_prefix()
|
||||
if issubclass(cls, SingleInstance):
|
||||
_id = prefix
|
||||
else:
|
||||
@@ -104,6 +113,20 @@ class SingleInstance(BaseInstance):
|
||||
super().__init__(parent, session, _id, auto_register)
|
||||
|
||||
|
||||
class UniqueInstance(BaseInstance):
|
||||
"""
|
||||
Base class for instances that can only have one instance at a time.
|
||||
But unlike SingleInstance, the __init__ is called every time it's instantiated.
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
parent: Optional[BaseInstance] = None,
|
||||
session: Optional[dict] = None,
|
||||
_id: Optional[str] = None,
|
||||
auto_register: bool = True):
|
||||
super().__init__(parent, session, _id, auto_register)
|
||||
|
||||
|
||||
class MultipleInstance(BaseInstance):
|
||||
"""
|
||||
Base class for instances that can have multiple instances at a time.
|
||||
@@ -157,6 +180,20 @@ class InstancesManager:
|
||||
@staticmethod
|
||||
def reset():
|
||||
InstancesManager.instances.clear()
|
||||
|
||||
@staticmethod
|
||||
def dynamic_get(parent: BaseInstance, component_type: str, instance_id: str):
|
||||
logger.debug(f"Dynamic get: {component_type=} {instance_id=}")
|
||||
cls = InstancesManager._get_class_name(component_type)
|
||||
fully_qualified_name = f"myfasthtml.controls.{cls}.{cls}"
|
||||
cls = get_class(fully_qualified_name)
|
||||
return cls(parent, instance_id)
|
||||
|
||||
@staticmethod
|
||||
def _get_class_name(component_type: str) -> str:
|
||||
component_type = component_type.replace("mf-", "")
|
||||
component_type = snake_to_pascal(component_type)
|
||||
return component_type
|
||||
|
||||
|
||||
RootInstance = SingleInstance(None, special_session, Ids.Root)
|
||||
|
||||
Reference in New Issue
Block a user