@azure-tools/typespec-ts 0.56.0-dev.10 → 0.56.0-dev.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-tools/typespec-ts",
3
- "version": "0.56.0-dev.10",
3
+ "version": "0.56.0-dev.11",
4
4
  "description": "A TypeSpec emitter for TypeScript",
5
5
  "main": "dist/src/index.js",
6
6
  "type": "module",
@@ -72,6 +72,19 @@ class ContextManager {
72
72
  public getContext<K extends ContextKey>(key: K): Contexts[K] | undefined {
73
73
  return this.contexts.get(key) as Contexts[K] | undefined;
74
74
  }
75
+
76
+ /**
77
+ * Clears all stored contexts.
78
+ *
79
+ * The manager is a process-wide singleton, so the values provided during an
80
+ * emit (the `EmitContext`/`Program`, the TCGC `SdkContext`, the ts-morph
81
+ * `Project`, etc.) stay reachable from this map until the next emit overwrites
82
+ * them. Clearing at the end of an emit lets the whole previous program graph
83
+ * be collected instead of being retained until the following emit.
84
+ */
85
+ public clearContexts(): void {
86
+ this.contexts.clear();
87
+ }
75
88
  }
76
89
 
77
90
  // Expose the singleton instance of the context manager.
@@ -99,3 +112,13 @@ export function useContext<K extends ContextKey>(key: K): Contexts[K] {
99
112
  export function provideContext<K extends ContextKey>(key: K, value: Contexts[K]): void {
100
113
  contextManager.setContext(key, value);
101
114
  }
115
+
116
+ /**
117
+ * Clears all contexts held by the singleton context manager. Call this once an
118
+ * emit has finished writing its output so the retained program graph (compiler
119
+ * `EmitContext`/`Program`, TCGC `SdkContext`, ts-morph `Project`, binder, …) can
120
+ * be garbage collected instead of lingering until the next emit overwrites it.
121
+ */
122
+ export function clearContexts(): void {
123
+ contextManager.clearContexts();
124
+ }
@@ -29,6 +29,20 @@ export const flattenPropertyModelMap: Map<SdkModelPropertyType, SdkModelType> =
29
29
  */
30
30
  export const pagedModelsKeptPublic = new Set<SdkType>();
31
31
 
32
+ /**
33
+ * Releases the module-level state that accumulates while visiting a package's
34
+ * types. These collections are cleared at the start of every
35
+ * {@link visitPackageTypes} call, but because they live at module scope they
36
+ * otherwise keep the most recently emitted program's SDK types reachable until
37
+ * the next emit runs. Call this once an emit has fully finished so that state
38
+ * can be garbage collected.
39
+ */
40
+ export function resetSdkTypesState(): void {
41
+ emitQueue.clear();
42
+ flattenPropertyModelMap.clear();
43
+ pagedModelsKeptPublic.clear();
44
+ }
45
+
32
46
  export interface SdkTypeContext {
33
47
  operations: Map<Type, SdkServiceMethod<SdkHttpOperation>>;
34
48
  types: Map<Type, SdkType>;
package/src/index.ts CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  resolvePath,
11
11
  type CompilerHost,
12
12
  } from "@typespec/compiler";
13
- import { provideContext, useContext } from "./context-manager.js";
13
+ import { clearContexts, provideContext, useContext } from "./context-manager.js";
14
14
  import { buildRootIndex, buildSubClientIndexFile } from "./modular/build-root-index.js";
15
15
  import {
16
16
  AzureCoreDependencies,
@@ -43,7 +43,7 @@ import {
43
43
  } from "@azure-tools/typespec-client-generator-core";
44
44
  import { Project } from "ts-morph";
45
45
  import { provideBinder } from "./framework/hooks/binder.js";
46
- import { provideSdkTypes } from "./framework/hooks/sdk-types.js";
46
+ import { provideSdkTypes, resetSdkTypesState } from "./framework/hooks/sdk-types.js";
47
47
  import { loadStaticHelpers } from "./framework/load-static-helpers.js";
48
48
  import { ClientModel, ClientOptions } from "./interfaces.js";
49
49
  import { EmitterOptions } from "./lib.js";
@@ -188,6 +188,15 @@ export async function $onEmit(context: EmitContext) {
188
188
 
189
189
  await generateMetadataAndTest(dpgContext);
190
190
 
191
+ // Release the emitter's process-wide singleton state now that this emit has
192
+ // finished writing output. Without this, the context manager keeps the whole
193
+ // previous program graph (compiler `EmitContext`/`Program`, TCGC `SdkContext`,
194
+ // ts-morph `Project`, binder, …) reachable until the next emit overwrites it.
195
+ // Clearing here lets it be collected between emits, which matters for hosts
196
+ // that run many emits in one process (test suites, benchmarks, watch mode).
197
+ clearContexts();
198
+ resetSdkTypesState();
199
+
191
200
  async function enrichDpgContext() {
192
201
  const generationPathDetail: GenerationDirDetail = await calculateGenerationDir();
193
202
  dpgContext.generationPathDetail = generationPathDetail;