@temporalio/workflow
Version:
Temporal.io SDK Workflow sub-package
74 lines • 2.72 kB
JavaScript
/**
* Type definitions for the Workflow end of the sinks mechanism.
*
* Sinks are a mechanism for exporting data from the Workflow isolate to the
* Node.js environment, they are necessary because the Workflow has no way to
* communicate with the outside World.
*
* Sinks are typically used for exporting logs, metrics and traces out from the
* Workflow.
*
* Sink functions may not return values to the Workflow in order to prevent
* breaking determinism.
*
* @module
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.proxySinks = proxySinks;
const global_attributes_1 = require("./global-attributes");
/**
* Get a reference to Sinks for exporting data out of the Workflow.
*
* These Sinks **must** be registered with the Worker in order for this
* mechanism to work.
*
* @example
* ```ts
* import { proxySinks, Sinks } from '@temporalio/workflow';
*
* interface MySinks extends Sinks {
* logger: {
* info(message: string): void;
* error(message: string): void;
* };
* }
*
* const { logger } = proxySinks<MyDependencies>();
* logger.info('setting up');
*
* export function myWorkflow() {
* return {
* async execute() {
* logger.info("hey ho");
* logger.error("lets go");
* }
* };
* }
* ```
*/
function proxySinks() {
return new Proxy({}, {
get(_, ifaceName) {
return new Proxy({}, {
get(_, fnName) {
return (...args) => {
const activator = (0, global_attributes_1.assertInWorkflowContext)('Proxied sinks functions may only be used from a Workflow Execution.');
activator.sinkCalls.push({
ifaceName: ifaceName,
fnName: fnName,
// Sink function doesn't get called immediately. Make a clone of the sink's args, so that further mutations
// to these objects don't corrupt the args that the sink function will receive. Only available from node 17.
args: globalThis.structuredClone ? globalThis.structuredClone(args) : args,
// activator.info is internally copy-on-write. This ensure that any further mutations
// to the workflow state in the context of the present activation will not corrupt the
// workflowInfo state that gets passed when the sink function actually gets called.
workflowInfo: activator.info,
});
};
},
});
},
});
}
//# sourceMappingURL=sinks.js.map
;