Skip to content

Instantly share code, notes, and snippets.

@stevekinney
Created December 12, 2023 20:42
Show Gist options
  • Save stevekinney/4a477d2249b35680dde69eae24bb9647 to your computer and use it in GitHub Desktop.
Save stevekinney/4a477d2249b35680dde69eae24bb9647 to your computer and use it in GitHub Desktop.
import {
condition,
defineQuery,
defineSignal,
proxyActivities,
setHandler,
sleep
} from '@temporalio/workflow';
import OrderStatus, { formatStatus } from '../utilities/status';
import type * as activities from './activities';
const { createOrder, updateOrder, processPayment, sendFollowUp } = proxyActivities<
typeof activities
>({
startToCloseTimeout: '1 minute'
});
export const resturantConfirmation = defineSignal('confirm');
export const updateStatus = defineSignal<[OrderStatus]>('updateStatus');
export const getStatus = defineQuery('getStatus');
export async function kickOffFoodOrderWorkflow(items: Omit<Item, 'id'>[], id: string) {
const order = await createOrder(items, id);
let status: OrderStatus = order.status;
let confirmed = false;
let paymentVerified = false;
status = await updateOrder(id, OrderStatus.ProcessingPayment);
await processPayment(id);
paymentVerified = true;
// We can use signals to communicate with the workflow.
setHandler(resturantConfirmation, () => {
confirmed = true;
});
// We can use queries to get information from the workflow.
setHandler(getStatus, () => {
return formatStatus(status);
});
// Pause and wait for this expression to be truthy.
await condition(() => paymentVerified);
status = await updateOrder(id, OrderStatus.WaitingForRestaurantConfirmation);
await condition(() => confirmed);
status = await updateOrder(id, OrderStatus.Confirmed);
// Think about all of the code you're *not* writing.
await sleep('1 day');
await sendFollowUp();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment