Skip to content

Commit 5239dd3

Browse files
committed
feat: init mqtt
1 parent c00ef5a commit 5239dd3

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/WorkflowManager.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
import React, { FC } from 'react';
1+
import React, { FC, useCallback, useEffect, useState } from 'react';
2+
3+
import { connect, MqttClient } from 'mqtt';
24

35
import { WorkflowManagerProps } from './types';
46

5-
export const WorkflowManager: FC<WorkflowManagerProps> = ({ children }) => {
6-
return <div>{children || `Hello WorkflowManager`}</div>;
7+
export const WorkflowManager: FC<WorkflowManagerProps> = ({
8+
brokerUrl,
9+
options,
10+
children,
11+
}) => {
12+
const [client, setClient] = useState<MqttClient | null>(null);
13+
14+
const init = useCallback(() => {
15+
if (client === null) {
16+
try {
17+
const mqttInstance = connect(brokerUrl, options);
18+
19+
setClient(mqttInstance);
20+
} catch (error) {
21+
// TODO: Handle error with invariant
22+
}
23+
}
24+
}, [brokerUrl, client, options]);
25+
26+
useEffect(() => {
27+
init();
28+
}, [init]);
29+
30+
return <div>{children ?? `Hello WorkflowManager`}</div>;
731
};

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { PropsWithChildren } from 'react';
22

3-
export interface WorkflowManagerProps extends PropsWithChildren {}
3+
import { IClientOptions } from 'mqtt';
4+
export interface WorkflowManagerProps extends PropsWithChildren {
5+
brokerUrl: string;
6+
options: IClientOptions;
7+
}

0 commit comments

Comments
 (0)