Skip to content

Commit 5f5e91c

Browse files
authored
Merge pull request #21 from nebarf/subscribe-fix
Subscribe fix
2 parents e6cb290 + 20836d1 commit 5f5e91c

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"warn",
4242
{
4343
"additionalHooks":
44-
"(useCompareCallback)"
44+
"(useCompareCallback, useCompareMemo, useCompareEffect, useCompareLayoutEffect)"
4545
}
4646
],
4747
"camelcase": "warn",

src/events-manager/use-bus-subscribe.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { useCompareEffect } from '../shared';
2-
import { useRef, useCallback, useContext } from 'react';
1+
import { useRef, useCallback } from 'react';
32
import fastCompare from 'react-fast-compare';
43
import { HttpEventClassType, HttpEventHandler } from './types';
5-
import { EventBusContext } from './event-bus-context';
4+
import { useEventBus } from './event-bus-context';
5+
import { useCompareLayoutEffect } from '../shared/use-compare-layout-effect';
66

77
export const useBusSubscribe = <T>(
88
eventName: HttpEventClassType<T>,
99
handler: HttpEventHandler<T>
1010
): void => {
1111
// The event bus.
12-
const eventBus = useContext(EventBusContext);
12+
const eventBus = useEventBus();
1313

1414
// A ref to unsubscribe from the event. It helps to avoid
1515
// registering the same event handler multiple times.
@@ -28,7 +28,7 @@ export const useBusSubscribe = <T>(
2828
/**
2929
* Setup the event handler.
3030
*/
31-
useCompareEffect(
31+
useCompareLayoutEffect(
3232
() => {
3333
// Subscribe to the event and keep track of the subscription.
3434
unsubscribeRef.current = eventBus.subscribe(eventName, handler);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { DependencyList, EffectCallback, useLayoutEffect } from 'react';
2+
import { DepsAreEqual } from './types';
3+
import { useCompareRef } from './use-compare-ref';
4+
5+
export function useCompareLayoutEffect(
6+
callback: EffectCallback,
7+
deps: DependencyList,
8+
compare: DepsAreEqual
9+
): void {
10+
const depsRef = useCompareRef(deps, compare);
11+
12+
// eslint-disable-next-line react-hooks/exhaustive-deps
13+
return useLayoutEffect(callback, depsRef.current);
14+
}

0 commit comments

Comments
 (0)