@temporalio/workflow 1.5.2 → 1.6.0

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/src/workflow.ts CHANGED
@@ -32,10 +32,12 @@ import {
32
32
  ChildWorkflowOptionsWithDefaults,
33
33
  ContinueAsNew,
34
34
  ContinueAsNewOptions,
35
+ DefaultSignalHandler,
35
36
  EnhancedStackTrace,
37
+ Handler,
36
38
  WorkflowInfo,
37
39
  } from './interfaces';
38
- import { LocalActivityDoBackoff, getActivator } from './internals';
40
+ import { LocalActivityDoBackoff, getActivator, maybeGetActivator } from './internals';
39
41
  import { Sinks } from './sinks';
40
42
  import { untrackPromise } from './stack-helpers';
41
43
  import { ChildWorkflowHandle, ExternalWorkflowHandle } from './workflow-handle';
@@ -843,18 +845,7 @@ export function workflowInfo(): WorkflowInfo {
843
845
  * Returns whether or not code is executing in workflow context
844
846
  */
845
847
  export function inWorkflowContext(): boolean {
846
- try {
847
- getActivator();
848
- return true;
849
- } catch (err: any) {
850
- // Use string comparison in case multiple versions of @temporalio/common are
851
- // installed in which case an instanceof check would fail.
852
- if (err.name === 'IllegalStateError') {
853
- return false;
854
- } else {
855
- throw err;
856
- }
857
- }
848
+ return maybeGetActivator() !== undefined;
858
849
  }
859
850
 
860
851
  /**
@@ -1151,19 +1142,6 @@ export function defineQuery<Ret, Args extends any[] = []>(name: string): QueryDe
1151
1142
  };
1152
1143
  }
1153
1144
 
1154
- /**
1155
- * A handler function capable of accepting the arguments for a given SignalDefinition or QueryDefinition.
1156
- */
1157
- export type Handler<
1158
- Ret,
1159
- Args extends any[],
1160
- T extends SignalDefinition<Args> | QueryDefinition<Ret, Args>
1161
- > = T extends SignalDefinition<infer A>
1162
- ? (...args: A) => void | Promise<void>
1163
- : T extends QueryDefinition<infer R, infer A>
1164
- ? (...args: A) => R
1165
- : never;
1166
-
1167
1145
  /**
1168
1146
  * Set a handler function for a Workflow query or signal.
1169
1147
  *
@@ -1178,21 +1156,48 @@ export function setHandler<Ret, Args extends any[], T extends SignalDefinition<A
1178
1156
  ): void {
1179
1157
  const activator = getActivator();
1180
1158
  if (def.type === 'signal') {
1181
- activator.signalHandlers.set(def.name, handler as any);
1182
- const bufferedSignals = activator.bufferedSignals.get(def.name);
1183
- if (bufferedSignals !== undefined && handler !== undefined) {
1184
- activator.bufferedSignals.delete(def.name);
1185
- for (const signal of bufferedSignals) {
1186
- activator.signalWorkflow(signal);
1187
- }
1159
+ if (typeof handler === 'function') {
1160
+ activator.signalHandlers.set(def.name, handler as any);
1161
+ activator.dispatchBufferedSignals();
1162
+ } else if (handler == null) {
1163
+ activator.signalHandlers.delete(def.name);
1164
+ } else {
1165
+ throw new TypeError(`Expected handler to be either a function or 'undefined'. Got: '${typeof handler}'`);
1188
1166
  }
1189
1167
  } else if (def.type === 'query') {
1190
- activator.queryHandlers.set(def.name, handler as any);
1168
+ if (typeof handler === 'function') {
1169
+ activator.queryHandlers.set(def.name, handler as any);
1170
+ } else if (handler == null) {
1171
+ activator.queryHandlers.delete(def.name);
1172
+ } else {
1173
+ throw new TypeError(`Expected handler to be either a function or 'undefined'. Got: '${typeof handler}'`);
1174
+ }
1191
1175
  } else {
1192
1176
  throw new TypeError(`Invalid definition type: ${(def as any).type}`);
1193
1177
  }
1194
1178
  }
1195
1179
 
1180
+ /**
1181
+ * Set a signal handler function that will handle signals calls for non-registered signal names.
1182
+ *
1183
+ * Signals are dispatched to the default signal handler in the order that they were accepted by the server.
1184
+ *
1185
+ * If this function is called multiple times for a given signal or query name the last handler will overwrite any previous calls.
1186
+ *
1187
+ * @param handler a function that will handle signals for non-registered signal names, or `undefined` to unset the handler.
1188
+ */
1189
+ export function setDefaultSignalHandler(handler: DefaultSignalHandler | undefined): void {
1190
+ const activator = getActivator();
1191
+ if (typeof handler === 'function') {
1192
+ activator.defaultSignalHandler = handler;
1193
+ activator.dispatchBufferedSignals();
1194
+ } else if (handler == null) {
1195
+ activator.defaultSignalHandler = undefined;
1196
+ } else {
1197
+ throw new TypeError(`Expected handler to be either a function or 'undefined'. Got: '${typeof handler}'`);
1198
+ }
1199
+ }
1200
+
1196
1201
  /**
1197
1202
  * Updates this Workflow's Search Attributes by merging the provided `searchAttributes` with the existing Search
1198
1203
  * Attributes, `workflowInfo().searchAttributes`.