temporalio / documentation

Temporal documentation
https://docs.temporal.io
Other
88 stars 228 forks source link

[ Documentation request ] Dev guide > TS > writing helper functions #1860

Open lorensr opened 1 year ago

lorensr commented 1 year ago

Brief description

There are various issues related to how to share functions between bundled workflow code and activity code.

Your recommended content

One example is how to log from such functions.

I have a number of modules that I want to use inside and outside a workflow. I would like these modules to use the logger sink when inside a workflow context, but use its current logger implementation when outside a workflow (without changing all these source files).

import myLogger from '~/myLogger';

// I wanna use this function in both contexts and have it "just work"
function thisIsPureExceptForTheLoggerStatement() {
  // ...
  myLogger.log(`Some debug information`);
  // ...
}

Answer: ignore the real logger module in the bundleOptions, something like this (untested):

// myLogger.js

import { inWorkflowContext } from '@temporalio/workflow';
import nonWorkflowLogger from './myRealLogger';

const logger = inWorkflowContext() ? proxySinks().logger : nonWorkflowLogger;
export default logger;

// worker.js

await Worker.create({
  bundlerOptions: { ignoreModules: ['./myRealLogger'] },
  ...
})
flossypurse commented 1 year ago

@lorensr since we have this sample https://github.com/temporalio/samples-typescript/pull/228 Can we close this issue?

lorensr commented 1 year ago

I think it would be helpful to have a dev guide section on it, as it's a common enough issue and many won't find it in the samples.