mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 23:14:39 +03:00
scan modules recursively in flatMap
This commit is contained in:
parent
ec69dff7ee
commit
2718e899e9
@ -26,12 +26,28 @@ export class BaseExplorerService {
|
|||||||
modules: Module[],
|
modules: Module[],
|
||||||
callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[],
|
callback: (instance: InstanceWrapper, moduleRef: Module) => T | T[],
|
||||||
): T[] {
|
): T[] {
|
||||||
const invokeMap = () => {
|
const visitedModules = new Set<Module>();
|
||||||
return modules.map((moduleRef) => {
|
|
||||||
|
const unwrap = (moduleRef: Module) => {
|
||||||
|
// protection from circular recursion
|
||||||
|
if (visitedModules.has(moduleRef)) {
|
||||||
|
return [];
|
||||||
|
} else {
|
||||||
|
visitedModules.add(moduleRef);
|
||||||
|
}
|
||||||
|
|
||||||
const providers = [...moduleRef.providers.values()];
|
const providers = [...moduleRef.providers.values()];
|
||||||
return providers.map((wrapper) => callback(wrapper, moduleRef));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user