Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an autoinject option to provider. #9

Open
mwiatrzyk opened this issue Jul 19, 2023 · 0 comments
Open

Add an autoinject option to provider. #9

mwiatrzyk opened this issue Jul 19, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@mwiatrzyk
Copy link
Owner

I had following provider added to a project I work on:

@provider.provides('listener')
def start_listening():
    def listener(event):
        ...
    unsubscribe = subscribe(listener)
    try:
        yield
    finally:
        unsubscribe()

It is responsible for collecting events when application is running, and no direct use was expected for that provider. Currently, to start such listener, the provider needs to be directly injected to the code:

injector.inject('listener')

But this is not very handy, as the provider is not expected to return any object, and is error-prone when called too late.

The solution will be to add autoinject=True|False (with False as default) to the provides decorator, so the injector will automatically create such providers when scope is entered, and destroy when scope is left:

@provider.provides('listener', autoinject=True)
def start_listening():
    def listener(event):
        ...
    unsubscribe = subscribe(listener)
    try:
        yield
    finally:
        unsubscribe()
@mwiatrzyk mwiatrzyk added the enhancement New feature or request label Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant