@@ -78,10 +78,12 @@ import {
7878 MetricMeter ,
7979 Priority ,
8080 ActivityCancellationDetails ,
81+ IllegalStateError ,
8182} from '@temporalio/common' ;
8283import { msToNumber } from '@temporalio/common/lib/time' ;
8384import { SymbolBasedInstanceOfError } from '@temporalio/common/lib/type-helpers' ;
8485import { ActivityCancellationDetailsHolder } from '@temporalio/common/lib/activity-cancellation-details' ;
86+ import { Client } from '@temporalio/client' ;
8587
8688export {
8789 ActivityFunction ,
@@ -238,93 +240,79 @@ export class Context {
238240 }
239241
240242 /**
241- * Holds information about the current executing Activity.
242- */
243- public readonly info : Info ;
244-
245- /**
246- * A Promise that fails with a {@link CancelledFailure} when cancellation of this activity is requested. The promise
247- * is guaranteed to never successfully resolve. Await this promise in an Activity to get notified of cancellation.
248- *
249- * Note that to get notified of cancellation, an activity must _also_ {@link Context.heartbeat}.
243+ * **Not** meant to instantiated by Activity code, used by the worker.
250244 *
251- * @see [Cancellation](/api/namespaces/activity#cancellation)
245+ * @ignore
252246 */
253- public readonly cancelled : Promise < never > ;
247+ constructor (
248+ /**
249+ * Holds information about the current executing Activity.
250+ */
251+ public readonly info : Info ,
254252
255- /**
256- * An {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal | `AbortSignal`} that can be used to react to
257- * Activity cancellation.
258- *
259- * This can be passed in to libraries such as
260- * {@link https://www.npmjs.com/package/node-fetch#request-cancellation-with-abortsignal | fetch} to abort an
261- * in-progress request and
262- * {@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options child_process}
263- * to abort a child process, as well as other built-in node modules and modules found on npm.
264- *
265- * Note that to get notified of cancellation, an activity must _also_ {@link Context.heartbeat}.
266- *
267- * @see [Cancellation](/api/namespaces/activity#cancellation)
268- */
269- public readonly cancellationSignal : AbortSignal ;
253+ /**
254+ * A Promise that fails with a {@link CancelledFailure} when cancellation of this activity is requested. The promise
255+ * is guaranteed to never successfully resolve. Await this promise in an Activity to get notified of cancellation.
256+ *
257+ * Note that to get notified of cancellation, an activity must _also_ {@link Context.heartbeat}.
258+ *
259+ * @see [Cancellation](/api/namespaces/activity#cancellation)
260+ */
261+ public readonly cancelled : Promise < never > ,
270262
271- /**
272- * The heartbeat implementation, injected via the constructor.
273- */
274- protected readonly heartbeatFn : ( details ?: any ) => void ;
263+ /**
264+ * An {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal | `AbortSignal`} that can be used to react to
265+ * Activity cancellation.
266+ *
267+ * This can be passed in to libraries such as
268+ * {@link https://www.npmjs.com/package/node-fetch#request-cancellation-with-abortsignal | fetch} to abort an
269+ * in-progress request and
270+ * {@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options child_process}
271+ * to abort a child process, as well as other built-in node modules and modules found on npm.
272+ *
273+ * Note that to get notified of cancellation, an activity must _also_ {@link Context.heartbeat}.
274+ *
275+ * @see [Cancellation](/api/namespaces/activity#cancellation)
276+ */
277+ public readonly cancellationSignal : AbortSignal ,
275278
276- /**
277- * The logger for this Activity.
278- *
279- * This defaults to the `Runtime`'s Logger (see {@link Runtime.logger}). Attributes from the current Activity context
280- * are automatically included as metadata on every log entries. An extra `sdkComponent` metadata attribute is also
281- * added, with value `activity`; this can be used for fine-grained filtering of log entries further downstream.
282- *
283- * To customize log attributes, register a {@link ActivityOutboundCallsInterceptor} that intercepts the
284- * `getLogAttributes()` method.
285- *
286- * Modifying the context logger (eg. `context.log = myCustomLogger` or by an {@link ActivityInboundLogInterceptor}
287- * with a custom logger as argument) is deprecated. Doing so will prevent automatic inclusion of custom log attributes
288- * through the `getLogAttributes()` interceptor. To customize _where_ log messages are sent, set the
289- * {@link Runtime.logger} property instead.
290- */
291- public log : Logger ;
279+ /**
280+ * The heartbeat implementation, injected via the constructor.
281+ */
282+ protected readonly heartbeatFn : ( details ?: any ) => void ,
292283
293- /**
294- * Get the metric meter for this activity with activity-specific tags.
295- *
296- * To add custom tags, register a {@link ActivityOutboundCallsInterceptor} that
297- * intercepts the `getMetricTags()` method.
298- */
299- public readonly metricMeter : MetricMeter ;
284+ private readonly _client : Client | undefined ,
300285
301- /**
302- * Holder object for activity cancellation details
303- */
304- private readonly _cancellationDetails : ActivityCancellationDetailsHolder ;
286+ /**
287+ * The logger for this Activity.
288+ *
289+ * This defaults to the `Runtime`'s Logger (see {@link Runtime.logger}). Attributes from the current Activity context
290+ * are automatically included as metadata on every log entries. An extra `sdkComponent` metadata attribute is also
291+ * added, with value `activity`; this can be used for fine-grained filtering of log entries further downstream.
292+ *
293+ * To customize log attributes, register a {@link ActivityOutboundCallsInterceptor} that intercepts the
294+ * `getLogAttributes()` method.
295+ *
296+ * Modifying the context logger (eg. `context.log = myCustomLogger` or by an {@link ActivityInboundLogInterceptor}
297+ * with a custom logger as argument) is deprecated. Doing so will prevent automatic inclusion of custom log attributes
298+ * through the `getLogAttributes()` interceptor. To customize _where_ log messages are sent, set the
299+ * {@link Runtime.logger} property instead.
300+ */
301+ public log : Logger ,
305302
306- /**
307- * **Not** meant to instantiated by Activity code, used by the worker.
308- *
309- * @ignore
310- */
311- constructor (
312- info : Info ,
313- cancelled : Promise < never > ,
314- cancellationSignal : AbortSignal ,
315- heartbeat : ( details ?: any ) => void ,
316- log : Logger ,
317- metricMeter : MetricMeter ,
318- details : ActivityCancellationDetailsHolder
319- ) {
320- this . info = info ;
321- this . cancelled = cancelled ;
322- this . cancellationSignal = cancellationSignal ;
323- this . heartbeatFn = heartbeat ;
324- this . log = log ;
325- this . metricMeter = metricMeter ;
326- this . _cancellationDetails = details ;
327- }
303+ /**
304+ * Get the metric meter for this activity with activity-specific tags.
305+ *
306+ * To add custom tags, register a {@link ActivityOutboundCallsInterceptor} that
307+ * intercepts the `getMetricTags()` method.
308+ */
309+ public readonly metricMeter : MetricMeter ,
310+
311+ /**
312+ * Holder object for activity cancellation details
313+ */
314+ private readonly _cancellationDetails : ActivityCancellationDetailsHolder
315+ ) { }
328316
329317 /**
330318 * Send a {@link https://docs.temporal.io/concepts/what-is-an-activity-heartbeat | heartbeat} from an Activity.
@@ -351,6 +339,25 @@ export class Context {
351339 this . heartbeatFn ( details ) ;
352340 } ;
353341
342+ /**
343+ * A Temporal Client, bound to the same Temporal Namespace as the Worker executing this Activity.
344+ *
345+ * May throw an {@link IllegalStateError} if the Activity is running inside a `MockActivityEnvironment`
346+ * that was created without a Client.
347+ *
348+ * @experimental Client support over `NativeConnection` is experimental. Error handling may be
349+ * incomplete or different from what would be observed using a {@link Connection}
350+ * instead. Client doesn't support cancellation through a Signal.
351+ */
352+ public get client ( ) : Client {
353+ if ( this . _client === undefined ) {
354+ throw new IllegalStateError (
355+ 'No Client available. This may be a MockActivityEnvironment that was created without a Client.'
356+ ) ;
357+ }
358+ return this . _client ;
359+ }
360+
354361 /**
355362 * Helper function for sleeping in an Activity.
356363 * @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
@@ -481,6 +488,22 @@ export function cancellationSignal(): AbortSignal {
481488 return Context . current ( ) . cancellationSignal ;
482489}
483490
491+ /**
492+ * A Temporal Client, bound to the same Temporal Namespace as the Worker executing this Activity.
493+ *
494+ * May throw an {@link IllegalStateError} if the Activity is running inside a `MockActivityEnvironment`
495+ * that was created without a Client.
496+ *
497+ * This is a shortcut for `Context.current().client` (see {@link Context.client}).
498+ *
499+ * @experimental Client support over `NativeConnection` is experimental. Error handling may be
500+ * incomplete or different from what would be observed using a {@link Connection}
501+ * instead. Client doesn't support cancellation through a Signal.
502+ */
503+ export function getClient ( ) : Client {
504+ return Context . current ( ) . client ;
505+ }
506+
484507/**
485508 * Get the metric meter for the current activity, with activity-specific tags.
486509 *
0 commit comments