joist-framework / joist

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

Allow for easier debug of dependency graph #960

Open deebloo opened 4 months ago

deebloo commented 4 months ago

To help make it easier to debug injectors and their dependencies there should be an optional API that will describe how a particular series was resolved.

Initial thought it is export an additional injectDebug() function. (To make it tree shakeable)

This function would log the dependency tree for the injected service.

Cc @Phoscur

Phoscur commented 4 months ago

Initial thought it is export an additional injectDebug() function. (To make it tree shakeable)

That sounds easiest to maintain, but it might be limited by or breaking encapsulation in places?

Replacing the implementation of entire core files would be a potential alternative I've seen in other projects. Applying that here it would mean: Copy injector.ts: injector.debug.ts, sprinkle in many console.debug statements or save debug info into arrays to be inspected later ... e.g. especially where undefined is returned. Although it is worse to maintain (code duplication), it does not tempt you to break encapsulation or rather gives you full freedom of debuggability.

deebloo commented 4 months ago

Another though would be to add the ability to pass a debug function to inject(). Inject(Service, { debug })

Phoscur commented 3 months ago

One debugging usecase, counting instances:

// in some element:
static providers = [{ provide: EconomyService, use: EconomyService }]; // force a new instance for each element

// then I need to decorate my service like this to debug the amount of actual instances created (and alive?!):
@injectable
export class EconomyService {
  static count = 0;
  constructor() {
    EconomyService.count++;
    console.log('E', EconomyService.count);
  }
}