Skip to content

Commit a2fd4b5

Browse files
refactor: stream query code
1 parent 114fb38 commit a2fd4b5

File tree

1 file changed

+44
-191
lines changed

1 file changed

+44
-191
lines changed
Lines changed: 44 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
import { ControlPropertyViewWrapper } from "components/control";
2-
import { Input } from "components/Input";
3-
import { KeyValueList } from "components/keyValueList";
4-
import { QueryConfigItemWrapper, QueryConfigLabel, QueryConfigWrapper } from "components/query";
51
import { simpleMultiComp } from "comps/generators/multi";
6-
import { ReactNode } from "react";
7-
import { JSONValue } from "../../../util/jsonTypes";
8-
import { keyValueListControl } from "../../controls/keyValueControl";
9-
import { ParamsJsonControl, ParamsStringControl } from "../../controls/paramsControl";
10-
import { list } from "../../generators/list";
11-
import { valueComp, withDefault } from "../../generators/simpleGenerators";
12-
import { FunctionProperty, toQueryView } from "../queryCompUtils";
2+
import { ParamsStringControl } from "../../controls/paramsControl";
133
import {
14-
HttpHeaderPropertyView,
15-
HttpParametersPropertyView,
164
HttpPathPropertyView,
175
} from "./httpQueryConstants";
186
import { QueryResult } from "../queryComp";
197
import { QUERY_EXECUTION_ERROR, QUERY_EXECUTION_OK } from "constants/queryConstants";
208
import { FunctionControl } from "comps/controls/codeControl";
9+
import { JSONValue } from "util/jsonTypes";
2110

