Repository: artusjs/core
Pull request #121 (2022-06-30 09:24:30)

fix: check md.value before container.set
原因参见：https://github.com/artusjs/injection/pull/29


Patch:
diff --git a/package.json b/package.json
index 3f597ef..ddc6c63 100644
--- a/package.json
+++ b/package.json
@@ -56,8 +56,8 @@
     "typescript": "^4.7.2"
   },
   "dependencies": {
-    "@artus/injection": "^0.2.0",
-    "@artus/pipeline": "^0.2.1",
+    "@artus/injection": "^0.2.5",
+    "@artus/pipeline": "^0.2.2",
     "deepmerge": "^4.2.2",
     "js-yaml": "^4.1.0",
     "minimatch": "^5.0.1"
diff --git a/src/loader/impl/module.ts b/src/loader/impl/module.ts
index 6556f1e..6cefa38 100644
--- a/src/loader/impl/module.ts
+++ b/src/loader/impl/module.ts
@@ -20,7 +20,10 @@ class ModuleLoader implements Loader {
     if (item.id) {
       opts.id = item.id;
     }
-    this.container.set(opts);
+
+    if(!this.container.hasValue(opts)) {
+      this.container.set(opts);
+    }
   }
 }
 


Test patch:
diff --git a/test/fixtures/custom_instance/bootstrap.ts b/test/fixtures/custom_instance/bootstrap.ts
new file mode 100644
index 0000000..0593f91
--- /dev/null
+++ b/test/fixtures/custom_instance/bootstrap.ts
@@ -0,0 +1,29 @@
+import {
+  ArtusApplication,
+  ApplicationLifecycle, LifecycleHookUnit, LifecycleHook,
+  WithApplication, WithContainer
+} from '../../../src/index';
+
+import { Container } from '@artus/injection';
+import Custom from './custom';
+
+@LifecycleHookUnit()
+export default class MyLifecycle implements ApplicationLifecycle {
+  app: ArtusApplication;
+  container: Container;
+
+  constructor(@WithApplication() app: ArtusApplication, @WithContainer() container) {
+    this.app = app;
+    this.container = container;
+  }
+
+  @LifecycleHook()
+  configDidLoad() {
+    this.container.set({ id: Custom, value: new Custom('foo') });
+  }
+
+  @LifecycleHook()
+  async beforeClose() {
+    console.log(this.container.get(Custom))
+  }
+}
\ No newline at end of file
diff --git a/test/fixtures/custom_instance/config/config.default.ts b/test/fixtures/custom_instance/config/config.default.ts
new file mode 100644
index 0000000..7c645e4
--- /dev/null
+++ b/test/fixtures/custom_instance/config/config.default.ts
@@ -0,0 +1,1 @@
+export default {};
\ No newline at end of file
diff --git a/test/fixtures/custom_instance/custom.ts b/test/fixtures/custom_instance/custom.ts
new file mode 100644
index 0000000..03cee03
--- /dev/null
+++ b/test/fixtures/custom_instance/custom.ts
@@ -0,0 +1,14 @@
+import { Injectable } from "../../../src/index";
+
+@Injectable()
+export default class Custom {
+  private name: string;
+
+  constructor(name: string) {
+    this.name = name;
+  }
+
+  getName(): string {
+    return this.name;
+  }
+}
\ No newline at end of file
diff --git a/test/fixtures/custom_instance/index.ts b/test/fixtures/custom_instance/index.ts
new file mode 100644
index 0000000..dab3467
--- /dev/null
+++ b/test/fixtures/custom_instance/index.ts
@@ -0,0 +1,17 @@
+import { Scanner, ArtusApplication } from '../../../src/index';
+
+export async function createApp() {
+  const baseDir = __dirname;
+  const scanner = new Scanner({
+    needWriteFile: false,
+    configDir: 'config',
+    extensions: ['.ts'],
+  });
+  const manifest = await scanner.scan(baseDir);
+
+  const app = new ArtusApplication();
+  await app.load(manifest.default, baseDir);
+  await app.run();
+
+  return app;
+}
diff --git a/test/loader.test.ts b/test/loader.test.ts
index 8ab7554..4669b78 100644
--- a/test/loader.test.ts
+++ b/test/loader.test.ts
@@ -4,6 +4,8 @@ import assert from 'assert';
 import path from 'path';
 import { Container } from '@artus/injection';
 import { LoaderFactory } from '../src';
+import { createApp } from './fixtures/custom_instance/index';
+import Custom from './fixtures/custom_instance/custom';
 
 describe('test/loader.test.ts', () => {
   describe('module with ts', () => {
@@ -16,6 +18,7 @@ describe('test/loader.test.ts', () => {
       assert((container.get('testServiceA') as any).testMethod() === 'Hello Artus');
     });
   });
+
   describe('module with js', () => {
     it('should load module testServiceA.js and testServiceB.js', async () => {
       const container = new Container('testDefault');
@@ -24,13 +27,14 @@ describe('test/loader.test.ts', () => {
       const manifest = require('./fixtures/module_with_js/src/index');
       await loaderFactory.loadManifest(manifest);
       const appProxy = new Proxy({}, {
-        get (_target, properName: string) {
+        get(_target, properName: string) {
           return container.get(properName);
         }
       });
       assert((container.get('testServiceA') as any).testMethod(appProxy) === 'Hello Artus');
     });
   });
+
   describe('module with custom loader', () => {
     it('should load module test.ts with custom loader', async () => {
       // SEEME: the import&register code need be replaced by scanner at production.
@@ -55,4 +59,11 @@ describe('test/loader.test.ts', () => {
       expect(console.log).toBeCalledWith('TestCustomLoader.state loaderState');
     });
   });
+
+  describe('custom instance', () => {
+    it('should not overide custom instance', async () => {
+      const app = await createApp();
+      expect(app.getContainer().get(Custom).getName()).toBe('foo');
+    })
+  });
 });