mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-08-02 15:04:50 +08:00
* ComfyAPI Core v0.0.2 * Respond to PR feedback * Fix Python 3.9 errors * Fix missing backward compatibility proxy * Reorganize types a bit The input types, input impls, and utility types are now all available in the versioned API. See the change in `comfy_extras/nodes_video.py` for an example of their usage. * Remove the need for `--generate-api-stubs` * Fix generated stubs differing by Python version * Fix ruff formatting issues
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
from typing import Type, TypeVar
|
|
|
|
class SingletonMetaclass(type):
|
|
T = TypeVar("T", bound="SingletonMetaclass")
|
|
_instances = {}
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
if cls not in cls._instances:
|
|
cls._instances[cls] = super(SingletonMetaclass, cls).__call__(
|
|
*args, **kwargs
|
|
)
|
|
return cls._instances[cls]
|
|
|
|
def inject_instance(cls: Type[T], instance: T) -> None:
|
|
assert cls not in SingletonMetaclass._instances, (
|
|
"Cannot inject instance after first instantiation"
|
|
)
|
|
SingletonMetaclass._instances[cls] = instance
|
|
|
|
def get_instance(cls: Type[T], *args, **kwargs) -> T:
|
|
"""
|
|
Gets the singleton instance of the class, creating it if it doesn't exist.
|
|
"""
|
|
if cls not in SingletonMetaclass._instances:
|
|
SingletonMetaclass._instances[cls] = super(
|
|
SingletonMetaclass, cls
|
|
).__call__(*args, **kwargs)
|
|
return cls._instances[cls]
|
|
|
|
|
|
class ProxiedSingleton(object, metaclass=SingletonMetaclass):
|
|
def __init__(self):
|
|
super().__init__()
|