# Pattern to create an event based workflow

**URL:** <https://community.temporal.io/t/pattern-to-create-an-event-based-workflow/3609>\
**Category:** Community Support\
**Tags:** events, typescript-sdk, workflow-config\
**Created:** [December 15, 2021, 3:36pm UTC](https://community.temporal.io/t/pattern-to-create-an-event-based-workflow/3609 "2021-12-15T15:36:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GSblanchet](https://avatars.discourse-cdn.com/v4/letter/g/c4cdca/32.png) [@GSblanchet](https://community.temporal.io/u/GSblanchet)\
**Post date:** [December 15, 2021, 3:36pm UTC](https://community.temporal.io/t/pattern-to-create-an-event-based-workflow/3609/1 "2021-12-15T15:36:00Z")

</div>

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

> **[Workflows in TypeScript | Temporal Documentation](https://docs.temporal.io/typescript/workflows/)**
>
> Workflows are async functions that can orchestrate Activities and access special Workflow APIs, subject to deterministic limitations.

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,

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [December 16, 2021, 2:40am UTC](https://community.temporal.io/t/pattern-to-create-an-event-based-workflow/3609/2 "2021-12-16T02:40:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bergundy](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bergundy/32/900_2.png) [@bergundy](https://community.temporal.io/u/bergundy)\
**Post date:** [June 24, 2022, 5:58pm UTC](https://community.temporal.io/t/pattern-to-create-an-event-based-workflow/3609/3 "2022-06-24T17:58:48Z")

</div>

This is a pretty common pattern, I’d base your workflow on this example: [Workflows in TypeScript | Temporal Documentation](https://docs.temporal.io/typescript/workflows/#example)
