Skip to content

Commit 5e0c004

Browse files
committed
refactor(use-http-event): remove useless unsubscribe ref
1 parent 65a6f62 commit 5e0c004

File tree

4 files changed

+7
-30
lines changed

4 files changed

+7
-30
lines changed

.eslintrc.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@
3636
"react/jsx-uses-react": "error",
3737
"react/jsx-uses-vars": "error",
3838
"react-hooks/rules-of-hooks": "error", // Check hooks rules
39-
// "react-hooks/exhaustive-deps": "warn", // Check hooks deps
4039
"react-hooks/exhaustive-deps": [
41-
"warn",
40+
"error",
4241
{
4342
"additionalHooks":
44-
"(useCompareCallback, useCompareMemo, useCompareEffect, useCompareLayoutEffect)"
43+
"(useCompareCallback|useCompareMemo|useCompareEffect|useCompareLayoutEffect)"
4544
}
4645
],
4746
"camelcase": "warn",

src/client/use-http-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const useHttpClient = (): UseHttpClientReturn => {
8080
*/
8181
const cachedResponse = cache.get<HttpResponseT, HttpRequestBodyT>(requestInfo);
8282
if (cachedResponse) {
83-
return cachedResponse;
83+
return Promise.resolve(cachedResponse);
8484
}
8585

8686
try {

src/events-manager/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export enum HttpEventIdentifier {
88

99
export type HttpEventHandler<PayloadT> = (payload: PayloadT) => void;
1010

11-
export type HttpEventClassType<PayloadT> = new (...args: unknown[]) => HttpEvent<PayloadT>;
11+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
12+
export type HttpEventClassType<PayloadT> = new (...args: any[]) => HttpEvent<PayloadT>;

src/events-manager/use-http-event.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useRef, useCallback } from 'react';
21
import fastCompare from 'react-fast-compare';
32
import { HttpEventClassType, HttpEventHandler } from './types';
43
import { useEventBus } from './event-bus-context';
@@ -11,34 +10,12 @@ export const useHttpEvent = <PayloadT>(
1110
// The event bus.
1211
const eventBus = useEventBus();
1312

14-
// A ref to unsubscribe from the event. It helps to avoid
15-
// registering the same event handler multiple times.
16-
const unsubscribeRef = useRef<() => void>();
17-
18-
/**
19-
* Detach the handler for the event.
20-
*/
21-
const unsubscribe = useCallback(() => {
22-
if (unsubscribeRef.current) {
23-
unsubscribeRef.current();
24-
unsubscribeRef.current = undefined;
25-
}
26-
}, []);
27-
2813
/**
2914
* Setup the event handler.
3015
*/
3116
useCompareLayoutEffect(
32-
() => {
33-
// Subscribe to the event and keep track of the subscription.
34-
unsubscribeRef.current = eventBus.subscribe(eventType, handler);
35-
36-
// Clean up: unsubscribe the previous event handler.
37-
return () => {
38-
unsubscribe();
39-
};
40-
},
41-
[eventBus, eventType, handler, unsubscribe],
17+
() => eventBus.subscribe(eventType, handler),
18+
[eventBus, eventType, handler],
4219
fastCompare
4320
);
4421
};

0 commit comments

Comments
 (0)