Slide 26
Slide 26 text
// Emitting customerIds 1 and 2 with a 25ms delay
createStream([1, 2], 25)
.pipe(
// Switch to request customer object
switchMap(id => requestCustomer(id)),
// Switch to request made by generating the customer code
switchMap((customer) => {
const code = makeCode(customer);
return requestByCode(code)
// Combine results into a bundle
.pipe(map(result => ({ customer, code, result })));
}),
// Switch to final request
switchMap(({ customer, result, ...bundle }) => {
return requestByCustomerAndResult(customer, result)
// Add the new result to the bundle
.pipe(map(combinedResult => ({ ...bundle, customer, result, combinedResult })));
}),
)
.subscribe(({ customer, code, result, combinedResult }) => {
// Update view or properties all at once
updateView('customer', customer.name);
updateView('code', code);
updateView('result', result.result);
updateView('combined', combinedResult);
});
Antipattern: Stateful Streams