Skip to content

Instantly share code, notes, and snippets.

@lorensr
Last active June 26, 2023 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lorensr/960c6151ccfc275b904c6a28ac18cf8d to your computer and use it in GitHub Desktop.
Save lorensr/960c6151ccfc275b904c6a28ac18cf8d to your computer and use it in GitHub Desktop.
const addItem = defineSignal('addItem')
const checkout = defineSignal('checkout')
function cart(userId) {
const items = []
setHandler(addItem, (item) => items.push(item))
setHandler(checkout, () => {
const userInfo = await getUser(userId) // activity that gets a handle to the User workflow with Workflow Id = userId and does a 'getUserInfo' Query
const amount = calculateTotal(items) // helper function, not an activity
const paymentId = await chargeUser(userInfo, amount)
try {
const trackingNumber = await ship(items, userInfo)
} catch (e) {
await refundUser(paymentId)
throw e
}
const user = getExternalWorkflowHandle(userId)
user.signal(addOrder, { items, paymentId, trackingNumber })
})
await CancellationScope.current().cancelRequested
}
const setAddress = defineSignal('setAddress')
const setPaymentMethod = defineSignal('setPaymentMethod')
const addOrder = defineSignal('addOrder')
const getUserInfo = defineQuery('getUserInfo')
function user(userInfo) {
const orders = []
setHandler(setAddress, (address) => {
userInfo.address = address
await notifyUser('Address updated')
})
setHandler(setPaymentMethod, (paymentMethodId) => {
userInfo.paymentMethodId = paymentMethodId
await notifyUser('Payment method updated')
})
setHandler(addOrder, (order) => {
orders.push(order)
await notifyUser('Order complete!')
})
setHandler(getUserInfo, () => ({ ...userInfo, orders }))
await CancellationScope.current().cancelRequested
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment