Skip to content

Commit 959c3f4

Browse files
authored
Merge pull request #10 from flow-build/bugfix/remove-duplicate-set-state
Bugfix/remove duplicate set state
2 parents 5ceca85 + 10bfdac commit 959c3f4

File tree

4 files changed

+49
-399
lines changed

4 files changed

+49
-399
lines changed

app/examples/Basic/Child.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import * as React from 'react';
22

3-
import { useMqtt, useSubscribe } from '../../../dist';
3+
import { useMqtt, useSubscribe, useUnsubscribe } from '../../../dist';
44
import { processTopic, actorTopic } from './constants';
55

6+
const topics = [processTopic, actorTopic];
7+
68
export const Child: React.FC = () => {
79
const { status, error } = useMqtt();
810
const subscribe = useSubscribe();
11+
const unsubscribe = useUnsubscribe();
12+
13+
const handleSubscribe = React.useCallback(() => {
14+
if (status === 'connected') subscribe(topics);
15+
}, [status, subscribe]);
916

10-
React.useEffect(() => {
11-
if (status === 'connected') subscribe([processTopic, actorTopic]);
12-
}, [subscribe, status]);
17+
const handleUnsubscribe = React.useCallback(() => {
18+
if (status === 'connected') unsubscribe(topics);
19+
}, [status, unsubscribe]);
1320

1421
return (
1522
<main>
1623
<h1>This is a basic example</h1>
24+
<h2>Topics</h2>
25+
<p>{topics.join(', ')}</p>
26+
<button type="button" onClick={handleSubscribe}>
27+
Inscrever-se nos tópicos
28+
</button>
29+
<button type="button" onClick={handleUnsubscribe}>
30+
Desinscrever-se dos tópicos
31+
</button>
1732
<p>
1833
<b>Mqtt status: </b>
1934
{status}

app/examples/Basic/store.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import { configureStore, createSlice, Middleware } from '@reduxjs/toolkit';
1+
import { configureStore, createSlice } from '@reduxjs/toolkit';
22

3-
import { workflowManagerReducer, WorkflowManagerConfig } from '../../../dist';
4-
import { processTopic, actorTopic } from './constants';
5-
6-
const middleware: Middleware = () => (next) => (action) => {
7-
next(action);
8-
9-
if (action.type === '@@workflowManager/external/TEST_WORKFLOW') {
10-
WorkflowManagerConfig.unsubscribe([processTopic, actorTopic]);
11-
}
12-
};
3+
import { workflowManagerReducer } from '../../../dist';
134

145
const basicSlice = createSlice({
156
name: '@basic',
@@ -20,5 +11,4 @@ const basicSlice = createSlice({
2011
export const store = configureStore({
2112
reducer: { workflowManagerReducer, basic: basicSlice.reducer },
2213
devTools: process.env.NODE_ENV !== 'production',
23-
middleware: [middleware],
2414
});

0 commit comments

Comments
 (0)