{"id":10006843,"date":"2022-11-16T23:58:55","date_gmt":"2022-11-16T15:58:55","guid":{"rendered":"https:\/\/ityet.com\/?p=10006843"},"modified":"2022-11-16T23:58:55","modified_gmt":"2022-11-16T15:58:55","slug":"vscode-for-web-%e6%b7%b1%e5%85%a5%e6%b5%85%e5%87%ba-%e4%be%9d%e8%b5%96%e6%b3%a8%e5%85%a5%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006843","title":{"rendered":"VSCode For Web \u6df1\u5165\u6d45\u51fa &#8212; \u4f9d\u8d56\u6ce8\u5165\u8bbe\u8ba1"},"content":{"rendered":"<p>\u5728\u9605\u8bfb VSCode \u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u6bcf\u4e00\u4e2a\u6a21\u5757\u4e2d\u90fd\u6709\u5927\u91cf\u88c5\u9970\u5668\u7684\u4f7f\u7528\uff0c\u7528\u6765\u88c5\u9970\u6a21\u5757\u4ee5\u53ca\u5176\u4e2d\u4f9d\u8d56\u7684\u6a21\u5757\u53d8\u91cf\u3002\u8fd9\u6837\u505a\u7684\u76ee\u7684\u662f\u4ec0\u4e48\u5462\uff1f\u5728\u8fd9\u4e00\u7bc7\u4e2d\u6211\u4eec\u6765\u8be6\u7ec6\u5206\u6790\u4e00\u4e0b\u3002<\/p>\n<h2>\u4f9d\u8d56\u6ce8\u5165\u4ecb\u7ecd<\/h2>\n<p>\u5982\u679c\u6709\u8fd9\u6837\u4e00\u4e2a\u6a21\u5757 A\uff0c\u5b83\u7684\u5b9e\u73b0\u4f9d\u8d56\u53e6\u4e00\u4e2a\u6a21\u5757 B \u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5e94\u8be5\u5982\u4f55\u8bbe\u8ba1\u5462\uff1f\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 A \u6a21\u5757\u7684\u6784\u9020\u51fd\u6570\u4e2d\u5b9e\u4f8b\u5316\u6a21\u5757 B\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u6a21\u5757 A \u5185\u90e8\u4f7f\u7528\u6a21\u5757 B \u7684\u80fd\u529b\u4e86\u3002<\/p>\n<pre><code class=\"ts\">class A {\n  constructor() {\n    this.b = new B();\n  }\n}\n\nclass B {}\n\nconst a = new A();<\/code><\/pre>\n<p>\u4f46\u662f\u8fd9\u6837\u505a\u6709\u4e24\u4e2a\u95ee\u9898\uff0c\u4e00\u662f\u6a21\u5757 A \u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u624b\u52a8\u5b9e\u4f8b\u5316\u6a21\u5757 B\uff0c\u800c\u4e14\u5982\u679c\u6a21\u5757 B \u7684\u4f9d\u8d56\u5173\u7cfb\u53d1\u751f\u53d8\u5316\uff0c\u90a3\u4e48\u4e5f\u9700\u8981\u4fee\u6539\u6a21\u5757 A \u7684\u6784\u9020\u51fd\u6570\uff0c\u5bfc\u81f4\u4ee3\u7801\u8026\u5408\u3002<\/p>\n<p>\u4e8c\u662f\u5728\u590d\u6742\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5728\u5b9e\u4f8b\u5316\u6a21\u5757 A \u65f6\uff0c\u96be\u4ee5\u5224\u65ad\u6a21\u5757 B \u662f\u5426\u88ab\u5176\u4ed6\u6a21\u5757\u4f9d\u8d56\u800c\u5df2\u7ecf\u5b9e\u4f8b\u5316\u8fc7\u4e86\uff0c\u4ece\u800c\u53ef\u80fd\u5c06\u6a21\u5757 B \u591a\u6b21\u5b9e\u4f8b\u5316\u3002\u82e5\u6a21\u5757 B \u8f83\u91cd\u6216\u8005\u9700\u8981\u4e3a\u5355\u4f8b\u8bbe\u8ba1\uff0c\u8fd9\u5c06\u5e26\u6765\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u66f4\u597d\u7684\u65b9\u5f0f\u662f\uff0c\u5c06\u6240\u6709\u6a21\u5757\u7684\u5b9e\u4f8b\u5316\u4ea4\u7ed9\u5916\u5c42\u6846\u67b6\uff0c\u7531\u6846\u67b6\u7edf\u4e00\u7ba1\u7406\u6a21\u5757\u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u89e3\u51b3\u4e0a\u8ff0\u4e24\u4e2a\u95ee\u9898\u3002<\/p>\n<pre><code class=\"ts\">class A {\n  constructor(private b: B) {\n    this.b = b;\n  }\n}\n\nclass B {}\n\nclass C {\n  constructor(private a: A, private b: B) {\n    this.b = b;\n  }\n}\n\nconst b = new B();\nconst a = new A(b);\nconst c = new C(a, b);<\/code><\/pre>\n<p>\u8fd9\u79cd\u5c06\u4f9d\u8d56\u5bf9\u8c61\u901a\u8fc7\u5916\u90e8\u6ce8\u5165\uff0c\u907f\u514d\u5728\u6a21\u5757\u5185\u90e8\u5b9e\u4f8b\u5316\u4f9d\u8d56\u7684\u65b9\u5f0f\uff0c\u79f0\u4e3a\u4f9d\u8d56\u6ce8\u5165 (Dependencies Inject, \u7b80\u79f0 DI)\u3002\u8fd9\u5728\u8f6f\u4ef6\u5de5\u7a0b\u4e2d\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u6211\u4eec\u5728 Java \u7684 Spring\uff0cJS \u7684 Angular\uff0cNode \u7684 NestJS \u7b49\u6846\u67b6\u4e2d\u90fd\u53ef\u4ee5\u770b\u5230\u8fd9\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u7684\u5e94\u7528\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7531\u4e8e\u6a21\u5757\u4f17\u591a\uff0c\u4f9d\u8d56\u590d\u6742\uff0c\u6211\u4eec\u5f88\u96be\u50cf\u4e0a\u9762\u7684\u4f8b\u5b50\u4e00\u6837\uff0c\u89c4\u5212\u51fa\u6765\u6bcf\u4e2a\u6a21\u5757\u7684\u5b9e\u4f8b\u5316\u65f6\u673a\uff0c\u4ece\u800c\u7f16\u5199\u6a21\u5757\u5b9e\u4f8b\u5316\u987a\u5e8f\u3002\u5e76\u4e14\uff0c\u8bb8\u591a\u6a21\u5757\u53ef\u80fd\u5e76\u4e0d\u9700\u8981\u7b2c\u4e00\u65f6\u95f4\u88ab\u521b\u5efa\uff0c\u9700\u8981<strong>\u6309\u9700\u5b9e\u4f8b\u5316<\/strong>\uff0c\u56e0\u6b64\uff0c\u7c97\u66b4\u7684\u7edf\u4e00\u5b9e\u4f8b\u5316\u662f\u4e0d\u53ef\u53d6\u7684\u3002<\/p>\n<p>\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u7edf\u4e00\u7684\u6846\u67b6\u6765\u5206\u6790\u5e76\u7ba1\u7406\u6240\u6709\u6a21\u5757\u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b\uff0c\u8fd9\u5c31\u662f\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u501f\u52a9\u4e8e TypeScript \u7684\u88c5\u9970\u5668\u80fd\u529b\uff0cVSCode \u5b9e\u73b0\u4e86\u4e00\u4e2a\u6781\u4e3a\u8f7b\u91cf\u5316\u7684\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u6765\u7b80\u5355\u5b9e\u73b0\u4e00\u4e0b\uff0c\u89e3\u5f00\u8fd9\u4e2a\u5de7\u5999\u8bbe\u8ba1\u7684\u795e\u79d8\u9762\u7eb1\u3002<\/p>\n<h2>\u6700\u7b80\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u8bbe\u8ba1<\/h2>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u53ea\u9700\u8981\u4e24\u6b65\uff0c\u4e00\u4e2a\u662f\u5c06\u6a21\u5757\u58f0\u660e\u5e76\u6ce8\u518c\u5230\u6846\u67b6\u4e2d\u8fdb\u884c\u7ba1\u7406\uff0c\u53e6\u4e00\u4e2a\u662f\u5728\u6a21\u5757\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u58f0\u660e\u6240\u9700\u8981\u4f9d\u8d56\u7684\u6a21\u5757\u6709\u54ea\u4e9b\u3002<\/p>\n<p>\u6211\u4eec\u5148\u6765\u770b\u6a21\u5757\u7684\u6ce8\u518c\u8fc7\u7a0b\uff0c\u8fd9\u9700\u8981 TypeScript \u7684\u7c7b\u88c5\u9970\u5668\u80fd\u529b\u3002\u6211\u4eec\u5728\u6ce8\u5165\u65f6\uff0c\u53ea\u9700\u8981\u5224\u65ad\u6a21\u5757\u662f\u5426\u5df2\u7ecf\u6ce8\u518c\uff0c\u5982\u679c\u6ca1\u6709\u6ce8\u518c\uff0c\u5c06\u6a21\u5757\u7684 id\uff08\u8fd9\u91cc\u7b80\u5316\u4e3a\u6a21\u5757 Class \u540d\u79f0\uff09\u4e0e\u7c7b\u578b\u4f20\u5165\u5373\u53ef\u5b8c\u6210\u5355\u4e2a\u6a21\u5757\u7684\u6ce8\u518c\u3002<\/p>\n<pre><code class=\"ts\">export function Injectable(): ClassDecorator {\n  return (Target: Class): any =&gt; {\n    if (!collection.providers.has(Target.name)) {\n      collection.providers.set(Target.name, target);\n    }\n    return target;\n  };\n}<\/code><\/pre>\n<p>\u4e4b\u540e\u6211\u4eec\u518d\u6765\u770b\u770b\u6a21\u5757\u662f\u5982\u4f55\u58f0\u660e\u4f9d\u8d56\u7684\uff0c\u8fd9\u9700\u8981 TypeScript \u7684\u5c5e\u6027\u88c5\u9970\u5668\u80fd\u529b\u3002\u6211\u4eec\u5728\u6ce8\u5165\u65f6\uff0c\u5148\u5224\u65ad\u4f9d\u8d56\u7684\u6a21\u5757\u662f\u5426\u5df2\u7ecf\u88ab\u5b9e\u4f8b\u5316\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u5c06\u4f9d\u8d56\u6a21\u5757\u8fdb\u884c\u5b9e\u4f8b\u5316\uff0c\u5e76\u5b58\u5165\u6846\u67b6\u4e2d\u7ba1\u7406\u3002\u6700\u7ec8\u8fd4\u56de\u5df2\u7ecf\u88ab\u5b9e\u4f8b\u5316\u5b8c\u6210\u7684\u6a21\u5757\u5b9e\u4f8b\u3002<\/p>\n<pre><code class=\"ts\">export function Inject(): PropertyDecorator {\n  return (target: Property, propertyKey: string) =&gt; {\n\n    const instance = collection.dependencies.get(propertyKey);\n    if (!instance) {\n      const DependencyProvider: Class = collection.providers.get(propertyKey);\n      collection.dependencies.set(propertyKey, new DependencyProvider());\n    }\n\n    target[propertyKey] = collection.dependencies.get(propertyKey);\n  };\n}<\/code><\/pre>\n<p>\u6700\u540e\u53ea\u9700\u8981\u4fdd\u8bc1\u6846\u67b6\u672c\u8eab\u5728\u9879\u76ee\u8fd0\u884c\u524d\u5b8c\u6210\u5b9e\u4f8b\u5316\u5373\u53ef\u3002\uff08\u5728\u4f8b\u5b50\u4e2d\u8868\u793a\u4e3a injector\uff09<\/p>\n<pre><code class=\"ts\">export class ServiceCollection {\n  readonly providers = new Map&lt;string, any&gt;();\n  readonly dependencies = new Map&lt;string, any&gt;();\n}\n\nconst collection = new ServiceCollection();\nexport default collection;<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4e00\u4e2a\u6700\u7b80\u5316\u7684\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u5c31\u5b8c\u6210\u4e86\u3002\u7531\u4e8e\u4fdd\u5b58\u4e86\u6a21\u5757\u7684\u7c7b\u578b\u4e0e\u5b9e\u4f8b\uff0c\u5b83\u5b9e\u73b0\u4e86\u6a21\u5757\u7684\u6309\u9700\u5b9e\u4f8b\u5316\uff0c\u65e0\u9700\u5728\u9879\u76ee\u542f\u52a8\u65f6\u5c31\u521d\u59cb\u5316\u6240\u6709\u6a21\u5757\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u8c03\u7528\u5b83\uff0c\u4ee5\u4e0a\u9762\u4e3e\u51fa\u7684\u4f8b\u5b50\u4e3a\u4f8b\uff1a<\/p>\n<pre><code class=\"ts\">@injectable()\nclass A {\n  constructor(@inject() private b: B) {\n    this.b = b;\n  }\n}\n\n@injectable()\nclass B {}\n\nclass C {\n  constructor(@inject() private a: A, @inject() private b: B) {\n    this.b = b;\n  }\n}\n\nconst c = new C();<\/code><\/pre>\n<p>\u65e0\u9700\u77e5\u6653\u6a21\u5757 A\uff0cB \u7684\u5b9e\u4f8b\u5316\u65f6\u673a\uff0c\u76f4\u63a5\u521d\u59cb\u5316\u4efb\u4f55\u4e00\u4e2a\u6a21\u5757\uff0c\u6846\u67b6\u4f1a\u81ea\u52a8\u5e2e\u4f60\u627e\u5230\u5e76\u5b9e\u4f8b\u5316\u597d\u6240\u6709\u4f9d\u8d56\u7684\u6a21\u5757\u3002<\/p>\n<h2>VSCode \u7684\u4f9d\u8d56\u6536\u96c6\u5b9e\u73b0<\/h2>\n<p>\u4e0a\u9762\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u7684\u6700\u7b80\u5b9e\u73b0\u3002\u4f46\u5f53\u6211\u4eec\u771f\u6b63\u9605\u8bfb VSCode \u7684\u6e90\u7801\u65f6\uff0c\u6211\u4eec\u53d1\u73b0 VSCode \u4e2d\u7684\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u8c8c\u4f3c\u5e76\u4e0d\u662f\u8fd9\u6837\u6d88\u8d39\u7684\u3002<\/p>\n<p>\u4f8b\u5982\u5728\u4e0b\u9762\u8fd9\u6bb5\u9274\u6743\u670d\u52a1\u4e2d\uff0c\u6211\u4eec\u53d1\u73b0\u8be5\u7c7b\u5e76\u6ca1\u6709<code>@injectable()<\/code>\u4f5c\u4e3a\u7c7b\u7684\u4f9d\u8d56\u6536\u96c6\uff0c\u5e76\u4e14\u4f9d\u8d56\u670d\u52a1\u4e5f\u76f4\u63a5\u7528\u5176\u7c7b\u540d\u4f5c\u4e3a\u4fee\u9970\u5668\uff0c\u800c\u4e0d\u662f<code>@inject()<\/code>\u3002<\/p>\n<pre><code class=\"ts\">\/\/ src\/vs\/workbench\/services\/authentication\/browser\/authenticationService.ts\nexport class AuthenticationService extends Disposable implements IAuthenticationService {\n  constructor(\n    @IActivityService private readonly activityService: IActivityService,\n    @IExtensionService private readonly extensionService: IExtensionService,\n    @IStorageService private readonly storageService: IStorageService,\n    @IRemoteAgentService private readonly remoteAgentService: IRemoteAgentService,\n    @IDialogService private readonly dialogService: IDialogService,\n    @IQuickInputService private readonly quickInputService: IQuickInputService\n  ) {}\n}<\/code><\/pre>\n<p>\u5176\u5b9e\u8fd9\u91cc\u7684\u4fee\u9970\u7b26\u5e76\u4e0d\u662f\u771f\u6b63\u6307\u5411\u7c7b\u540d\uff0c\u800c\u662f\u4e00\u4e2a\u540c\u540d\u7684\u8d44\u6e90\u63cf\u8ff0\u7b26 id(VSCode \u4e2d\u79f0\u4e4b\u4e3a <code>ServiceIdentifier<\/code>)\uff0c\u901a\u5e38\u4f7f\u7528\u5b57\u7b26\u4e32\u6216 Symbol \u6807\u8bc6\u3002<\/p>\n<p>\u901a\u8fc7 <code>ServiceIdentifier<\/code> \u4f5c\u4e3a id\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u7c97\u66b4\u5730\u901a\u8fc7\u7c7b\u540d\u79f0\u4f5c\u4e3a id \u6ce8\u518c Service\uff0c\u6709\u5229\u4e8e\u5904\u7406\u9879\u76ee\u4e2d\u4e00\u4e2a interface \u53ef\u80fd\u5b58\u5728\u591a\u6001\u5b9e\u73b0\uff0c\u9700\u8981\u540c\u65f6\u591a\u4e2a\u540c\u540d\u7c7b\u5b9e\u4f8b\u7684\u95ee\u9898\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5728\u6784\u9020 <code>ServiceIdentifier<\/code> \u65f6\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u5c06\u8be5\u7c7b\u58f0\u660e\u6ce8\u5165\u6846\u67b6\uff0c\u800c\u65e0\u9700<code>@injectable()<\/code>\u663e\u793a\u8c03\u7528\u4e86\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u8fd9\u6837\u4e00\u4e2a <code>ServiceIdentifier<\/code> \u8be5\u5982\u4f55\u6784\u9020\u5462\uff1f<\/p>\n<pre><code class=\"ts\">\/\/ src\/vs\/platform\/instantiation\/common\/instantiation.ts\n\/**\n * The *only* valid way to create a {{ServiceIdentifier}}.\n *\/\nexport function createDecorator&lt;T&gt;(serviceId: string): ServiceIdentifier&lt;T&gt; {\n\n  if (_util.serviceIds.has(serviceId)) {\n    return _util.serviceIds.get(serviceId)!;\n  }\n\n  const id = &lt;any&gt;function (target: Function, key: string, index: number): any {\n    if (arguments.length !== 3) {\n      throw new Error('@IServiceName-decorator can only be used to decorate a parameter');\n    }\n    storeServiceDependency(id, target, index);\n  };\n\n  id.toString = () =&gt; serviceId;\n\n  _util.serviceIds.set(serviceId, id);\n  return id;\n}\n\n\/\/ \u88ab ServiceIdentifier \u88c5\u9970\u7684\u7c7b\u5728\u8fd0\u884c\u65f6\uff0c\u5c06\u6536\u96c6\u8be5\u7c7b\u7684\u4f9d\u8d56\uff0c\u6ce8\u5165\u5230\u6846\u67b6\u4e2d\u3002\nfunction storeServiceDependency(id: Function, target: Function, index: number): void {\n  if ((target as any)[_util.DI_TARGET] === target) {\n    (target as any)[_util.DI_DEPENDENCIES].push({ id, index });\n  } else {\n    (target as any)[_util.DI_DEPENDENCIES] = [{ id, index }];\n    (target as any)[_util.DI_TARGET] = target;\n  }\n}<\/code><\/pre>\n<p>\u6211\u4eec\u4ec5\u9700\u901a\u8fc7<code>createDecorator<\/code>\u65b9\u6cd5\u4e3a\u7c7b\u521b\u5efa\u4e00\u4e2a\u552f\u4e00\u7684<code>ServiceIdentifier<\/code>\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4fee\u9970\u7b26\u5373\u53ef\u3002<\/p>\n<p>\u4ee5\u4e0a\u9762\u7684 AuthenticationService \u4e3a\u4f8b\uff0c\u82e5\u6240\u4f9d\u8d56\u7684 ActivityService \u9700\u8981\u53d8\u66f4\u591a\u6001\u5b9e\u73b0\uff0c\u4ec5\u9700\u4fee\u6539 <code>ServiceIdentifier<\/code> \u4fee\u9970\u7b26\u786e\u5b9a\u5b9e\u73b0\u65b9\u5f0f\u5373\u53ef\uff0c\u65e0\u9700\u66f4\u6539\u4e1a\u52a1\u7684\u8c03\u7528\u4ee3\u7801\u3002<\/p>\n<pre><code class=\"ts\">export const IActivityServicePlanA = createDecorator&lt;IActivityService&gt;(&quot;IActivityServicePlanA&quot;);\nexport const IActivityServicePlanB = createDecorator&lt;IActivityService&gt;(&quot;IActivityServicePlanB&quot;);\nexport interface IActivityService {...}\n\nexport class AuthenticationService {\n  constructor(\n    @IActivityServicePlanA private readonly activityService: IActivityService,\n  ) {}\n}<\/code><\/pre>\n<h2>\u5faa\u73af\u4f9d\u8d56\u95ee\u9898<\/h2>\n<p>\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u662f\u6709\u53ef\u80fd\u5b58\u5728\u5faa\u73af\u4f9d\u8d56\u7684\uff0c\u6bd4\u5982 A \u4f9d\u8d56 B\uff0cB \u4f9d\u8d56 A\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8fdb\u884c\u4e24\u4e2a\u6a21\u5757\u7684\u5b9e\u4f8b\u5316\u4f1a\u9020\u6210\u6b7b\u5faa\u73af\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5728\u6846\u67b6\u4e2d\u52a0\u5165\u5faa\u73af\u4f9d\u8d56\u68c0\u6d4b\u673a\u5236\u6765\u8fdb\u884c\u89c4\u907f\u3002<\/p>\n<p>\u672c\u8d28\u4e0a\uff0c\u4e00\u4e2a\u5065\u5eb7\u7684\u6a21\u5757\u4f9d\u8d56\u5173\u7cfb\u5c31\u662f\u4e00\u4e2a\u6709\u5411\u65e0\u73af\u56fe\uff08DAG\uff09\uff0c\u6211\u4eec\u4e4b\u524d\u4ecb\u7ecd\u8fc7\u6709\u5411\u65e0\u73af\u56fe\u5728 excel \u8868\u683c\u51fd\u6570\u4e2d\u7684\u5e94\u7528\uff0c\u653e\u5728\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\u7684\u8bbe\u8ba1\u4e2d\u4e5f\u540c\u6837\u9002\u7528\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08DFS\uff09\u6765\u68c0\u6d4b\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5982\u679c\u53d1\u73b0\u5b58\u5728\u5faa\u73af\u4f9d\u8d56\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"ts\">\/\/ src\/vs\/platform\/instantiation\/common\/instantiationService.ts\nwhile (true) {\n  let roots = graph.roots();\n\n  \/\/ if there is no more roots but still\n  \/\/ nodes in the graph we have a cycle\n  if (roots.length === 0) {\n    if (graph.length !== 0) {\n      throwCycleError();\n    }\n    break;\n  }\n\n  for (let root of roots) {\n    \/\/ create instance and overwrite the service collections\n    const instance = this._createInstance(root.data.desc, []);\n    this._services.set(root.data.id, instance);\n    graph.removeNode(root.data);\n  }\n}<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u901a\u8fc7\u83b7\u53d6\u56fe\u8282\u70b9\u7684\u51fa\u5ea6\uff0c\u5c06\u8be5\u7c7b\u7684\u5168\u90e8\u4f9d\u8d56\u63d0\u53d6\u51fa\u6765\u4f5c\u4e3aroots\uff0c\u7136\u540e\u9010\u4e2a\u5b9e\u4f8b\u5316\uff0c\u5e76\u4ece\u9014\u4e2d\u5265\u79bb\u8be5\u4f9d\u8d56\u8282\u70b9\u3002\u7531\u4e8e\u4f9d\u8d56\u6811\u7684\u6784\u5efa\u662f\u9010\u5c42\u4f9d\u8d56\u7684\uff0c\u56e0\u6b64\u6309\u987a\u5e8f\u5b9e\u4f8b\u5316\u5373\u53ef\u3002\u5f53\u53d1\u73b0\u8be5\u7c7b\u7684\u6240\u6709\u4f9d\u8d56\u90fd\u88ab\u5b9e\u4f8b\u5316\u540e\uff0c\u56fe\u4e2d\u4ecd\u5b58\u5728\u8282\u70b9\uff0c\u5219\u8ba4\u4e3a\u5b58\u5728\u5faa\u73af\u4f9d\u8d56\uff0c\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u7bc7\u6587\u7ae0\u7b80\u8981\u4ecb\u7ecd\u5e76\u5b9e\u73b0\u4e86\u4e00\u4e2a\u4f9d\u8d56\u6ce8\u5165\u6846\u67b6\uff0c\u5e76\u89e3\u6790\u4e86VSCode\u5728\u5b9e\u9645\u95ee\u9898\u4e0a\u505a\u51fa\u7684\u4e00\u4e9b\u6539\u8fdb\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a VSCode \u7684\u4f9d\u8d56\u6ce8\u5165\u80fd\u529b\u8fd8\u6709\u5f88\u591a\u7ec6\u8282\u9700\u8981\u5904\u7406\u3002\u4f8b\u5982\u5f02\u6b65\u5b9e\u4f8b\u5316\u80fd\u529b\u652f\u6301\uff0c\u901a\u8fc7\u5c01\u88c5 Deferred \u7c7b\u53d6\u5f97Promise\u6267\u884c\u72b6\u6001\uff0c\u7b49\u7b49\uff0c\u5728\u6b64\u5c31\u4e0d\u4e00\u4e00\u5c55\u5f00\u4e86\u3002\u611f\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u53c2\u8003 VSCode \u6e90\u7801\uff1a<a href=\"https:\/\/segmentfault.com\/a\/src\/vs\/platform\/instantiation\/common\/instantiationService.ts\">src\/vs\/platform\/instantiation\/common\/instantiationService.ts<\/a>\uff0c\u505a\u66f4\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u3002<\/p>\n<h2>\u9644\u5f55<\/h2>\n<p>\u6700\u7b80 DI \u6846\u67b6\u5b8c\u6574 demo\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=1gOdSVJgasd0eJuB1OEmwg%3D%3D.JJ%2BDa9Z4A1ZDlCs%2BGqbnp%2F9cRPMzNEEZHR%2F9ZthF5I%2BFDItjhvZ0ML%2Bqqx%2F7SNo2k5FDoY6UchRKwacoU%2BJb5g%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/realDuang\/dependencies-injector-demo<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042817181\">https:\/\/segmentfault.com\/a\/1190000042817181<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9605\u8bfb VSCode \u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u6bcf\u4e00\u4e2a\u6a21\u5757\u4e2d\u90fd\u6709\u5927\u91cf\u88c5\u9970\u5668\u7684\u4f7f\u7528\uff0c\u7528\u6765\u88c5\u9970\u6a21\u5757\u4ee5\u53ca\u5176\u4e2d\u4f9d\u8d56\u7684\u6a21\u5757 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006843","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006843"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006843\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}