Skip to content

Commit c01bdc0

Browse files
authored
docs: incremental loading (#737)
1 parent 3382d30 commit c01bdc0

5 files changed

Lines changed: 153 additions & 3 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
sidebar_position: 4
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+
```tsx
14+
import { useLazyLoadQuery } from "@reactive-dot/react";
15+
16+
function TotalStaked() {
17+
const ledgers = useLazyLoadQuery((query) =>
18+
query.readStorages(
19+
"Staking",
20+
"Ledger",
21+
addresses.map((address) => [address] as const),
22+
),
23+
);
24+
25+
return (
26+
<p>Total staked: {ledgers.reduce((prev, curr) => prev + curr.total, 0n)}</p>
27+
);
28+
}
29+
```
30+
31+
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.
32+
33+
When you pass `{ stream: true }` to the query, each item in the result array will now be either:
34+
35+
- A resolved value (the response you expect), or
36+
- A special `pending` symbol from `@reactive-dot/core`, indicating that the data for that item hasn’t arrived yet.
37+
38+
This allows your UI to update incrementally as each item resolves:
39+
40+
```tsx
41+
import { pending } from "@reactive-dot/core";
42+
import { useLazyLoadQuery } from "@reactive-dot/react";
43+
44+
function TotalStaked() {
45+
const ledgers = useLazyLoadQuery((query) =>
46+
query.readStorages(
47+
"Staking",
48+
"Ledger",
49+
addresses.map((address) => [address] as const),
50+
{ stream: true },
51+
),
52+
);
53+
54+
const loadedLedgers = ledgers.filter((ledger) => ledger !== pending);
55+
56+
const hasMore = ledgers.includes(pending);
57+
58+
return (
59+
<p>
60+
Total staked:{" "}
61+
{loadedLedgers.reduce((prev, curr) => prev + curr.total, 0n)}
62+
{hasMore ? "..." : ""}
63+
</p>
64+
);
65+
}
66+
```
67+
68+
With `stream: true`, the component can render incrementally, updating the total as each balance loads.
69+
70+
:::tip
71+
You can use the presence of `pending` to show loading indicators, spinners, or skeletons while waiting for the rest of the data.
72+
:::

apps/docs/react/guides/smart-contract.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 5
2+
sidebar_position: 6
33
---
44

55
# Smart contract

apps/docs/react/guides/using-papi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 4
2+
sidebar_position: 5
33
---
44

55
# Using Polkadot-API (PAPI)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
:::

apps/docs/vue/guides/using-papi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 3
2+
sidebar_position: 4
33
---
44

55
# Using Polkadot-API (PAPI)

0 commit comments

Comments
 (0)