Skip to content

Commit 4a69540

Browse files
committed
fix: better code
1 parent a2c7987 commit 4a69540

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

src/services/api/streaming.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import {
88
} from '../../store/reducers/query/utils';
99
import type {Actions, TracingLevel} from '../../types/api/query';
1010
import type {QuerySyntax, StatisticsMode} from '../../types/store/query';
11-
import type {
12-
QueryResponseChunk,
13-
SessionChunk,
14-
StreamDataChunk,
15-
StreamingChunk,
16-
} from '../../types/store/streaming';
11+
import type {QueryResponseChunk, SessionChunk, StreamDataChunk} from '../../types/store/streaming';
1712
import {BINARY_DATA_IN_PLAIN_TEXT_DISPLAY} from '../../utils/constants';
1813
import {settingsManager} from '../settings';
1914

@@ -49,18 +44,6 @@ export class StreamingAPI extends BaseYdbAPI {
4944

5045
let traceId: string | undefined = '';
5146

52-
const handleChunk = (chunk: StreamingChunk) => {
53-
if (isSessionChunk(chunk)) {
54-
const sessionChunk = chunk;
55-
sessionChunk.meta.trace_id = traceId;
56-
options.onSessionChunk(chunk);
57-
} else if (isStreamDataChunk(chunk)) {
58-
options.onStreamDataChunk(chunk);
59-
} else if (isQueryResponseChunk(chunk)) {
60-
options.onQueryResponseChunk(chunk);
61-
}
62-
};
63-
6447
const queryParams = new URLSearchParams();
6548
// Add only string/number params
6649
Object.entries(params).forEach(([key, value]) => {
@@ -98,7 +81,16 @@ export class StreamingAPI extends BaseYdbAPI {
9881
await parseMultipart(response.body, {boundary: 'boundary'}, async (part) => {
9982
try {
10083
const chunk = JSON.parse(await part.text());
101-
handleChunk(chunk);
84+
85+
if (isSessionChunk(chunk)) {
86+
const sessionChunk = chunk;
87+
sessionChunk.meta.trace_id = traceId;
88+
options.onSessionChunk(chunk);
89+
} else if (isStreamDataChunk(chunk)) {
90+
options.onStreamDataChunk(chunk);
91+
} else if (isQueryResponseChunk(chunk)) {
92+
options.onQueryResponseChunk(chunk);
93+
}
10294
} catch (e) {
10395
throw new Error(`Error parsing chunk: ${e}`);
10496
}

0 commit comments

Comments
 (0)