@@ -80,7 +80,9 @@ function resolveDiagnosticsChannel(): DiagnosticsChannelModule | undefined {
8080 . getBuiltinModule === 'function'
8181 ) {
8282 dc = (
83- process as { getBuiltinModule : ( id : string ) => DiagnosticsChannelModule }
83+ process as {
84+ getBuiltinModule : ( id : string ) => DiagnosticsChannelModule ;
85+ }
8486 ) . getBuiltinModule ( 'node:diagnostics_channel' ) ;
8587 }
8688 if ( ! dc && typeof require === 'function' ) {
@@ -97,80 +99,56 @@ function resolveDiagnosticsChannel(): DiagnosticsChannelModule | undefined {
9799
98100const dc = resolveDiagnosticsChannel ( ) ;
99101
100- const channels : GraphQLChannels | undefined = dc && {
101- execute : dc . tracingChannel ( 'graphql:execute' ) ,
102- parse : dc . tracingChannel ( 'graphql:parse' ) ,
103- validate : dc . tracingChannel ( 'graphql:validate' ) ,
104- resolve : dc . tracingChannel ( 'graphql:resolve' ) ,
105- subscribe : dc . tracingChannel ( 'graphql:subscribe' ) ,
106- } ;
107-
108102/**
109- * Internal accessor used at emission sites. Returns `undefined` when
110- * `node:diagnostics_channel` isn't available on this runtime, allowing
111- * emission sites to short-circuit on a single property access.
103+ * Per-channel handles, resolved once at module load. `undefined` when
104+ * `node:diagnostics_channel` isn't available. Emission sites read these
105+ * directly to keep the no-subscriber fast path to a single property access
106+ * plus a `hasSubscribers` check (no function calls, no closures).
112107 *
113108 * @internal
114109 */
115- export function getChannels ( ) : GraphQLChannels | undefined {
116- return channels ;
117- }
110+ export const parseChannel : MinimalTracingChannel | undefined =
111+ dc ?. tracingChannel ( 'graphql:parse' ) ;
112+ /** @internal */
113+ export const validateChannel : MinimalTracingChannel | undefined =
114+ dc ?. tracingChannel ( 'graphql:validate' ) ;
115+ /** @internal */
116+ export const executeChannel : MinimalTracingChannel | undefined =
117+ dc ?. tracingChannel ( 'graphql:execute' ) ;
118+ /** @internal */
119+ export const subscribeChannel : MinimalTracingChannel | undefined =
120+ dc ?. tracingChannel ( 'graphql:subscribe' ) ;
121+ /** @internal */
122+ export const resolveChannel : MinimalTracingChannel | undefined =
123+ dc ?. tracingChannel ( 'graphql:resolve' ) ;
118124
119125/**
120- * Gate for emission sites. Returns `true` when the named channel exists and
121- * publishing should proceed.
122- *
123- * Uses `!== false` rather than a truthy check so runtimes which do not
124- * implement the aggregated `hasSubscribers` getter on `TracingChannel` still
125- * publish.
126+ * Publish a synchronous operation through `channel`. Caller has already
127+ * verified that a subscriber is attached; this helper exists only so the
128+ * traced path doesn't need to be duplicated at every emission site.
126129 *
127130 * @internal
128131 */
129- export function shouldTrace (
130- channel : MinimalTracingChannel | undefined ,
131- ) : channel is MinimalTracingChannel {
132- // eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare
133- return channel !== undefined && channel . hasSubscribers !== false ;
134- }
135-
136- /**
137- * Publish a synchronous operation through the named graphql tracing channel,
138- * short-circuiting to `fn()` when the channel isn't registered or nothing is
139- * listening.
140- *
141- * @internal
142- */
143- export function maybeTraceSync < T > (
144- name : keyof GraphQLChannels ,
145- ctxFactory : ( ) => object ,
132+ export function traceSync < T > (
133+ channel : MinimalTracingChannel ,
134+ ctx : object ,
146135 fn : ( ) => T ,
147136) : T {
148- const channel = getChannels ( ) ?. [ name ] ;
149- if ( ! shouldTrace ( channel ) ) {
150- return fn ( ) ;
151- }
152- return channel . traceSync ( fn , ctxFactory ( ) ) ;
137+ return channel . traceSync ( fn , ctx ) ;
153138}
154139
155140/**
156- * Publish a mixed sync-or-promise operation through the named graphql tracing
157- * channel .
141+ * Publish a mixed sync-or-promise operation through `channel`. Caller has
142+ * already verified that a subscriber is attached .
158143 *
159144 * @internal
160145 */
161- export function maybeTraceMixed < T > (
162- name : keyof GraphQLChannels ,
163- ctxFactory : ( ) => object ,
146+ export function traceMixed < T > (
147+ channel : MinimalTracingChannel ,
148+ ctxInput : object ,
164149 fn : ( ) => T | Promise < T > ,
165150) : T | Promise < T > {
166- const channel = getChannels ( ) ?. [ name ] ;
167- if ( ! shouldTrace ( channel ) ) {
168- return fn ( ) ;
169- }
170- const ctx = ctxFactory ( ) as {
171- error ?: unknown ;
172- result ?: unknown ;
173- } ;
151+ const ctx = ctxInput as { error ?: unknown ; result ?: unknown } ;
174152
175153 return channel . start . runStores ( ctx , ( ) => {
176154 let result : T | Promise < T > ;
0 commit comments