Skip to content

Commit 4012e00

Browse files
committed
feat: cancellation
1 parent b7c30a5 commit 4012e00

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

src/containers/MultipartTest/MultipartTest.tsx

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import React from 'react';
33
import {Button} from '@gravity-ui/uikit';
44

55
import type {MultipartChunk} from '../../store/reducers/multipart/multipart';
6-
import {useStreamMultipartQuery} from '../../store/reducers/multipart/multipart';
6+
import {useLazyStreamMultipartQuery} from '../../store/reducers/multipart/multipart';
77

88
const ANIMATION_DURATION = 300;
99

1010
export function MultipartTest() {
11-
const [startStream, setStartStream] = React.useState(false);
1211
const [receivedChunks, setReceivedChunks] = React.useState<MultipartChunk[]>([]);
1312

1413
const handleChunk = React.useCallback((chunk: MultipartChunk) => {
@@ -22,29 +21,32 @@ export function MultipartTest() {
2221
});
2322
}, []);
2423

25-
console.log('Component rendered, startStream:', startStream);
26-
27-
const {error, isLoading} = useStreamMultipartQuery(
28-
{
29-
url: '/viewer/json/query',
30-
onChunk: handleChunk,
31-
},
32-
{
33-
skip: !startStream,
34-
},
35-
);
24+
const [trigger, {error, isLoading}] = useLazyStreamMultipartQuery();
25+
const requestRef = React.useRef<ReturnType<typeof trigger> | null>(null);
3626

3727
const handleStart = React.useCallback(() => {
3828
console.log('Starting stream...');
3929
setReceivedChunks([]);
40-
setStartStream(true);
41-
}, []);
30+
requestRef.current = trigger({
31+
url: '/viewer/json/query',
32+
onChunk: handleChunk,
33+
});
34+
}, [trigger, handleChunk]);
4235

4336
const handleStop = React.useCallback(() => {
4437
console.log('Stopping stream...');
45-
setStartStream(false);
38+
requestRef.current?.abort();
39+
requestRef.current = null;
4640
}, []);
4741

42+
const handleButtonClick = React.useCallback(() => {
43+
if (isLoading) {
44+
handleStop();
45+
} else {
46+
handleStart();
47+
}
48+
}, [isLoading, handleStart, handleStop]);
49+
4850
const getErrorMessage = (err: unknown): string => {
4951
if (err instanceof Error) {
5052
return err.message;
@@ -72,13 +74,8 @@ export function MultipartTest() {
7274
</h2>
7375

7476
<div style={{marginBottom: '24px', display: 'flex', gap: '12px', alignItems: 'center'}}>
75-
<Button
76-
view="action"
77-
size="l"
78-
onClick={startStream ? handleStop : handleStart}
79-
loading={isLoading}
80-
>
81-
{startStream ? 'Stop Streaming' : 'Start Streaming'}
77+
<Button view="action" size="l" onClick={handleButtonClick} loading={isLoading}>
78+
{isLoading ? 'Stop Streaming' : 'Start Streaming'}
8279
</Button>
8380
{receivedChunks.length > 0 && (
8481
<span

src/services/api/multipart.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export class MultipartAPI extends BaseYdbAPI {
1515
getPath(): string {
1616
return 'http://localhost:3000/stream';
1717
}
18-
1918
async streamMultipartResponse<T>(
2019
url: string,
2120
onChunk: (chunk: MultipartChunk<T>) => void,

src/store/reducers/multipart/multipart.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface StreamOptions {
1515
export const multipartApi = api.injectEndpoints({
1616
endpoints: (build) => ({
1717
streamMultipart: build.query<void, StreamOptions>({
18-
queryFn: async ({url, onChunk}, _signal, _extraOptions, _baseQuery) => {
18+
queryFn: async ({url, onChunk}, {signal}, _extraOptions, _baseQuery) => {
1919
try {
2020
console.log('Starting multipart stream with onChunk:', Boolean(onChunk));
2121

@@ -28,6 +28,10 @@ export const multipartApi = api.injectEndpoints({
2828
onChunk(chunk);
2929
}
3030
},
31+
{},
32+
{
33+
signal,
34+
},
3135
);
3236

3337
return {data: undefined};
@@ -46,4 +50,4 @@ export const multipartApi = api.injectEndpoints({
4650
}),
4751
});
4852

49-
export const {useStreamMultipartQuery} = multipartApi;
53+
export const {useStreamMultipartQuery, useLazyStreamMultipartQuery} = multipartApi;

0 commit comments

Comments
 (0)