diff --git a/lib/services/base-explorer.service.ts b/lib/services/base-explorer.service.ts index 89af422..2bfb50a 100644 --- a/lib/services/base-explorer.service.ts +++ b/lib/services/base-explorer.service.ts @@ -26,12 +26,28 @@ export class BaseExplorerService { modules: Module[], callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[], ): T[] { - const invokeMap = () => { - return modules.map((moduleRef) => { - const providers = [...moduleRef.providers.values()]; - return providers.map((wrapper) => callback(wrapper, moduleRef)); - }); + const visitedModules = new Set(); + + const unwrap = (moduleRef: Module) => { + // protection from circular recursion + if (visitedModules.has(moduleRef)) { + return []; + } else { + visitedModules.add(moduleRef); + } + + const providers = [...moduleRef.providers.values()]; + const defined = providers.map((wrapper) => callback(wrapper, moduleRef)); + + const imported: (T | T[])[] = moduleRef.imports?.size + ? [...moduleRef.imports.values()].reduce((prev, cur) => { + return [...prev, ...unwrap(cur)]; + }, []) + : []; + + return [...defined, ...imported]; }; - return flattenDeep(invokeMap()).filter(identity); + + return flattenDeep(modules.map(unwrap)).filter(identity); } }