@celljs/core 4.5.1 → 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -49
- package/esm/browser/index.d.ts +0 -1
- package/esm/browser/index.js +0 -1
- package/esm/browser/index.js.map +1 -1
- package/esm/browser/shell/application-shell.d.ts +23 -3
- package/esm/browser/shell/application-shell.js +60 -4
- package/esm/browser/shell/application-shell.js.map +1 -1
- package/esm/browser/shell/index.d.ts +0 -1
- package/esm/browser/shell/index.js +0 -1
- package/esm/browser/shell/index.js.map +1 -1
- package/esm/common/annotation/autowired.d.ts +8 -2
- package/esm/common/annotation/autowired.js +7 -8
- package/esm/common/annotation/autowired.js.map +1 -1
- package/esm/common/annotation/detached/autowired-provider.js +1 -2
- package/esm/common/annotation/detached/autowired-provider.js.map +1 -1
- package/esm/common/annotation/value.d.ts +1 -0
- package/esm/common/annotation/value.js +3 -4
- package/esm/common/annotation/value.js.map +1 -1
- package/esm/common/aop/aop-proxy-factory.d.ts +2 -1
- package/esm/common/aop/aop-proxy-factory.js +10 -3
- package/esm/common/aop/aop-proxy-factory.js.map +1 -1
- package/esm/common/aop/class-filter.d.ts +2 -0
- package/esm/common/aop/class-filter.js +10 -3
- package/esm/common/aop/class-filter.js.map +1 -1
- package/esm/common/application/application-entry.js +0 -9
- package/esm/common/application/application-entry.js.map +1 -1
- package/esm/common/application/application-factory.d.ts +9 -3
- package/esm/common/application/application-factory.js +13 -6
- package/esm/common/application/application-factory.js.map +1 -1
- package/esm/common/application/application-protocol.d.ts +12 -0
- package/esm/common/application/application-protocol.js +1 -0
- package/esm/common/application/application-protocol.js.map +1 -1
- package/esm/common/application/application-state.d.ts +1 -1
- package/esm/common/application/application-state.js +4 -4
- package/esm/common/application/application-state.js.map +1 -1
- package/esm/common/application/application.d.ts +4 -2
- package/esm/common/application/application.js +19 -7
- package/esm/common/application/application.js.map +1 -1
- package/esm/common/application/index.d.ts +2 -0
- package/esm/common/application/index.js +2 -0
- package/esm/common/application/index.js.map +1 -1
- package/esm/common/config/config-util.d.ts +0 -1
- package/esm/common/config/config-util.js +0 -6
- package/esm/common/config/config-util.js.map +1 -1
- package/esm/common/container/container-error.d.ts +1 -1
- package/esm/common/container/container-error.js.map +1 -1
- package/esm/common/container/container.d.ts +1 -0
- package/esm/common/container/container.js +6 -1
- package/esm/common/container/container.js.map +1 -1
- package/esm/common/container/index.d.ts +0 -2
- package/esm/common/container/index.js +0 -2
- package/esm/common/container/index.js.map +1 -1
- package/esm/common/container/injection.d.ts +15 -1
- package/esm/common/container/injection.js +37 -1
- package/esm/common/container/injection.js.map +1 -1
- package/esm/common/el/core-context-initializer.d.ts +2 -1
- package/esm/common/el/core-context-initializer.js +5 -3
- package/esm/common/el/core-context-initializer.js.map +1 -1
- package/esm/common/provider/index.d.ts +0 -1
- package/esm/common/provider/index.js +0 -1
- package/esm/common/provider/index.js.map +1 -1
- package/esm/common/test/assembly-test-util.d.ts +16 -0
- package/esm/common/test/assembly-test-util.js +34 -0
- package/esm/common/test/assembly-test-util.js.map +1 -0
- package/esm/node/application/exit-signal-lifecycle.d.ts +5 -0
- package/esm/node/application/exit-signal-lifecycle.js +30 -0
- package/esm/node/application/exit-signal-lifecycle.js.map +1 -0
- package/esm/node/application/index.d.ts +1 -2
- package/esm/node/application/index.js +1 -2
- package/esm/node/application/index.js.map +1 -1
- package/lib/browser/index.d.ts +0 -1
- package/lib/browser/index.d.ts.map +1 -1
- package/lib/browser/index.js +0 -1
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/shell/application-shell.d.ts +23 -3
- package/lib/browser/shell/application-shell.d.ts.map +1 -1
- package/lib/browser/shell/application-shell.js +62 -5
- package/lib/browser/shell/application-shell.js.map +1 -1
- package/lib/browser/shell/index.d.ts +0 -1
- package/lib/browser/shell/index.d.ts.map +1 -1
- package/lib/browser/shell/index.js +0 -1
- package/lib/browser/shell/index.js.map +1 -1
- package/lib/common/annotation/autowired.d.ts +8 -2
- package/lib/common/annotation/autowired.d.ts.map +1 -1
- package/lib/common/annotation/autowired.js +7 -8
- package/lib/common/annotation/autowired.js.map +1 -1
- package/lib/common/annotation/detached/autowired-provider.d.ts.map +1 -1
- package/lib/common/annotation/detached/autowired-provider.js +1 -2
- package/lib/common/annotation/detached/autowired-provider.js.map +1 -1
- package/lib/common/annotation/value.d.ts +1 -0
- package/lib/common/annotation/value.d.ts.map +1 -1
- package/lib/common/annotation/value.js +3 -4
- package/lib/common/annotation/value.js.map +1 -1
- package/lib/common/aop/aop-proxy-factory.d.ts +2 -1
- package/lib/common/aop/aop-proxy-factory.d.ts.map +1 -1
- package/lib/common/aop/aop-proxy-factory.js +9 -2
- package/lib/common/aop/aop-proxy-factory.js.map +1 -1
- package/lib/common/aop/aop.spec.js +24 -18
- package/lib/common/aop/aop.spec.js.map +1 -1
- package/lib/common/aop/class-filter.d.ts +2 -0
- package/lib/common/aop/class-filter.d.ts.map +1 -1
- package/lib/common/aop/class-filter.js +9 -2
- package/lib/common/aop/class-filter.js.map +1 -1
- package/lib/common/application/application-entry.d.ts.map +1 -1
- package/lib/common/application/application-entry.js +0 -9
- package/lib/common/application/application-entry.js.map +1 -1
- package/lib/common/application/application-factory.d.ts +9 -3
- package/lib/common/application/application-factory.d.ts.map +1 -1
- package/lib/common/application/application-factory.js +13 -6
- package/lib/common/application/application-factory.js.map +1 -1
- package/lib/common/application/application-protocol.d.ts +12 -0
- package/lib/common/application/application-protocol.d.ts.map +1 -1
- package/lib/common/application/application-protocol.js +2 -1
- package/lib/common/application/application-protocol.js.map +1 -1
- package/lib/common/application/application-state.d.ts +1 -1
- package/lib/common/application/application-state.d.ts.map +1 -1
- package/lib/common/application/application-state.js +5 -5
- package/lib/common/application/application-state.js.map +1 -1
- package/lib/common/application/application.d.ts +4 -2
- package/lib/common/application/application.d.ts.map +1 -1
- package/lib/common/application/application.js +18 -6
- package/lib/common/application/application.js.map +1 -1
- package/lib/common/application/index.d.ts +2 -0
- package/lib/common/application/index.d.ts.map +1 -1
- package/lib/common/application/index.js +2 -0
- package/lib/common/application/index.js.map +1 -1
- package/lib/common/application/lifecycle.spec.js +18 -13
- package/lib/common/application/lifecycle.spec.js.map +1 -1
- package/lib/common/config/config-util.d.ts +0 -1
- package/lib/common/config/config-util.d.ts.map +1 -1
- package/lib/common/config/config-util.js +0 -6
- package/lib/common/config/config-util.js.map +1 -1
- package/lib/common/container/assembly.spec.js +115 -43
- package/lib/common/container/assembly.spec.js.map +1 -1
- package/lib/common/container/container-error.d.ts +1 -1
- package/lib/common/container/container-error.d.ts.map +1 -1
- package/lib/common/container/container-error.js.map +1 -1
- package/lib/common/container/container.d.ts +1 -0
- package/lib/common/container/container.d.ts.map +1 -1
- package/lib/common/container/container.js +6 -1
- package/lib/common/container/container.js.map +1 -1
- package/lib/common/container/container.spec.js +6 -1
- package/lib/common/container/container.spec.js.map +1 -1
- package/lib/common/container/index.d.ts +0 -2
- package/lib/common/container/index.d.ts.map +1 -1
- package/lib/common/container/index.js +0 -2
- package/lib/common/container/index.js.map +1 -1
- package/lib/common/container/injection.d.ts +15 -1
- package/lib/common/container/injection.d.ts.map +1 -1
- package/lib/common/container/injection.js +37 -1
- package/lib/common/container/injection.js.map +1 -1
- package/lib/common/el/core-context-initializer.d.ts +2 -1
- package/lib/common/el/core-context-initializer.d.ts.map +1 -1
- package/lib/common/el/core-context-initializer.js +5 -3
- package/lib/common/el/core-context-initializer.js.map +1 -1
- package/lib/common/el/value-and-el.spec.js +35 -26
- package/lib/common/el/value-and-el.spec.js.map +1 -1
- package/lib/common/provider/index.d.ts +0 -1
- package/lib/common/provider/index.d.ts.map +1 -1
- package/lib/common/provider/index.js +0 -1
- package/lib/common/provider/index.js.map +1 -1
- package/lib/common/test/assembly-test-util.d.ts +17 -0
- package/lib/common/test/assembly-test-util.d.ts.map +1 -0
- package/lib/common/test/assembly-test-util.js +37 -0
- package/lib/common/test/assembly-test-util.js.map +1 -0
- package/lib/node/application/exit-signal-lifecycle.d.ts +6 -0
- package/lib/node/application/exit-signal-lifecycle.d.ts.map +1 -0
- package/lib/node/application/exit-signal-lifecycle.js +33 -0
- package/lib/node/application/exit-signal-lifecycle.js.map +1 -0
- package/lib/node/application/index.d.ts +1 -2
- package/lib/node/application/index.d.ts.map +1 -1
- package/lib/node/application/index.js +1 -2
- package/lib/node/application/index.js.map +1 -1
- package/lib/package.spec.js +17 -2
- package/lib/package.spec.js.map +1 -1
- package/package.json +1 -5
- package/src/browser/index.ts +0 -1
- package/src/browser/shell/application-shell.ts +64 -5
- package/src/browser/shell/index.ts +0 -1
- package/src/common/annotation/autowired.ts +14 -11
- package/src/common/annotation/detached/autowired-provider.ts +1 -2
- package/src/common/annotation/value.ts +4 -4
- package/src/common/aop/aop-proxy-factory.ts +6 -4
- package/src/common/aop/aop.spec.ts +26 -18
- package/src/common/aop/class-filter.ts +6 -3
- package/src/common/application/application-entry.ts +1 -9
- package/src/common/application/application-factory.ts +15 -6
- package/src/common/application/application-protocol.ts +14 -1
- package/src/common/application/application-state.ts +1 -1
- package/src/common/application/application.ts +13 -3
- package/src/common/application/index.ts +2 -0
- package/src/common/application/lifecycle.spec.ts +20 -13
- package/src/common/config/config-util.ts +0 -6
- package/src/common/container/assembly.spec.ts +100 -44
- package/src/common/container/container-error.ts +3 -2
- package/src/common/container/container.spec.ts +8 -2
- package/src/common/container/container.ts +7 -2
- package/src/common/container/index.ts +0 -2
- package/src/common/container/injection.ts +40 -3
- package/src/common/el/core-context-initializer.ts +6 -4
- package/src/common/el/value-and-el.spec.ts +34 -25
- package/src/common/provider/index.ts +0 -1
- package/src/common/test/assembly-test-util.ts +41 -0
- package/src/node/application/exit-signal-lifecycle.ts +23 -0
- package/src/node/application/index.ts +1 -2
- package/src/package.spec.ts +16 -3
- package/esm/browser/application/frontend-application-state.d.ts +0 -4
- package/esm/browser/application/frontend-application-state.js +0 -14
- package/esm/browser/application/frontend-application-state.js.map +0 -1
- package/esm/browser/application/frontend-application.d.ts +0 -30
- package/esm/browser/application/frontend-application.js +0 -123
- package/esm/browser/application/frontend-application.js.map +0 -1
- package/esm/browser/application/index.d.ts +0 -2
- package/esm/browser/application/index.js +0 -3
- package/esm/browser/application/index.js.map +0 -1
- package/esm/browser/shell/shell-protocol.d.ts +0 -4
- package/esm/browser/shell/shell-protocol.js +0 -2
- package/esm/browser/shell/shell-protocol.js.map +0 -1
- package/esm/common/container/container-provider.d.ts +0 -7
- package/esm/common/container/container-provider.js +0 -24
- package/esm/common/container/container-provider.js.map +0 -1
- package/esm/common/container/container-util.d.ts +0 -7
- package/esm/common/container/container-util.js +0 -18
- package/esm/common/container/container-util.js.map +0 -1
- package/esm/common/provider/provider-util.d.ts +0 -5
- package/esm/common/provider/provider-util.js +0 -10
- package/esm/common/provider/provider-util.js.map +0 -1
- package/esm/node/application/backend-application-state.d.ts +0 -4
- package/esm/node/application/backend-application-state.js +0 -14
- package/esm/node/application/backend-application-state.js.map +0 -1
- package/esm/node/application/backend-application.d.ts +0 -10
- package/esm/node/application/backend-application.js +0 -52
- package/esm/node/application/backend-application.js.map +0 -1
- package/lib/browser/application/frontend-application-state.d.ts +0 -5
- package/lib/browser/application/frontend-application-state.d.ts.map +0 -1
- package/lib/browser/application/frontend-application-state.js +0 -17
- package/lib/browser/application/frontend-application-state.js.map +0 -1
- package/lib/browser/application/frontend-application.d.ts +0 -31
- package/lib/browser/application/frontend-application.d.ts.map +0 -1
- package/lib/browser/application/frontend-application.js +0 -126
- package/lib/browser/application/frontend-application.js.map +0 -1
- package/lib/browser/application/index.d.ts +0 -3
- package/lib/browser/application/index.d.ts.map +0 -1
- package/lib/browser/application/index.js +0 -19
- package/lib/browser/application/index.js.map +0 -1
- package/lib/browser/shell/shell-protocol.d.ts +0 -5
- package/lib/browser/shell/shell-protocol.d.ts.map +0 -1
- package/lib/browser/shell/shell-protocol.js +0 -5
- package/lib/browser/shell/shell-protocol.js.map +0 -1
- package/lib/common/container/container-provider.d.ts +0 -8
- package/lib/common/container/container-provider.d.ts.map +0 -1
- package/lib/common/container/container-provider.js +0 -27
- package/lib/common/container/container-provider.js.map +0 -1
- package/lib/common/container/container-util.d.ts +0 -8
- package/lib/common/container/container-util.d.ts.map +0 -1
- package/lib/common/container/container-util.js +0 -21
- package/lib/common/container/container-util.js.map +0 -1
- package/lib/common/provider/provider-util.d.ts +0 -6
- package/lib/common/provider/provider-util.d.ts.map +0 -1
- package/lib/common/provider/provider-util.js +0 -13
- package/lib/common/provider/provider-util.js.map +0 -1
- package/lib/node/application/backend-application-state.d.ts +0 -5
- package/lib/node/application/backend-application-state.d.ts.map +0 -1
- package/lib/node/application/backend-application-state.js +0 -17
- package/lib/node/application/backend-application-state.js.map +0 -1
- package/lib/node/application/backend-application.d.ts +0 -11
- package/lib/node/application/backend-application.d.ts.map +0 -1
- package/lib/node/application/backend-application.js +0 -55
- package/lib/node/application/backend-application.js.map +0 -1
- package/src/browser/application/frontend-application-state.ts +0 -11
- package/src/browser/application/frontend-application.ts +0 -121
- package/src/browser/application/index.ts +0 -2
- package/src/browser/shell/shell-protocol.ts +0 -5
- package/src/common/container/container-provider.ts +0 -25
- package/src/common/container/container-util.ts +0 -17
- package/src/common/provider/provider-util.ts +0 -10
- package/src/node/application/backend-application-state.ts +0 -9
- package/src/node/application/backend-application.ts +0 -43
|
@@ -1,19 +1,29 @@
|
|
|
1
|
-
import { Component, Autowired } from '../annotation';
|
|
2
|
-
import { AbstractApplication, Application, ApplicationState, ApplicationStateService } from './application-protocol';
|
|
1
|
+
import { Component, Autowired, Optional } from '../annotation';
|
|
2
|
+
import { AbstractApplication, Application, ApplicationShell, ApplicationState, ApplicationStateService } from './application-protocol';
|
|
3
3
|
|
|
4
|
+
/** The application of every target: the lifecycles, then the shell if there is one. */
|
|
4
5
|
@Component(Application)
|
|
5
|
-
export class
|
|
6
|
+
export class ApplicationImpl extends AbstractApplication {
|
|
6
7
|
|
|
7
8
|
@Autowired(ApplicationStateService)
|
|
8
9
|
protected readonly stateService: ApplicationStateService<ApplicationState>;
|
|
9
10
|
|
|
11
|
+
@Autowired(ApplicationShell) @Optional()
|
|
12
|
+
protected readonly shell?: ApplicationShell;
|
|
13
|
+
|
|
10
14
|
async start(): Promise<void> {
|
|
11
15
|
await this.doStart();
|
|
12
16
|
this.stateService.state = 'started';
|
|
17
|
+
if (this.shell) {
|
|
18
|
+
await this.shell.attach(this);
|
|
19
|
+
}
|
|
13
20
|
this.stateService.state = 'ready';
|
|
14
21
|
}
|
|
15
22
|
|
|
16
23
|
async stop(): Promise<void> {
|
|
24
|
+
if (this.stateService.state === 'stopped') {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
17
27
|
this.doStop();
|
|
18
28
|
this.stateService.state = 'stopped';
|
|
19
29
|
}
|
|
@@ -3,12 +3,19 @@ import { expect } from 'chai';
|
|
|
3
3
|
import { ApplicationFactory } from './application-factory';
|
|
4
4
|
import { ApplicationLifecycle, ApplicationStateService, ApplicationState, Application } from './application-protocol';
|
|
5
5
|
import { Component } from '../annotation';
|
|
6
|
-
import { ContainerUtil } from '../container/container-util';
|
|
7
6
|
import { Scope } from '../container/scope';
|
|
7
|
+
import { Container, ContainerModule } from '../container/container';
|
|
8
8
|
import { Logger } from '../logger/logger-protocol';
|
|
9
9
|
import { MockLogger } from '../test/mock-logger';
|
|
10
10
|
import { SecurityCheck, SecurityCheckError, SecurityWarning } from '../security-check/security-check-protocol';
|
|
11
11
|
|
|
12
|
+
let container: Container;
|
|
13
|
+
/** A new container for each test; the components registered since the previous one go into it. */
|
|
14
|
+
async function create(props: Record<string, any>, ...modules: ContainerModule[]): Promise<Application> {
|
|
15
|
+
container = ApplicationFactory.createContainer(props, ...modules);
|
|
16
|
+
return container.get<Application>(Application);
|
|
17
|
+
}
|
|
18
|
+
|
|
12
19
|
/**
|
|
13
20
|
* 应用生命周期的约定(阶段 1.E 行为测试):ApplicationLifecycle 的调用顺序、出错时的行为、状态变化。
|
|
14
21
|
*/
|
|
@@ -44,7 +51,7 @@ describe('core 应用生命周期', () => {
|
|
|
44
51
|
onStart() { rec.events.push('start:second'); }
|
|
45
52
|
onStop() { rec.events.push('stop:second'); }
|
|
46
53
|
}
|
|
47
|
-
const app = await
|
|
54
|
+
const app = await create({});
|
|
48
55
|
expect(rec.events).to.deep.equal([]);
|
|
49
56
|
await app.start();
|
|
50
57
|
const inits = rec.events.slice(0, 2);
|
|
@@ -73,7 +80,7 @@ describe('core 应用生命周期', () => {
|
|
|
73
80
|
}
|
|
74
81
|
void L;
|
|
75
82
|
}
|
|
76
|
-
const app = await
|
|
83
|
+
const app = await create({});
|
|
77
84
|
await app.start();
|
|
78
85
|
expect(order).to.have.members(['a', 'b', 'c']);
|
|
79
86
|
expect(maxRunning).to.equal(1);
|
|
@@ -94,7 +101,7 @@ describe('core 应用生命周期', () => {
|
|
|
94
101
|
onStart() { rec.events.push('start'); }
|
|
95
102
|
onStop() { rec.events.push('stop'); }
|
|
96
103
|
}
|
|
97
|
-
const app = await
|
|
104
|
+
const app = await create({});
|
|
98
105
|
await app.start();
|
|
99
106
|
await app.stop();
|
|
100
107
|
expect(rec.events).to.deep.equal(['init', 'start', 'stop']);
|
|
@@ -108,14 +115,14 @@ describe('core 应用生命周期', () => {
|
|
|
108
115
|
});
|
|
109
116
|
|
|
110
117
|
it('没有任何 ApplicationLifecycle 时 start / stop 正常', async () => {
|
|
111
|
-
const app = await
|
|
118
|
+
const app = await create({});
|
|
112
119
|
await app.start();
|
|
113
120
|
await app.stop();
|
|
114
121
|
});
|
|
115
122
|
|
|
116
123
|
it('状态:init → started → ready,stop 之后是 stopped;reachedState 与 onStateChanged', async () => {
|
|
117
|
-
const app = await
|
|
118
|
-
const states =
|
|
124
|
+
const app = await create({});
|
|
125
|
+
const states = container.get<ApplicationStateService<ApplicationState>>(ApplicationStateService);
|
|
119
126
|
expect(states.state).to.equal('init');
|
|
120
127
|
const seen: string[] = [];
|
|
121
128
|
states.onStateChanged(s => seen.push(s));
|
|
@@ -133,8 +140,8 @@ describe('core 应用生命周期', () => {
|
|
|
133
140
|
});
|
|
134
141
|
|
|
135
142
|
it('离开一个状态之后,再 reachedState(那个状态) 立即 resolve(F7)', async () => {
|
|
136
|
-
const app = await
|
|
137
|
-
const states =
|
|
143
|
+
const app = await create({});
|
|
144
|
+
const states = container.get<ApplicationStateService<ApplicationState>>(ApplicationStateService);
|
|
138
145
|
await app.start();
|
|
139
146
|
const race = (p: Promise<void>) => Promise.race([p.then(() => 'resolved'), new Promise(r => setTimeout(() => r('pending'), 20))]);
|
|
140
147
|
expect(await race(states.reachedState('ready'))).to.equal('resolved');
|
|
@@ -143,8 +150,8 @@ describe('core 应用生命周期', () => {
|
|
|
143
150
|
});
|
|
144
151
|
|
|
145
152
|
it('同一个状态重复设置不会重复通知', async () => {
|
|
146
|
-
await
|
|
147
|
-
const states =
|
|
153
|
+
await create({});
|
|
154
|
+
const states = container.get<ApplicationStateService<ApplicationState>>(ApplicationStateService);
|
|
148
155
|
const seen: string[] = [];
|
|
149
156
|
states.onStateChanged(s => seen.push(s));
|
|
150
157
|
states.state = 'started';
|
|
@@ -175,13 +182,13 @@ describe('启动时的安全检查', () => {
|
|
|
175
182
|
process.env.NODE_ENV = 'production';
|
|
176
183
|
try {
|
|
177
184
|
components(rec);
|
|
178
|
-
const failing = await
|
|
185
|
+
const failing = await create({ securityChecks: 'fail', logger: { level: 'error' } });
|
|
179
186
|
const error = await failing.start().then(() => undefined, e => e);
|
|
180
187
|
expect(error).to.be.instanceOf(SecurityCheckError);
|
|
181
188
|
expect(error.code).to.equal('CELL_SECURITY_CHECK_FAILED');
|
|
182
189
|
expect(rec.events).to.deep.equal([]);
|
|
183
190
|
components(rec);
|
|
184
|
-
const warning = await
|
|
191
|
+
const warning = await create({ securityChecks: 'warn', logger: { level: 'error' } });
|
|
185
192
|
await warning.start();
|
|
186
193
|
expect(rec.events).to.deep.equal(['start']);
|
|
187
194
|
await warning.stop();
|
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import { ConfigProvider } from './config-protocol';
|
|
2
|
-
import { ContainerUtil } from '../container';
|
|
3
1
|
import { config, types } from './dynamic-config';
|
|
4
2
|
import type { ConfigTypes } from './env-config-util';
|
|
5
3
|
import { currentThis } from '../utils';
|
|
6
4
|
|
|
7
5
|
export namespace ConfigUtil {
|
|
8
|
-
export function get<T = any>(key: string, defaultValue?: T): T {
|
|
9
|
-
return ContainerUtil.get<ConfigProvider>(ConfigProvider).get(key, defaultValue);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
6
|
export function getRaw() {
|
|
13
7
|
return currentThis.cellProps || config;
|
|
14
8
|
}
|
|
@@ -4,22 +4,30 @@ import { ApplicationFactory } from '../application/application-factory';
|
|
|
4
4
|
import {
|
|
5
5
|
Component, Autowired, Optional, Constant, AutowiredProvider, PostConstruct, Named, Tagged
|
|
6
6
|
} from '../annotation';
|
|
7
|
-
import { ContainerUtil } from './container-util';
|
|
8
|
-
import { ContainerProvider } from './container-provider';
|
|
9
7
|
import { Scope } from './scope';
|
|
10
8
|
import { Provider } from '../provider';
|
|
11
9
|
import { ConfigProvider } from '../config/config-protocol';
|
|
12
10
|
import { autoBind } from './auto-bind';
|
|
11
|
+
import { Container, ContainerModule } from './container';
|
|
12
|
+
import { ContainerError } from './container-error';
|
|
13
|
+
import { Application } from '../application/application-protocol';
|
|
14
|
+
|
|
15
|
+
let container: Container;
|
|
16
|
+
/** A new container for each test; the components registered since the previous one go into it. */
|
|
17
|
+
async function create(props: Record<string, any>, ...modules: ContainerModule[]): Promise<Application> {
|
|
18
|
+
container = ApplicationFactory.createContainer(props, ...modules);
|
|
19
|
+
return container.get<Application>(Application);
|
|
20
|
+
}
|
|
13
21
|
|
|
14
22
|
/**
|
|
15
|
-
* 组件装配的约定(阶段 1.E 行为测试):只用公开的注解、ApplicationFactory
|
|
23
|
+
* 组件装配的约定(阶段 1.E 行为测试):只用公开的注解、ApplicationFactory 和容器断言,
|
|
16
24
|
* 不碰 inversify 的 API,这样阶段 2 换容器时这些用例可以原样复用。
|
|
17
25
|
*
|
|
18
|
-
* 注意:组件元数据是全局登记的,
|
|
26
|
+
* 注意:组件元数据是全局登记的,create() 里的 autoBind() 会把登记过的全部取走。
|
|
19
27
|
* 所以每个用例都在自己的函数里定义类,然后马上 create,用例之间互不影响。
|
|
20
28
|
*/
|
|
21
29
|
async function boot(props: Record<string, any> = {}) {
|
|
22
|
-
return
|
|
30
|
+
return create(props);
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
describe('core 组件装配', () => {
|
|
@@ -28,9 +36,9 @@ describe('core 组件装配', () => {
|
|
|
28
36
|
@Component()
|
|
29
37
|
class Plain { }
|
|
30
38
|
await boot();
|
|
31
|
-
const a =
|
|
39
|
+
const a = container.get(Plain);
|
|
32
40
|
expect(a).to.be.instanceOf(Plain);
|
|
33
|
-
expect(
|
|
41
|
+
expect(container.get(Plain)).to.equal(a);
|
|
34
42
|
});
|
|
35
43
|
|
|
36
44
|
it('@Component(id) 同时可以按类和按 id 取到,且是同一个实例', async () => {
|
|
@@ -38,9 +46,9 @@ describe('core 组件装配', () => {
|
|
|
38
46
|
@Component(Greeter)
|
|
39
47
|
class GreeterImpl { hello() { return 'hi'; } }
|
|
40
48
|
await boot();
|
|
41
|
-
const byId =
|
|
49
|
+
const byId = container.get<GreeterImpl>(Greeter);
|
|
42
50
|
expect(byId.hello()).to.equal('hi');
|
|
43
|
-
expect(
|
|
51
|
+
expect(container.get(GreeterImpl)).to.equal(byId);
|
|
44
52
|
});
|
|
45
53
|
|
|
46
54
|
it('一个组件可以登记多个 id,都指向同一个实例', async () => {
|
|
@@ -49,16 +57,16 @@ describe('core 组件装配', () => {
|
|
|
49
57
|
@Component({ id: [A, B] })
|
|
50
58
|
class Both { }
|
|
51
59
|
await boot();
|
|
52
|
-
expect(
|
|
53
|
-
expect(
|
|
54
|
-
expect(
|
|
60
|
+
expect(container.get(A)).to.be.instanceOf(Both);
|
|
61
|
+
expect(container.get(A)).to.equal(container.get(B));
|
|
62
|
+
expect(container.get(Both)).to.equal(container.get(A));
|
|
55
63
|
});
|
|
56
64
|
|
|
57
65
|
it('Scope.Transient 每次取都是新实例', async () => {
|
|
58
66
|
@Component({ scope: Scope.Transient })
|
|
59
67
|
class Fresh { }
|
|
60
68
|
await boot();
|
|
61
|
-
expect(
|
|
69
|
+
expect(container.get(Fresh)).not.to.equal(container.get(Fresh));
|
|
62
70
|
});
|
|
63
71
|
|
|
64
72
|
it('@Autowired 属性注入:按声明的类型,也可以显式给 id', async () => {
|
|
@@ -75,9 +83,9 @@ describe('core 组件装配', () => {
|
|
|
75
83
|
readonly dep: Dep;
|
|
76
84
|
}
|
|
77
85
|
await boot();
|
|
78
|
-
const s =
|
|
86
|
+
const s = container.get(Service);
|
|
79
87
|
expect(s.repo.find()).to.equal('row');
|
|
80
|
-
expect(s.dep).to.equal(
|
|
88
|
+
expect(s.dep).to.equal(container.get(Dep));
|
|
81
89
|
});
|
|
82
90
|
|
|
83
91
|
it('@Autowired 构造器参数注入', async () => {
|
|
@@ -89,7 +97,7 @@ describe('core 组件装配', () => {
|
|
|
89
97
|
constructor(@Autowired(Clock) readonly clock: FixedClock) { }
|
|
90
98
|
}
|
|
91
99
|
await boot();
|
|
92
|
-
expect(
|
|
100
|
+
expect(container.get(UsesClock).clock.now()).to.equal(42);
|
|
93
101
|
});
|
|
94
102
|
|
|
95
103
|
it('同一 id 有多个实现时,数组类型的 @Autowired 拿到全部', async () => {
|
|
@@ -104,8 +112,8 @@ describe('core 组件装配', () => {
|
|
|
104
112
|
readonly plugins: { name: string }[];
|
|
105
113
|
}
|
|
106
114
|
await boot();
|
|
107
|
-
expect(
|
|
108
|
-
expect(
|
|
115
|
+
expect(container.get(Host).plugins.map(p => p.name).sort()).to.deep.equal(['p1', 'p2']);
|
|
116
|
+
expect(container.getAll(Plugin)).to.have.length(2);
|
|
109
117
|
expect([P1, P2]).to.have.length(2);
|
|
110
118
|
});
|
|
111
119
|
|
|
@@ -120,7 +128,7 @@ describe('core 组件装配', () => {
|
|
|
120
128
|
readonly many: unknown[];
|
|
121
129
|
}
|
|
122
130
|
await boot();
|
|
123
|
-
const t =
|
|
131
|
+
const t = container.get(Tolerant);
|
|
124
132
|
expect(t.one).to.equal(undefined);
|
|
125
133
|
expect(t.many).to.deep.equal([]);
|
|
126
134
|
});
|
|
@@ -133,7 +141,7 @@ describe('core 组件装配', () => {
|
|
|
133
141
|
readonly absent: unknown;
|
|
134
142
|
}
|
|
135
143
|
await boot();
|
|
136
|
-
expect(() =>
|
|
144
|
+
expect(() => container.get(Needy)).to.throw(/AbsentService/);
|
|
137
145
|
});
|
|
138
146
|
|
|
139
147
|
it('@Component({ id, name / tag / default }) 按名字、标签或默认选实现,类本身仍可直接取(F1)', async () => {
|
|
@@ -154,11 +162,11 @@ describe('core 组件装配', () => {
|
|
|
154
162
|
readonly fallback: { kind: string };
|
|
155
163
|
}
|
|
156
164
|
await boot();
|
|
157
|
-
const c =
|
|
165
|
+
const c = container.get(Consumer);
|
|
158
166
|
expect([c.redis.kind, c.eu.kind, c.fallback.kind]).to.deep.equal(['redis', 'eu', 'memory']);
|
|
159
|
-
expect(
|
|
160
|
-
expect(
|
|
161
|
-
expect(
|
|
167
|
+
expect(container.get<{ kind: string }>(Store, { name: 'redis' })).to.equal(container.get(RedisStore));
|
|
168
|
+
expect(container.get<{ kind: string }>(Store, { tags: { region: 'eu' } })).to.equal(container.get(EuStore));
|
|
169
|
+
expect(container.get(Store)).to.equal(container.get(MemoryStore));
|
|
162
170
|
});
|
|
163
171
|
|
|
164
172
|
it('rebind: true 替换先加载的模块里的实现(应用覆盖框架或其他包的组件)', async () => {
|
|
@@ -169,8 +177,8 @@ describe('core 组件装配', () => {
|
|
|
169
177
|
@Component({ id: Mailer, rebind: true })
|
|
170
178
|
class CustomMailer { readonly who = 'custom'; }
|
|
171
179
|
const overrides = autoBind();
|
|
172
|
-
await
|
|
173
|
-
expect(
|
|
180
|
+
await create({}, base, overrides);
|
|
181
|
+
expect(container.getAll<{ who: string }>(Mailer).map(m => m.who)).to.deep.equal(['custom']);
|
|
174
182
|
expect([DefaultMailer, CustomMailer]).to.have.length(2);
|
|
175
183
|
});
|
|
176
184
|
|
|
@@ -181,7 +189,7 @@ describe('core 组件装配', () => {
|
|
|
181
189
|
@Component({ id: Mailer, rebind: true })
|
|
182
190
|
class CustomMailer { readonly who = 'custom'; }
|
|
183
191
|
await boot();
|
|
184
|
-
expect(
|
|
192
|
+
expect(container.getAll<{ who: string }>(Mailer).map(m => m.who)).to.deep.equal(['custom']);
|
|
185
193
|
expect([DefaultMailer, CustomMailer]).to.have.length(2);
|
|
186
194
|
});
|
|
187
195
|
|
|
@@ -196,8 +204,8 @@ describe('core 组件装配', () => {
|
|
|
196
204
|
readonly config: FixedConfig;
|
|
197
205
|
}
|
|
198
206
|
await boot();
|
|
199
|
-
expect(
|
|
200
|
-
expect(
|
|
207
|
+
expect(container.get(ReadsConfig).config).to.be.instanceOf(FixedConfig);
|
|
208
|
+
expect(container.get(ReadsConfig).config.get('answer')).to.equal(42);
|
|
201
209
|
});
|
|
202
210
|
|
|
203
211
|
it('@Constant 登记常量,多个 id 都能取到', async () => {
|
|
@@ -206,8 +214,8 @@ describe('core 组件装配', () => {
|
|
|
206
214
|
@Constant([Token, Alias], { secret: 'abc' })
|
|
207
215
|
class Holder { }
|
|
208
216
|
await boot();
|
|
209
|
-
expect(
|
|
210
|
-
expect(
|
|
217
|
+
expect(container.get(Token)).to.deep.equal({ secret: 'abc' });
|
|
218
|
+
expect(container.get(Alias)).to.equal(container.get(Token));
|
|
211
219
|
expect(Holder).to.be.a('function');
|
|
212
220
|
});
|
|
213
221
|
|
|
@@ -225,7 +233,7 @@ describe('core 组件装配', () => {
|
|
|
225
233
|
readonly steps: Provider<{ name: string, priority: number }>;
|
|
226
234
|
}
|
|
227
235
|
await boot();
|
|
228
|
-
const names =
|
|
236
|
+
const names = container.get(Pipeline).steps.sortSync().map(s => s.name);
|
|
229
237
|
expect(names).to.have.members(['early', 'middle', 'late']);
|
|
230
238
|
// Prioritizeable:priority 大的排前面
|
|
231
239
|
expect(names).to.deep.equal(['late', 'middle', 'early']);
|
|
@@ -248,12 +256,44 @@ describe('core 组件装配', () => {
|
|
|
248
256
|
}
|
|
249
257
|
}
|
|
250
258
|
await boot();
|
|
251
|
-
expect(
|
|
252
|
-
|
|
259
|
+
expect(container.get(Init).seenDep).to.equal(true);
|
|
260
|
+
container.get(Init);
|
|
253
261
|
expect(calls).to.equal(1);
|
|
254
262
|
});
|
|
255
263
|
|
|
256
|
-
it('detached 的 @Autowired
|
|
264
|
+
it('detached 的 @Autowired:第一次读属性时才从创建实例的容器取值,能打破循环依赖', async () => {
|
|
265
|
+
@Component()
|
|
266
|
+
class Parent {
|
|
267
|
+
@Autowired({ id: Symbol.for('test/Child'), detached: true })
|
|
268
|
+
readonly child: { parent: Parent };
|
|
269
|
+
}
|
|
270
|
+
@Component(Symbol.for('test/Child'))
|
|
271
|
+
class Child {
|
|
272
|
+
constructor(@Autowired(Parent) readonly parent: Parent) { }
|
|
273
|
+
}
|
|
274
|
+
await boot();
|
|
275
|
+
const parent = container.get(Parent);
|
|
276
|
+
expect(parent.child).to.be.instanceOf(Child);
|
|
277
|
+
expect(parent.child.parent).to.equal(parent);
|
|
278
|
+
// 取一次后留在实例上
|
|
279
|
+
expect(Object.getOwnPropertyDescriptor(parent, 'child')?.value).to.equal(parent.child);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('detached 属性从各自的容器取值:两个容器并存互不干扰', async () => {
|
|
283
|
+
const Clock = Symbol('DetachedClock');
|
|
284
|
+
@Component()
|
|
285
|
+
class UsesClock {
|
|
286
|
+
@Autowired({ id: Clock, detached: true })
|
|
287
|
+
readonly clock: { now(): number };
|
|
288
|
+
}
|
|
289
|
+
const module = autoBind();
|
|
290
|
+
const a = ApplicationFactory.createContainer({}, module, new ContainerModule(bind => { bind(Clock).toConstantValue({ now: () => 1 }); }));
|
|
291
|
+
const b = ApplicationFactory.createContainer({}, module, new ContainerModule(bind => { bind(Clock).toConstantValue({ now: () => 2 }); }));
|
|
292
|
+
expect(a.get(UsesClock).clock.now()).to.equal(1);
|
|
293
|
+
expect(b.get(UsesClock).clock.now()).to.equal(2);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it('不是容器创建的对象读 detached 属性:CELL_DI_NO_CONTAINER,说明怎么改', async () => {
|
|
257
297
|
const Clock = Symbol('DetachedClock');
|
|
258
298
|
@Component(Clock)
|
|
259
299
|
class Clk { now() { return 7; } }
|
|
@@ -262,7 +302,14 @@ describe('core 组件装配', () => {
|
|
|
262
302
|
readonly clock: Clk;
|
|
263
303
|
}
|
|
264
304
|
await boot();
|
|
265
|
-
|
|
305
|
+
let error: unknown;
|
|
306
|
+
try {
|
|
307
|
+
void new NotAComponent().clock;
|
|
308
|
+
} catch (e) {
|
|
309
|
+
error = e;
|
|
310
|
+
}
|
|
311
|
+
expect(ContainerError.is(error, 'CELL_DI_NO_CONTAINER')).to.equal(true);
|
|
312
|
+
expect((error as ContainerError).message).to.contain('NotAComponent.clock');
|
|
266
313
|
});
|
|
267
314
|
|
|
268
315
|
it('detached 不支持构造器参数,给出明确的错误', () => {
|
|
@@ -271,18 +318,27 @@ describe('core 组件装配', () => {
|
|
|
271
318
|
constructor(@Autowired({ id: Symbol('x'), detached: true }) readonly x: unknown) { }
|
|
272
319
|
}
|
|
273
320
|
return Bad;
|
|
274
|
-
}).to.throw(/
|
|
321
|
+
}).to.throw(/cannot be a constructor parameter/);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('容器绑定了自己:组件用 @Autowired(Container) 拿到创建它的容器', async () => {
|
|
325
|
+
@Component()
|
|
326
|
+
class NeedsContainer {
|
|
327
|
+
@Autowired(Container)
|
|
328
|
+
readonly container: Container;
|
|
329
|
+
}
|
|
330
|
+
await boot();
|
|
331
|
+
expect(container.get(NeedsContainer).container).to.equal(container);
|
|
275
332
|
});
|
|
276
333
|
|
|
277
|
-
it('每次
|
|
334
|
+
it('每次 create 都是新的容器:上一次登记的组件不会进入下一个容器', async () => {
|
|
278
335
|
@Component()
|
|
279
336
|
class Counter { value = 0; }
|
|
280
337
|
await boot();
|
|
281
|
-
const first =
|
|
282
|
-
|
|
338
|
+
const first = container;
|
|
339
|
+
container.get(Counter).value = 5;
|
|
283
340
|
await boot();
|
|
284
|
-
expect(
|
|
285
|
-
|
|
286
|
-
expect(() => ContainerUtil.get(Counter)).to.throw();
|
|
341
|
+
expect(container).not.to.equal(first);
|
|
342
|
+
expect(() => container.get(Counter)).to.throw();
|
|
287
343
|
});
|
|
288
|
-
});
|
|
344
|
+
});
|
|
@@ -23,9 +23,10 @@ export namespace ContainerError {
|
|
|
23
23
|
| 'CELL_DI_AMBIGUOUS'
|
|
24
24
|
| 'CELL_DI_CIRCULAR'
|
|
25
25
|
| 'CELL_DI_MISSING_INJECT'
|
|
26
|
-
| 'CELL_DI_INCOMPLETE_BINDING'
|
|
26
|
+
| 'CELL_DI_INCOMPLETE_BINDING'
|
|
27
|
+
| 'CELL_DI_NO_CONTAINER';
|
|
27
28
|
|
|
28
29
|
export function is(error: unknown, code?: Code): error is ContainerError {
|
|
29
30
|
return error instanceof ContainerError && (code === undefined || error.code === code);
|
|
30
31
|
}
|
|
31
|
-
}
|
|
32
|
+
}
|
|
@@ -176,7 +176,13 @@ describe('Container', () => {
|
|
|
176
176
|
c.bind(A).to(A).inSingletonScope();
|
|
177
177
|
c.bind(Named1).toService(A).whenTargetNamed('x');
|
|
178
178
|
const rows = [...c.bindings()].flatMap(([id, bindings]) => bindings.map(b => [Container.idName(id), b.type, b.scope, b.condition]));
|
|
179
|
-
expect(rows).to.deep.equal([['A', 'class', Scope.Singleton, undefined], ['Symbol(Named1)', 'service', Scope.Transient, 'named x']]);
|
|
179
|
+
expect(rows.slice(1)).to.deep.equal([['A', 'class', Scope.Singleton, undefined], ['Symbol(Named1)', 'service', Scope.Transient, 'named x']]);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('is bound to itself', () => {
|
|
183
|
+
const c = new Container();
|
|
184
|
+
expect(c.get(Container)).to.equal(c);
|
|
185
|
+
expect([...c.bindings().keys()][0]).to.equal(Container);
|
|
180
186
|
});
|
|
181
187
|
|
|
182
188
|
describe('errors', () => {
|
|
@@ -259,4 +265,4 @@ describe('Container', () => {
|
|
|
259
265
|
expect(errorOf(() => c.get('nothing')).code).to.equal('CELL_DI_INCOMPLETE_BINDING');
|
|
260
266
|
});
|
|
261
267
|
});
|
|
262
|
-
});
|
|
268
|
+
});
|
|
@@ -130,7 +130,9 @@ export class Container {
|
|
|
130
130
|
protected readonly resolving: Binding[] = [];
|
|
131
131
|
protected readonly requests: InjectionTarget[] = [];
|
|
132
132
|
|
|
133
|
+
/** The container is bound to itself: a component gets it with `@Autowired(Container)`. */
|
|
133
134
|
constructor(...modules: ContainerModule[]) {
|
|
135
|
+
this.bind(Container).toConstantValue(this);
|
|
134
136
|
this.load(...modules);
|
|
135
137
|
}
|
|
136
138
|
|
|
@@ -274,6 +276,9 @@ export class Container {
|
|
|
274
276
|
args[i] = this.resolve(parameters[i]);
|
|
275
277
|
}
|
|
276
278
|
const instance: any = new type(...args);
|
|
279
|
+
if (plan.keepsContainer) {
|
|
280
|
+
InjectionUtil.setContainer(instance, this);
|
|
281
|
+
}
|
|
277
282
|
for (const property of plan.properties) {
|
|
278
283
|
const value = this.resolve(property);
|
|
279
284
|
// Like a default parameter: an optional property that nothing is bound to keeps its initial value.
|
|
@@ -330,7 +335,7 @@ export class Container {
|
|
|
330
335
|
return new ContainerError({
|
|
331
336
|
code: 'CELL_DI_CIRCULAR',
|
|
332
337
|
message: `Circular dependency: ${path.join(' → ')}.`,
|
|
333
|
-
hint: 'Break the cycle: get one side lazily (
|
|
338
|
+
hint: 'Break the cycle: get one side lazily (@AutowiredProvider, or @Autowired({ detached: true }) on a property), or move the shared part into another component.',
|
|
334
339
|
path
|
|
335
340
|
});
|
|
336
341
|
}
|
|
@@ -360,4 +365,4 @@ export class Container {
|
|
|
360
365
|
}
|
|
361
366
|
return String(id);
|
|
362
367
|
}
|
|
363
|
-
}
|
|
368
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ComponentId } from './container';
|
|
1
|
+
import type { ComponentId, Container } from './container';
|
|
2
2
|
import { ContainerError } from './container-error';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -23,6 +23,8 @@ export interface InjectionPlan {
|
|
|
23
23
|
readonly parameters: readonly InjectionTarget[];
|
|
24
24
|
readonly properties: readonly InjectionTarget[];
|
|
25
25
|
readonly postConstruct?: string | symbol;
|
|
26
|
+
/** Whether instances remember the container that created them ({@link InjectionUtil.containerOf}). */
|
|
27
|
+
readonly keepsContainer?: boolean;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
type Point = { -readonly [K in keyof InjectionTarget]?: InjectionTarget[K] };
|
|
@@ -30,12 +32,14 @@ type Point = { -readonly [K in keyof InjectionTarget]?: InjectionTarget[K] };
|
|
|
30
32
|
const PARAMETERS = 'cell:di:parameters';
|
|
31
33
|
const PROPERTIES = 'cell:di:properties';
|
|
32
34
|
const POST_CONSTRUCT = 'cell:di:post-construct';
|
|
35
|
+
const KEEPS_CONTAINER = 'cell:di:keeps-container';
|
|
33
36
|
/** What TypeScript writes for parameters whose type is an interface, a union or a primitive. */
|
|
34
37
|
const NOT_AN_ID: unknown[] = [Object, Function, Array, String, Number, Boolean, Symbol, BigInt, Promise];
|
|
35
38
|
|
|
36
39
|
export namespace InjectionUtil {
|
|
37
40
|
|
|
38
41
|
const plans = new WeakMap<Function, InjectionPlan>();
|
|
42
|
+
const containers = new WeakMap<object, Container>();
|
|
39
43
|
|
|
40
44
|
/**
|
|
41
45
|
* The injection point of a parameter or property decorator (`@Autowired`, `@Optional`, `@Named`...):
|
|
@@ -73,6 +77,38 @@ export namespace InjectionUtil {
|
|
|
73
77
|
properties?.delete(key);
|
|
74
78
|
}
|
|
75
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Instances of the class (`target` is its prototype) remember the container that created them, for code
|
|
82
|
+
* that runs on them later and needs a component: `detached` injections, method decorators.
|
|
83
|
+
*/
|
|
84
|
+
export function keepContainer(target: any): void {
|
|
85
|
+
Reflect.defineMetadata(KEEPS_CONTAINER, true, target.constructor);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Called by the container for the instances of a class that keeps it. */
|
|
89
|
+
export function setContainer(instance: object, container: Container): void {
|
|
90
|
+
containers.set(instance, container);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The container that created `instance`, whose class called {@link keepContainer} (a `detached` injection
|
|
95
|
+
* does). Throws when no container created it: an object made with `new` has none.
|
|
96
|
+
*/
|
|
97
|
+
export function containerOf(instance: object, what?: string): Container {
|
|
98
|
+
const container = containers.get(instance);
|
|
99
|
+
if (!container) {
|
|
100
|
+
const owner = instance?.constructor?.name || '(anonymous class)';
|
|
101
|
+
const where = what ? `${owner}.${what}` : owner;
|
|
102
|
+
throw new ContainerError({
|
|
103
|
+
code: 'CELL_DI_NO_CONTAINER',
|
|
104
|
+
message: `${where} needs the container that created the instance, but no container created this ${owner}.`,
|
|
105
|
+
hint: 'Get the instance from the container (bind its class with @Component and inject it) instead of creating it with new.',
|
|
106
|
+
path: [where]
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return container;
|
|
110
|
+
}
|
|
111
|
+
|
|
76
112
|
export function setPostConstruct(target: any, key: string | symbol): void {
|
|
77
113
|
Reflect.defineMetadata(POST_CONSTRUCT, key, target.constructor);
|
|
78
114
|
}
|
|
@@ -83,7 +119,8 @@ export namespace InjectionUtil {
|
|
|
83
119
|
plan = {
|
|
84
120
|
parameters: parameters(type),
|
|
85
121
|
properties: [...(Reflect.getMetadata(PROPERTIES, type) as Map<string | symbol, Point> | undefined)?.values() ?? []].map(complete),
|
|
86
|
-
postConstruct: Reflect.getMetadata(POST_CONSTRUCT, type)
|
|
122
|
+
postConstruct: Reflect.getMetadata(POST_CONSTRUCT, type),
|
|
123
|
+
keepsContainer: Reflect.getMetadata(KEEPS_CONTAINER, type) === true
|
|
87
124
|
};
|
|
88
125
|
plans.set(type, plan);
|
|
89
126
|
}
|
|
@@ -132,4 +169,4 @@ export namespace InjectionUtil {
|
|
|
132
169
|
const owner = target.owner?.name || '(anonymous class)';
|
|
133
170
|
return typeof target.key === 'number' ? `${owner} constructor parameter #${target.key}` : `${owner}.${String(target.key)}`;
|
|
134
171
|
}
|
|
135
|
-
}
|
|
172
|
+
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Component, Autowired } from '../annotation';
|
|
2
|
-
import { ContainerUtil } from '../container';
|
|
3
2
|
import { ContextInitializer, ExpressionContext, ExpressionHandler } from './expression-protocol';
|
|
4
3
|
import { ExpressionEngine } from './expression-engine';
|
|
5
4
|
import { ExpressionUtil } from './expression-util';
|
|
@@ -10,19 +9,22 @@ export class CoreContextInitializer implements ContextInitializer {
|
|
|
10
9
|
@Autowired(ExpressionEngine)
|
|
11
10
|
protected readonly engine: ExpressionEngine;
|
|
12
11
|
|
|
12
|
+
// Read when a template calls eval: the handler needs the context initializers.
|
|
13
|
+
@Autowired({ id: ExpressionHandler, detached: true })
|
|
14
|
+
protected readonly expressionHandler: ExpressionHandler;
|
|
15
|
+
|
|
13
16
|
initialize(ctx: ExpressionContext): void {
|
|
14
17
|
if (typeof process !== 'undefined') {
|
|
15
18
|
// An empty variable counts as unset, so `${env.PORT ?? 3000}` takes the default.
|
|
16
19
|
ctx.env = { ...ExpressionUtil.definedEnv(process.env), _ignoreEl: true };
|
|
17
20
|
}
|
|
18
|
-
const expressionHandler = ContainerUtil.get<ExpressionHandler>(ExpressionHandler);
|
|
19
21
|
this.engine.addTransforms({
|
|
20
22
|
replace: (val: string, searchValue: string | RegExp, replaceValue: string) => val && val.replace(new RegExp(searchValue, 'g'), replaceValue),
|
|
21
23
|
regexp: (pattern: string, flags?: string) => new RegExp(pattern, flags),
|
|
22
|
-
eval: (text: string) => expressionHandler.handle(text)
|
|
24
|
+
eval: (text: string) => this.expressionHandler.handle(text)
|
|
23
25
|
});
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
priority = 500;
|
|
27
29
|
|
|
28
|
-
}
|
|
30
|
+
}
|