Last active
June 26, 2023 20:24
-
-
Save lorensr/960c6151ccfc275b904c6a28ac18cf8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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