documentation icon indicating copy to clipboard operation
documentation copied to clipboard

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

Open lorensr opened this issue 2 years ago • 2 comments

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'] },
  ...
})

lorensr avatar Dec 30 '22 22:12 lorensr

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

flossypurse avatar Feb 13 '23 16:02 flossypurse

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.

lorensr avatar Feb 13 '23 23:02 lorensr