Parent is now mandatory when creating a new BaseInstance class

This commit is contained in:
2025-11-16 17:46:44 +01:00
parent edcd3ae1a8
commit e286b60348
14 changed files with 55 additions and 38 deletions

View File

@@ -1,4 +1,5 @@
import uuid
from typing import Self
from myfasthtml.controls.helpers import Ids
@@ -17,10 +18,11 @@ class BaseInstance:
Base class for all instances (manageable by InstancesManager)
"""
def __init__(self, session: dict, prefix: str, _id: str, auto_register: bool = True):
def __init__(self, session: dict, prefix: str, _id: str, parent: Self, auto_register: bool = True):
self._session = session
self._id = _id
self._prefix = prefix
self._parent = parent
if auto_register:
InstancesManager.register(session, self)
@@ -39,8 +41,8 @@ class SingleInstance(BaseInstance):
Base class for instances that can only have one instance at a time.
"""
def __init__(self, session: dict, prefix: str, auto_register: bool = True):
super().__init__(session, prefix, prefix, auto_register)
def __init__(self, session: dict, prefix: str, parent, auto_register: bool = True):
super().__init__(session, prefix, prefix, parent, auto_register)
class UniqueInstance(BaseInstance):
@@ -49,8 +51,8 @@ class UniqueInstance(BaseInstance):
Does not throw exception if the instance already exists, it simply overwrites it.
"""
def __init__(self, session: dict, prefix: str, auto_register: bool = True):
super().__init__(session, prefix, prefix, auto_register)
def __init__(self, prefix: str, parent: BaseInstance, auto_register: bool = True):
super().__init__(parent.get_session(), prefix, prefix, parent, auto_register)
self._prefix = prefix
@@ -59,8 +61,8 @@ class MultipleInstance(BaseInstance):
Base class for instances that can have multiple instances at a time.
"""
def __init__(self, session: dict, prefix: str, auto_register: bool = True, _id=None):
super().__init__(session, prefix, _id or f"{prefix}-{str(uuid.uuid4())}", auto_register)
def __init__(self, prefix: str, parent: BaseInstance, auto_register: bool = True, _id=None):
super().__init__(parent.get_session(), prefix, _id or f"{prefix}-{str(uuid.uuid4())}", parent, auto_register)
self._prefix = prefix
@@ -84,12 +86,13 @@ class InstancesManager:
return instance
@staticmethod
def get(session: dict, instance_id: str, instance_type: type = None, *args, **kwargs):
def get(session: dict, instance_id: str, instance_type: type = None, parent: BaseInstance = None, *args, **kwargs):
"""
Get or create an instance of the given type (from its id)
:param session:
:param instance_id:
:param instance_type:
:param parent:
:param args:
:param kwargs:
:return:
@@ -100,7 +103,9 @@ class InstancesManager:
return InstancesManager.instances[key]
except KeyError:
if instance_type:
return instance_type(session, *args, **kwargs) # it will be automatically registered
if not issubclass(instance_type, SingleInstance):
assert parent is not None, "Parent instance must be provided if not SingleInstance"
return instance_type(session, parent=parent, *args, **kwargs) # it will be automatically registered
else:
raise
@@ -119,3 +124,6 @@ class InstancesManager:
@staticmethod
def reset():
return InstancesManager.instances.clear()
RootInstance = SingleInstance(special_session, Ids.Root, None)