Skip to content

Brainstorm alternative approaches to support the singleton pattern #1

@dnut

Description

@dnut

Any components of the app (whether added via init, services, components) will be instantiated separately for each component that depends on it. If you want some component to be shared as just a single instance, like a database connection, you need to do multiple manual steps to make that available. There is not first class support for a singleton pattern.

If you want a component to be instantiated only a single time, it needs to be a field in your app wrapped in an Arc, and you need to manually construct it. Then, you need to also declare it in the provides section with a manual access of that field, or manually implement Provides<Arc<...>>. That means the singleton needs to be defined in one place, then declared as a component of the application in at least three other places. Maybe another design could consolidate that somewhat.

The current approach for singletons is workable, so it shouldn't be replaced with something extremely obfuscated and complex just for the sake of reducing lines of code. It should be obvious how it works under the hood.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions