Pattern to create an event based workflow

I Want to create an infinite workflow that will work using signal but I’m not sure which approche is the best one. the workflow that I would like to create will be infinite until it receives a signal to end itself.
I’m developing my workflows with the typescript-sdk.

I saw that in the typescript-sdk there is continueAsNew function so one of the pattern that I thought of is to set my signal handlers and continueAsNew

rough workflow example: `export async function myEventBasedFunction (){
sethandler(“signal1”,()=>{//do something based on signal1});
sethandler(“signal1”,()=>{//do something based on signal2});

continueAsNew();
}`

an other pattern that I though of is an infinite loop but I’m not sure if it will work but I saw it in one of the example of the typescript-sdk

specificaly the example: `import * as wf from ‘@temporalio/workflow’;

function useState<T = any>(name: string, initialValue: T) {
const signal = wf.defineSignal<[T]>(name);
const query = wf.defineQuery(name);
let state: T = initialValue;
return {
signal,
query,
get value() {
// need to use closure because function doesn’t rerun unlike React Hooks
return state;
},
set value(newVal: T) {
state = newVal;
},
};
}

// usage in Workflow file
const store = useState(‘my-store’, 10);
function MyWorkflow() {
wf.setHandler(store.signal, (newValue: T) => {
// console.log('updating ', name, newValue) // optional but useful for debugging
state = store.value;
});
wf.setHandler(store.query, () => store.value);
while (true) {
console.log('sleeping for ', store.value);
wf.sleep(store.value++ * 100); // you can mutate the value as well
}
}

// usage in Client file
await handle.signal(store.signal, 30);
const storeState = handle.query(store.query); // 30`

thanks for the advice,

I think that example in docs should call continueAsNew. You can’t really have an infinite workflow execution as the workflow history size is bound to 50K events. Eventually if you do hit this max the workflow execution will terminate.

This is a pretty common pattern, I’d base your workflow on this example: Workflows in TypeScript | Temporal Documentation