UNPKG

3.36 kBSource Map (JSON)View Raw
1{"version":3,"file":"resolve_dependencies.js","sourceRoot":"","sources":["../../lib/util/resolve_dependencies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAK5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,MAAM,UAAU,mBAAmB;IAAC,gBAAwB;SAAxB,UAAwB,EAAxB,qBAAwB,EAAxB,IAAwB;QAAxB,2BAAwB;;IAC1D,IAAM,IAAI,GAAG,IAAI,GAAG,EAAe,CAAC;IAEpC,SAAS,QAAQ,CAAC,KAAkB;QAClC,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;YAAE,OAAO;QAE5B,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAEhB,mFAAmF;QACnF,kCAAkC;QAClC,kBAAkB,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC;aAChC,MAAM,CAAC,UAAC,CAAC,EAAE,CAA6B,IAAK,OAAA,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,iBAAiB,CAAC,EAA7B,CAA6B,EAAE,EAAiC,CAAC;aAC9G,MAAM,CAAC,UAAC,CAAC,EAAE,CAA4B,IAAK,OAAA,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,EAAxB,CAAwB,EAAE,EAA4B,CAAC;aACnG,OAAO,CAAC,UAAA,CAAC,IAAI,OAAA,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,KAAoB,CAAC,EAApC,CAAoC,CAAC,CAAC;IACxD,CAAC;IAED,KAAoB,UAAM,EAAN,iBAAM,EAAN,oBAAM,EAAN,IAAM,EAAE;QAAvB,IAAM,KAAK,eAAA;QACd,QAAQ,CAAC,KAAK,CAAC,CAAC;KACjB;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC","sourcesContent":["import { ReflectiveInjector } from '../reflective_injector';\nimport { ResolvedReflectiveProvider, ResolvedReflectiveFactory, ReflectiveDependency } from '../reflective_provider';\n\ntype Constructor = new (...args: any[]) => any;\n\n/**\n * This utility function receives a spread of injectable classes\n * and returns an array of all their dependencies. Also resolves\n * optional dependencies.\n *\n * ### Important:\n *\n * Dynamically resolving dependencies using this function\n * will not play nice with static analysis tools, including tree-shaking.\n * From a static analysis perspective, any dependencies which are only resolved\n * using this function will look as though they are not used (and will be\n * removed by tree-shaking). This *severely* limits the usefulness of this function.\n *\n * ### Example:\n *\n * ```typescript\n * class HTTP {}\n * class Database {}\n *\n * // commenting out the decorator because the `@` symbol is spoiling\n * // jsDoc rendering in vscode\n * // @Injectable()\n * class PersonService {\n * constructor(\n * private http: HTTP,\n * private database: Database,\n * ) {}\n * }\n *\n * // @Injectable()\n * class OrganizationService {\n * constructor(\n * private http: HTTP,\n * private personService: PersonService,\n * ) {}\n * }\n *\n * const injector = ReflectiveInjector.resolveAndCreate(\n * resolveDependencies(OrganizationService)\n * );\n *\n * const organizationService = injector.get(OrganizationService);\n * ```\n */\nexport function resolveDependencies(...inputs: Constructor[]) {\n const deps = new Set<Constructor>();\n\n function resolver(klass: Constructor) {\n if (deps.has(klass)) return;\n\n deps.add(klass);\n\n // resolve all dependencies of the provided class and run the `resolver()` function\n // on their constructor functions.\n ReflectiveInjector.resolve([klass])\n .reduce((a, x: ResolvedReflectiveProvider) => a.concat(x.resolvedFactories), [] as ResolvedReflectiveFactory[])\n .reduce((a, r: ResolvedReflectiveFactory) => a.concat(r.dependencies), [] as ReflectiveDependency[])\n .forEach(d => resolver(d.key.token as Constructor));\n }\n\n for (const input of inputs) {\n resolver(input);\n }\n\n return Array.from(deps);\n}\n"]}
\No newline at end of file