Skip to content

Commit 3ce098b

Browse files
authored
fix(activity): Change to arrow function notation so class context is not lost (#1249)
<!--- Note to EXTERNAL Contributors --> <!-- Thanks for opening a PR! If it is a significant code change, please **make sure there is an open issue** for this. We work best with you when we have accepted the idea first before you code. --> <!--- For ALL Contributors 👇 --> ## What was changed <!-- Describe what has changed in this PR --> Added missing bindings on activity context class. ## Why? <!-- Tell your future self why have you made these changes --> Since we are restoring the `Context` class from `AsyncLocalStorage`. Class scope is missing and thus rebinding is necessary. Attempting to use `Context.current().heartbeat()` or `Context.current().sleep()` would result on `this` property is undefined error. ## Checklist <!--- add/delete as needed ---> 1. Closes <!-- add issue number here --> 2. How was this tested: <!--- Please describe how you tested your changes/how we can test them --> I tested it with the `activities-cancellation-heartbeating` SDK example. 3. Any docs updates needed? <!--- update README if applicable or point out where to update docs.temporal.io --> Since it addresses a current bug, no doc changes are necessary.
1 parent e769006 commit 3ce098b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

packages/activity/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ export class Context {
283283
* :warning: Cancellation is not propagated from this function, use {@link cancelled} or {@link cancellationSignal} to
284284
* subscribe to cancellation notifications.
285285
*/
286-
public heartbeat(details?: unknown): void {
286+
public heartbeat = (details?: unknown): void => {
287287
this.heartbeatFn(details);
288-
}
288+
};
289289

290290
/**
291291
* Gets the context of the current Activity.
@@ -305,11 +305,11 @@ export class Context {
305305
* @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
306306
* @returns A Promise that either resolves when `ms` is reached or rejects when the Activity is cancelled
307307
*/
308-
public sleep(ms: Duration): Promise<void> {
308+
public sleep = (ms: Duration): Promise<void> => {
309309
let handle: NodeJS.Timeout;
310310
const timer = new Promise<void>((resolve) => {
311311
handle = setTimeout(resolve, msToNumber(ms));
312312
});
313313
return Promise.race([this.cancelled.finally(() => clearTimeout(handle)), timer]);
314-
}
314+
};
315315
}

0 commit comments

Comments
 (0)