joist-framework / joist

A small (~2kb) library to help with the creation of web components and web component based applications
121 stars 6 forks source link

di: new bootstrap API (browser) #1018

Closed deebloo closed 1 month ago

deebloo commented 1 month ago

Currently the browser environment automatically bootstraps the root injector. You can provide your own "scope" by defining a custom element provider.

There is currently no way to scope only one part of the DOM tree without falling back to root. Another way to put that is that I don't have a clean way of creating multiple root injectors for different parts of my application.

My proposal is to define an imperative bootstrap API. To achieve the current behavior you do the following.

const app = new DOMInjector();

app.attach(document.body);

TECHNICALLY this could all be done directly with Injector. where attach just accepts object but it is really only something that needs to be done with DOM. Having a separate injector would also help isolate any future potential DOM specific logic.

While the current environment API is convenient it means you get a root injector whether you want one or not. This requires more explicit defining of different scopes. Would need to figure out if this change can be made without it being a breaking change. (I don't think so)

NOTE: will add examples of what I want to be able to do when I ma back at a computer.