Multiple mode is an optimization technique useful when you want to process all available parts in a chunk synchronously, rather than yielding once per part.
This is particularly valuable for technologies like GraphQL where you can commit multiple payloads to the store in a single synchronous operation instead of across multiple process ticks.
Enable multiple mode by setting options.multiple to true:
const chunks = await fetch('/api').then((response) => meros(response, { multiple: true }));for await (const parts of chunks) { // parts is an array of Part objects for (const part of parts) { // Process each part in the batch }}
Multiple mode is ideal for GraphQL @defer and @stream directives:
import { meros } from 'meros';interface GraphQLResponse { data?: any; errors?: any[]; extensions?: any;}const chunks = await fetch('/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: '...' })}).then((response) => meros<GraphQLResponse>(response, { multiple: true }));for await (const parts of chunks) { // Commit all parts to the store synchronously const updates = parts.map(part => { if (part.json) { return part.body.data; } }).filter(Boolean); // Single synchronous store update with all data store.merge(updates);}
// Each part triggers a separate async iterationfor await (const part of parts) { store.update(part); // Async boundary // Next part waits for next tick}
One async iteration per part
Store updates happen across multiple ticks
More async overhead
// All parts in chunk processed togetherfor await (const parts of chunks) { for (const part of parts) { // Synchronous loop - no async boundary } store.batchUpdate(parts); // Single update}
const chunks = await fetch('/api').then((response) => meros(response, { multiple: true }));for await (const parts of chunks) { // Aggregate data from all parts in the chunk const aggregated = parts.reduce((acc, part) => { if (part.json) { return { ...acc, ...part.body }; } return acc; }, {}); // Single operation with aggregated data processData(aggregated);}