Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lorensr opened this issue Dec 30, 2022 · 2 comments
Open

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

lorensr opened this issue Dec 30, 2022 · 2 comments

Comments

@lorensr
Copy link
Contributor

lorensr commented Dec 30, 2022

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'] },
  ...
})
vkarpov15 added a commit to temporalio/samples-typescript that referenced this issue Jan 13, 2023
vkarpov15 added a commit to temporalio/samples-typescript that referenced this issue Jan 20, 2023
* working example for temporalio/documentation#1860

* package.json and README cleanup

* create CI task for new samples

* quick readme fix

* fix copy shared files with new samples and github CI
@flossypurse
Copy link
Contributor

@lorensr since we have this sample temporalio/samples-typescript#228
Can we close this issue?

@lorensr
Copy link
Contributor Author

lorensr commented Feb 13, 2023

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.

TRIPH925 added a commit to TRIPH925/Sample-TypeScript that referenced this issue Mar 12, 2024
* working example for temporalio/documentation#1860

* package.json and README cleanup

* create CI task for new samples

* quick readme fix

* fix copy shared files with new samples and github CI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants