-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathConstructor.js
More file actions
52 lines (50 loc) · 2.02 KB
/
Copy pathConstructor.js
File metadata and controls
52 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Text } from '@asyncapi/generator-react-sdk';
import { QueryParamsVariables } from '@asyncapi/generator-components';
import { InitSignature } from './InitSignature';
import { QueryParamsArgumentsDocs } from './QueryParamsArgumentsDocs';
export function Constructor({ serverUrl, queryParams, sendOperations }) {
const sendOperationsId = sendOperations.map((operation) => operation.id());
const sendOperationsArray = JSON.stringify(sendOperationsId);
const queryParamsArray = queryParams && Array.from(queryParams.entries());
return (
<Text indent={2}>
{`/*
* Constructor to initialize the WebSocket client
* @param {string} url - The WebSocket server URL. Use it if the server URL is different from the default one taken from the AsyncAPI document.
`}
<QueryParamsArgumentsDocs queryParams={queryParamsArray} />
{` * @param {boolean} [throwSendErrors=true] - Controls how the instance send methods react to a send failure.
* When true (default) the error is re-thrown after the registered error handlers run, so the caller can
* handle each failure. When false the error is suppressed after the handlers run, which keeps a
* high-throughput producer loop going.
*/
`}
<InitSignature queryParams={queryParamsArray} />
<Text indent={2} newLines={1}>
{`this.url = url || '${serverUrl}';
`}
{queryParamsArray && queryParamsArray.length > 0 && (
<Text>
{`const params = {};
`}
<QueryParamsVariables language="javascript" queryParams={queryParamsArray} />
{`const queryString = querystring.stringify(params);
if (queryString) {
this.url += '?' + queryString;
}
`}
</Text>
)}
{`this.websocket = null;
this.messageHandlers = [];
this.errorHandlers = [];
this.outgoingProcessors = [];
this.compiledSchemas = {};
this.schemasCompiled = false;
this.sendOperationsId = ${sendOperationsArray};
this.throwSendErrors = throwSendErrors; // Re-throw send failures after handlers run`}
</Text>
{'}'}
</Text>
);
}