Skip to Content
Progressive response

Progressive Response example

Demonstrates an experimental feature called “Progressive Response” that implemented with JSONLinesResponse on the server and handled with progressive function on the client. For demonstational purposes users and tasks resolve in random order, using setTimeout with a random delay of 0-10 seconds. Press “Load” button to load users and tasks. The response is streamed progressively, so you can see the results as they come in, rather than waiting for the entire response to be ready. Open devtools to see the network requests.

Result

Code

ProgressiveService.ts
ProgressiveController.ts
ProgressiveExample.tsx
1import { JSONLinesResponse, type VovkIteration } from 'vovk';
2import type ProgressiveController from './ProgressiveController';
3
4export default class ProgressiveService {
5 static async getUsers() {
6 await new Promise((resolve) => setTimeout(resolve, Math.random() * 10_000));
7 return [
8 { id: 1, name: 'John Doe' },
9 { id: 2, name: 'Jane Smith' },
10 { id: 3, name: 'Alice Johnson' },
11 { id: 4, name: 'Bob Brown' },
12 { id: 5, name: 'Charlie White' },
13 ];
14 }
15
16 static async getTasks() {
17 await new Promise((resolve) => setTimeout(resolve, Math.random() * 10_000));
18 return [
19 { id: 1, title: 'Task One', completed: false },
20 { id: 2, title: 'Task Two', completed: true },
21 { id: 3, title: 'Task Three', completed: false },
22 { id: 4, title: 'Task Four', completed: true },
23 { id: 5, title: 'Task Five', completed: false },
24 ];
25 }
26
27 static streamProgressiveResponse(
28 resp: JSONLinesResponse<VovkIteration<typeof ProgressiveController.streamProgressiveResponse>>
29 ) {
30 Promise.all([
31 this.getUsers().then((users) => resp.send({ users })),
32 this.getTasks().then((tasks) => resp.send({ tasks })),
33 ])
34 .then(resp.close)
35 .catch(resp.throw);
36 }
37}
1import { JSONLinesResponse, type VovkIteration } from 'vovk';
2import type ProgressiveController from './ProgressiveController';
3
4export default class ProgressiveService {
5 static async getUsers() {
6 await new Promise((resolve) => setTimeout(resolve, Math.random() * 10_000));
7 return [
8 { id: 1, name: 'John Doe' },
9 { id: 2, name: 'Jane Smith' },
10 { id: 3, name: 'Alice Johnson' },
11 { id: 4, name: 'Bob Brown' },
12 { id: 5, name: 'Charlie White' },
13 ];
14 }
15
16 static async getTasks() {
17 await new Promise((resolve) => setTimeout(resolve, Math.random() * 10_000));
18 return [
19 { id: 1, title: 'Task One', completed: false },
20 { id: 2, title: 'Task Two', completed: true },
21 { id: 3, title: 'Task Three', completed: false },
22 { id: 4, title: 'Task Four', completed: true },
23 { id: 5, title: 'Task Five', completed: false },
24 ];
25 }
26
27 static streamProgressiveResponse(
28 resp: JSONLinesResponse<VovkIteration<typeof ProgressiveController.streamProgressiveResponse>>
29 ) {
30 Promise.all([
31 this.getUsers().then((users) => resp.send({ users })),
32 this.getTasks().then((tasks) => resp.send({ tasks })),
33 ])
34 .then(resp.close)
35 .catch(resp.throw);
36 }
37}
Last updated on