viridia / quill_v1

Reactive UI framework for Bevy game engine
MIT License
60 stars 7 forks source link

Dependency Injection vs. Hooks #4

Open viridia opened 9 months ago

viridia commented 9 months ago

Currently presenters use React-style hooks such as use_local() and use_resource() to get access to reactive data sources. However, these could also work using dependency injection, similar to the way Bevy systems work:

fn example(cx: Cx, counter: UseRes<Counter>) {
    Element::new().children(format!("The count is: {}", counter.count))
}

Note that we have to invent new injectable types here (UseRes instead of Res) because the existing types don't support dependency tracking. This means that presenters cannot simply be one-shot systems (I know folks are going to ask this.)

As a first pass, we should be able to implement:

Unfortunately, dependency injection cannot completely replace hooks, because some hooks require runtime parameters. For example, a hook to access an ECS component needs to know which entity we're accessing the component for.

Here's an online resource that explains how to implement dependency injection.