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" ;
5
1
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" ;
13
3
import {
14
- HttpHeaderPropertyView ,
15
- HttpParametersPropertyView ,
16
4
HttpPathPropertyView ,
17
5
} from "./httpQueryConstants" ;
18
6
import { QueryResult } from "../queryComp" ;
19
7
import { QUERY_EXECUTION_ERROR , QUERY_EXECUTION_OK } from "constants/queryConstants" ;
20
8
import { FunctionControl } from "comps/controls/codeControl" ;
9
+ import { JSONValue } from "util/jsonTypes" ;
21
10
22
- const connect = async ( socket : WebSocket , timeout = 10000 ) => {
11
+ const socketConnection = async ( socket : WebSocket , timeout = 10000 ) => {
23
12
const isOpened = ( ) => ( socket . readyState === WebSocket . OPEN )
24
13
25
14
if ( socket . readyState !== WebSocket . CONNECTING ) {
@@ -37,6 +26,29 @@ const connect = async (socket: WebSocket, timeout = 10000) => {
37
26
}
38
27
}
39
28
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
+
40
52
const childrenMap = {
41
53
path : ParamsStringControl ,
42
54
destroySocketConnection : FunctionControl ,
@@ -58,60 +70,37 @@ export class StreamQuery extends StreamTmpQuery {
58
70
59
71
try {
60
72
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
+ }
66
79
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` ) ;
69
82
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 ) )
76
84
p ?. callback ?.( result ) ;
77
85
}
78
- } ;
86
+ }
79
87
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
+ }
87
91
88
92
this . socket . onerror = function ( error ) {
89
93
throw new Error ( error as any )
90
- } ;
94
+ }
91
95
92
- const isConnectionOpen = await connect ( this . socket ) ;
96
+ const isConnectionOpen = await socketConnection ( this . socket ) ;
93
97
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" )
100
99
}
101
100
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 ( ) ) )
108
102
} 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 || "" )
115
104
}
116
105
} ;
117
106
}
@@ -138,139 +127,3 @@ const PropertyView = (props: { comp: InstanceType<typeof StreamQuery>; datasourc
138
127
</ >
139
128
) ;
140
129
} ;
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¬ify_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