Skip to content

Instantly share code, notes, and snippets.

@Coly010
Created September 5, 2020 09:37
Show Gist options
  • Select an option

  • Save Coly010/fff546b5d5ae061e6f0bee4a02e16b1a to your computer and use it in GitHub Desktop.

Select an option

Save Coly010/fff546b5d5ae061e6f0bee4a02e16b1a to your computer and use it in GitHub Desktop.
const mappedInv$ =
// Gets all docs
this.invService.getAll().pipe(
// Stream each element of the array
mergeMap((inventory) => from(inventory)),
// Using each element of the array, do your next action
mergeMap((inventoryItem: InventoryItem) =>
// This use case involved getting more details from a difference collection
this.itemService.getAsync(inventoryItem.itemId).pipe(
take(1),
map((item) => ({ ...inventoryItem, item } as InventoryItem))
)
),
// Collect all the results back into one array
// Scan will collect over time, it may be worth using reduce()
scan(
(inventory: Inventory, inventoryItem) =>
inventory.find((i) => i.id === inventoryItem.id)
? inventory
: [...inventory, inventoryItem],
[]
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment