Skip to content

Commit 2cfdf7f

Browse files
authored
Merge pull request #79 from Lemoncode/fix/avoid-unsubscribing-by-using-a-hook
Pass a callback function to the emitter
2 parents 9719b9b + 4d8bb08 commit 2cfdf7f

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

src/trackerHook.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import React from "react";
2-
import { emitter, promiseCounterUpdateEventId, getCounter} from "./trackPromise";
3-
import { defaultConfig, setupConfig } from './setupConfig';
4-
2+
import {
3+
emitter,
4+
promiseCounterUpdateEventId,
5+
getCounter
6+
} from "./trackPromise";
7+
import { defaultConfig, setupConfig } from "./setupConfig";
58

69
export const usePromiseTracker = (outerConfig = defaultConfig) => {
10+
let isMounted = React.useRef(false);
11+
12+
React.useEffect(() => {
13+
isMounted.current = true;
14+
return () => (isMounted.current = false);
15+
}, []);
16+
717
// Included in state, it will be evaluated just the first time,
818
// TODO: discuss if this is a good approach
919
// We need to apply defensive programming, ensure area and delay default to secure data
@@ -14,11 +24,16 @@ export const usePromiseTracker = (outerConfig = defaultConfig) => {
1424
// data, event emitter could have already emitted the event but subscription is not yet
1525
// setup
1626
React.useEffect(() => {
17-
if(config && config.area && getCounter(config.area) > 0) {
27+
if (
28+
isMounted.current &&
29+
config &&
30+
config.area &&
31+
getCounter(config.area) > 0
32+
) {
1833
setInternalPromiseInProgress(true);
1934
setPromiseInProgress(true);
2035
}
21-
}, [config])
36+
}, [config]);
2237

2338
// Internal will hold the current value
2439
const [
@@ -36,43 +51,37 @@ export const usePromiseTracker = (outerConfig = defaultConfig) => {
3651
internalPromiseInProgress
3752
);
3853

39-
const notifiyPromiseInProgress = () => {
40-
(!config || !config.delay || config.delay === 0) ?
41-
setPromiseInProgress(true)
42-
:
43-
setTimeout(() => {
44-
// Check here ref to internalPromiseInProgress
45-
if (latestInternalPromiseInProgress.current) {
46-
setPromiseInProgress(true);
47-
}
48-
}, config.delay);
54+
const notifyPromiseInProgress = () => {
55+
!config || !config.delay || config.delay === 0
56+
? setPromiseInProgress(true)
57+
: setTimeout(() => {
58+
// Check here ref to internalPromiseInProgress
59+
if (latestInternalPromiseInProgress.current) {
60+
setPromiseInProgress(true);
61+
}
62+
}, config.delay);
4963
};
5064

5165
const updatePromiseTrackerStatus = (anyPromiseInProgress, areaAffected) => {
52-
if (config.area === areaAffected) {
66+
if (isMounted.current && config.area === areaAffected) {
5367
setInternalPromiseInProgress(anyPromiseInProgress);
5468
// Update the ref object as well, we will check it when we need to
5569
// cover the _delay_ case (setTimeout)
5670
latestInternalPromiseInProgress.current = anyPromiseInProgress;
5771
if (!anyPromiseInProgress) {
5872
setPromiseInProgress(false);
5973
} else {
60-
notifiyPromiseInProgress();
74+
notifyPromiseInProgress();
6175
}
6276
}
6377
};
6478

6579
React.useEffect(() => {
6680
latestInternalPromiseInProgress.current = internalPromiseInProgress;
67-
emitter.on(promiseCounterUpdateEventId,
68-
(anyPromiseInProgress, areaAffected) => {
69-
updatePromiseTrackerStatus(anyPromiseInProgress, areaAffected);
70-
}
71-
);
81+
emitter.on(promiseCounterUpdateEventId, updatePromiseTrackerStatus);
7282

73-
return () => {
74-
emitter.off(promiseCounterUpdateEventId);
75-
};
83+
return () =>
84+
emitter.off(promiseCounterUpdateEventId, updatePromiseTrackerStatus);
7685
}, []);
7786

7887
return { promiseInProgress };

0 commit comments

Comments
 (0)