joist
joist copied to clipboard
Allow for easier debug of dependency graph
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
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.
Another though would be to add the ability to pass a debug function to inject(). Inject(Service, { debug })
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);
}
}