Skip to content

Commit c14770c

Browse files
committed
fix: avoid constructing unused service instances
1 parent 9704bec commit c14770c

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/schema-to-typescript/common/client.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
objectExpression,
1919
objectProperty,
2020
program,
21+
returnStatement,
2122
spreadElement,
2223
Statement,
2324
stringLiteral,
@@ -166,9 +167,17 @@ export function generateClient({
166167
});
167168
clientClassBody.body.push(
168169
attachJsDocComment(
169-
classProperty(
170+
classMethod(
171+
'get',
170172
identifier(applyEntityNameCase(tag, 'camelCase')),
171-
newExpression(identifier(name), [memberExpression(thisExpression(), identifier('getClient'))])
173+
[],
174+
blockStatement([
175+
returnStatement(
176+
callExpression(memberExpression(thisExpression(), identifier('getServiceInstance')), [
177+
identifier(name)
178+
])
179+
)
180+
])
172181
),
173182
renderJsDoc(jsdoc, jsDocRenderConfig)
174183
)
@@ -246,14 +255,6 @@ export function generateClient({
246255
)
247256
)
248257
])
249-
),
250-
classProperty(
251-
identifier('createClientWithServices'),
252-
memberExpression(identifier(commonHttpClientImportName), identifier('createClientWithServices')),
253-
null,
254-
null,
255-
false,
256-
true
257258
)
258259
);
259260

src/schema-to-typescript/common/core/common-http-service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import * as commonHttpClient from './common-http-client';
22

33
export class CommonHttpService {
4+
protected serviceInstancesMap: Map<
5+
new (getClientInstance: () => commonHttpClient.CommonHttpClient) => CommonHttpService,
6+
CommonHttpService
7+
> = new Map();
8+
9+
protected getServiceInstance<T extends CommonHttpService>(
10+
serviceClass: new (getClientInstance: () => commonHttpClient.CommonHttpClient) => T
11+
): T {
12+
let serviceInstance = this.serviceInstancesMap.get(serviceClass);
13+
if (!serviceInstance) {
14+
serviceInstance = new serviceClass(this.getClientInstance);
15+
this.serviceInstancesMap.set(serviceClass, serviceInstance);
16+
}
17+
return serviceInstance as T;
18+
}
19+
420
protected getClientInstance: () => commonHttpClient.CommonHttpClient;
521
constructor(getClientInstance: () => commonHttpClient.CommonHttpClient) {
622
this.getClientInstance = () => {

0 commit comments

Comments
 (0)