|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# Incremental loading |
| 6 | + |
| 7 | +## `stream` |
| 8 | + |
| 9 | +For multi-entry queries like `storages`, `runtimeApis`, etc., the `stream` directive allows the client to receive partial results as they become available, before the entire response is ready. |
| 10 | + |
| 11 | +Take this basic example, where a component displays the total balance across multiple staking positions: |
| 12 | + |
| 13 | +```vue |
| 14 | +<script setup lang="ts"> |
| 15 | +import { useLazyLoadQuery } from "@reactive-dot/vue"; |
| 16 | +
|
| 17 | +const { data: ledgers } = await useLazyLoadQuery((query) => |
| 18 | + query.readStorages( |
| 19 | + "Staking", |
| 20 | + "Ledger", |
| 21 | + addresses.map((address) => [address] as const), |
| 22 | + ), |
| 23 | +); |
| 24 | +
|
| 25 | +const totalStaked = computed(() => |
| 26 | + ledgers.value.reduce((prev, curr) => prev + curr.total, 0n), |
| 27 | +); |
| 28 | +</script> |
| 29 | +
|
| 30 | +<template> |
| 31 | + <p>Total staked: {{ totalStaked }}</p> |
| 32 | +</template> |
| 33 | +``` |
| 34 | + |
| 35 | +This works well for a small number of accounts, where results load quickly. But with many accounts, waiting for the full response may be too slow and undesirable. To show users a partial result as soon as possible, you can enable streaming. |
| 36 | + |
| 37 | +When you pass `{ stream: true }` to the query, each item in the result array will now be either: |
| 38 | + |
| 39 | +- A resolved value (the response you expect), or |
| 40 | +- A special `pending` symbol from `@reactive-dot/core`, indicating that the data for that item hasn’t arrived yet. |
| 41 | + |
| 42 | +This allows your UI to update incrementally as each item resolves: |
| 43 | + |
| 44 | +```vue |
| 45 | +<script setup lang="ts"> |
| 46 | +import { pending } from "@reactive-dot/core"; |
| 47 | +import { useLazyLoadQuery } from "@reactive-dot/vue"; |
| 48 | +
|
| 49 | +const { data: ledgers } = await useLazyLoadQuery((query) => |
| 50 | + query.readStorages( |
| 51 | + "Staking", |
| 52 | + "Ledger", |
| 53 | + addresses.map((address) => [address] as const), |
| 54 | + { stream: true }, |
| 55 | + ), |
| 56 | +); |
| 57 | +
|
| 58 | +const loadedLedgers = computed(() => |
| 59 | + ledgers.value.filter((ledger) => ledger !== pending), |
| 60 | +); |
| 61 | +
|
| 62 | +const totalStaked = computed(() => |
| 63 | + loadedLedgers.value.reduce((prev, curr) => prev + curr.total, 0n), |
| 64 | +); |
| 65 | +
|
| 66 | +const hasMore = computed(() => ledgers.value.includes(pending)); |
| 67 | +</script> |
| 68 | +
|
| 69 | +<template> |
| 70 | + <p>Total staked: {{ totalStaked }}<span v-if="hasMore">...</span></p> |
| 71 | +</template> |
| 72 | +``` |
| 73 | + |
| 74 | +With `stream: true`, the component can render incrementally, updating the total as each balance loads. |
| 75 | + |
| 76 | +:::tip |
| 77 | +You can use the presence of `pending` to show loading indicators, spinners, or skeletons while waiting for the rest of the data. |
| 78 | +::: |
0 commit comments