22-
const connect = async (socket: WebSocket, timeout = 10000) => {
11+
const socketConnection = async (socket: WebSocket, timeout = 10000) => {
2312
const isOpened = () => (socket.readyState === WebSocket.OPEN)
2413

2514
if (socket.readyState !== WebSocket.CONNECTING) {
@@ -37,6 +26,29 @@ const connect = async (socket: WebSocket, timeout = 10000) => {
3726
}
3827
}
3928

29+
const createSuccessResponse = (
30+
data: JSONValue,
31+
runTime?: number,
32+
): QueryResult => {
33+
return {
34+
data,
35+
runTime,
36+
success: true,
37+
code: QUERY_EXECUTION_OK,
38+
}
39+
}
40+
41+
const createErrorResponse = (
42+
message: string,
43+
): QueryResult => {
44+
return {
45+
message,
46+
data: "",
47+
success: false,
48+
code: QUERY_EXECUTION_ERROR,
49+
}
50+
}
51+
4052
const childrenMap = {
4153
path: ParamsStringControl,
4254
destroySocketConnection: FunctionControl,
@@ -58,60 +70,37 @@ export class StreamQuery extends StreamTmpQuery {
5870

5971
try {
6072
const timer = performance.now();
61-
this.socket = new WebSocket(children.path.children.text.getView());
62-
63-
this.socket.onopen = function(e) {
64-
console.log("[open] Connection established");
65-
};
73+
const socketUrl = children.path.children.text.getView();
74+
75+
this.socket = new WebSocket(socketUrl);
76+
this.socket.onopen = () => {
77+
console.log("[WebSocket] Connection established");
78+
}
6679

67-
this.socket.onmessage = function(event) {
68-
console.log(`[message] Data received from server: ${event.data}`);
80+
this.socket.onmessage = (event) => {
81+
console.log(`[WebSocket] Data received from server`);
6982
if(typeof JSON.parse(event.data) === 'object') {
70-
const result = {
71-
data: JSON.parse(event.data),
72-
code: QUERY_EXECUTION_OK,
73-
success: true,
74-
runTime: Number((performance.now() - timer).toFixed()),
75-
}
83+
const result = createSuccessResponse(JSON.parse(event.data))
7684
p?.callback?.(result);
7785
}
78-
};
86+
}
7987

80-
this.socket.onclose = function(event) {
81-
if (event.wasClean) {
82-
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
83-
} else {
84-
console.log('[close] Connection died');
85-
}
86-
};
88+
this.socket.onclose = () => {
89+
console.log(`[WebSocket] Connection closed`);
90+
}
8791

8892
this.socket.onerror = function(error) {
8993
throw new Error(error as any)
90-
};
94+
}
9195

92-
const isConnectionOpen = await connect(this.socket);
96+
const isConnectionOpen = await socketConnection(this.socket);
9397
if(!isConnectionOpen) {
94-
return {
95-
success: false,
96-
data: "",
97-
code: QUERY_EXECUTION_ERROR,
98-
message: "Socket connection failed",
99-
};
98+
return createErrorResponse("Socket connection failed")
10099
}
101100

102-
return {
103-
data: "",
104-
code: QUERY_EXECUTION_OK,
105-
success: true,
106-
runTime: Number((performance.now() - timer).toFixed()),
107-
};
101+
return createSuccessResponse("", Number((performance.now() - timer).toFixed()))
108102
} catch (e) {
109-
return {
110-
success: false,
111-
data: "",
112-
code: QUERY_EXECUTION_ERROR,
113-
message: (e as any).message || "",
114-
};
103+
return createErrorResponse((e as any).message || "")
115104
}
116105
};
117106
}
@@ -138,139 +127,3 @@ const PropertyView = (props: { comp: InstanceType<typeof StreamQuery>; datasourc
138127
</>
139128
);
140129
};
141-
142-
143-
144-
// import { ParamsStringControl } from "comps/controls/paramsControl";
145-
// import { FunctionControl, StringControl, codeControl } from "comps/controls/codeControl";
146-
// import { MultiCompBuilder } from "comps/generators";
147-
// import { QueryResult } from "../queryComp";
148-
// import { QueryTutorials } from "util/tutorialUtils";
149-
// import { DocLink } from "lowcoder-design";
150-
// import { getGlobalSettings } from "comps/utils/globalSettings";
151-
// import { trans } from "i18n";
152-
// import { QUERY_EXECUTION_ERROR, QUERY_EXECUTION_OK } from "constants/queryConstants";
153-
154-
// const connect = async (socket: WebSocket, timeout = 10000) => {
155-
// const isOpened = () => (socket.readyState === WebSocket.OPEN)
156-
157-
// if (socket.readyState !== WebSocket.CONNECTING) {
158-
// return isOpened()
159-
// }
160-
// else {
161-
// const intrasleep = 100
162-
// const ttl = timeout / intrasleep // time to loop
163-
// let loop = 0
164-
// while (socket.readyState === WebSocket.CONNECTING && loop < ttl) {
165-
// await new Promise(resolve => setTimeout(resolve, intrasleep))
166-
// loop++
167-
// }
168-
// return isOpened()
169-
// }
170-
// }
171-
172-
// export const StreamQuery = (function () {
173-
// const childrenMap = {
174-
// path: StringControl,
175-
// destroySocketConnection: FunctionControl,
176-
// };
177-
// return new MultiCompBuilder(childrenMap, (props) => {
178-
// const { orgCommonSettings } = getGlobalSettings();
179-
// const runInHost = !!orgCommonSettings?.runJavaScriptInHost;
180-
181-
// console.log(props.path);
182-
// return async (
183-
// p: {
184-
// args?: Record<string, unknown>,
185-
// callback?: (result: QueryResult) => void
186-
// }
187-
// ): Promise<QueryResult> => {
188-
// console.log('Stream Query', props)
189-
190-
// try {
191-
// const timer = performance.now();
192-
// // const url = 'wss://free.blr2.piesocket.com/v3/1?api_key=yWUvGQggacrrTdXYjvTpRD5qhm4RIsglS7YJlKzp&notify_self=1'
193-
// const socket = new WebSocket(props.path);
194-
195-
// props.destroySocketConnection = () => {
196-
// socket.close();
197-
// };
198-
199-
// socket.onopen = function(e) {
200-
// console.log("[open] Connection established");
201-
// };
202-
203-
// socket.onmessage = function(event) {
204-
// console.log(`[message] Data received from server: ${event.data}`);
205-
// console.log(JSON.parse(event.data))
206-
// if(typeof JSON.parse(event.data) === 'object') {
207-
// const result = {
208-
// data: JSON.parse(event.data),
209-
// code: QUERY_EXECUTION_OK,
210-
// success: true,
211-
// runTime: Number((performance.now() - timer).toFixed()),
212-
// }
213-
// p?.callback?.(result);
214-
// }
215-
// };
216-
217-
// socket.onclose = function(event) {
218-
// if (event.wasClean) {
219-
// console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
220-
// } else {
221-
// // e.g. server process killed or network down
222-
// // event.code is usually 1006 in this case
223-
// console.log('[close] Connection died');
224-
// }
225-
// };
226-
227-
// socket.onerror = function(error) {
228-
// throw new Error(error as any)
229-
// };
230-
// const isConnectionOpen = await connect(socket);
231-
// if(!isConnectionOpen) {
232-
// return {
233-
// success: false,
234-
// data: "",
235-
// code: QUERY_EXECUTION_ERROR,
236-
// message: "Socket connection failed",
237-
// };
238-
// }
239-
240-
// // const data = await props.script(p.args, runInHost);
241-
// return {
242-
// data: "",
243-
// code: QUERY_EXECUTION_OK,
244-
// success: true,
245-
// runTime: Number((performance.now() - timer).toFixed()),
246-
// };
247-
// } catch (e) {
248-
// return {
249-
// success: false,
250-
// data: "",
251-
// code: QUERY_EXECUTION_ERROR,
252-
// message: (e as any).message || "",
253-
// };
254-
// }
255-
// };
256-
// })
257-
// .setPropertyViewFn((children) => {
258-
// return (
259-
// <>
260-
// {
261-
// children.path.propertyView({
262-
// label: "URL",
263-
// placement: "bottom",
264-
// placeholder:"wss://www.example.com/socketserver",
265-
// })
266-
// }
267-
268-
// {/* TODO: Add docs for Stream Query
269-
// {QueryTutorials.js && (
270-
// <DocLink href={QueryTutorials.js}>{trans("query.jsQueryDocLink")}</DocLink>
271-
// )} */}
272-
// </>
273-
// );
274-
// })
275-
// .build();
276-
// })();

0 commit comments

Comments
 (0)