Skip to content

Commit bf8be34

Browse files
committed
fix(context): Clean EventsContext
1 parent 029f939 commit bf8be34

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

packages/module/src/DataViewEventsContext/DataViewEventsContext.tsx

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,51 @@ export const EventTypes = {
1212

1313
export type DataViewEvent = typeof EventTypes[keyof typeof EventTypes];
1414

15-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
16-
type Callback = (...args: any[]) => void;
15+
type Callback = (...args: any[]) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
1716
interface Subscriptions { [id: string]: Callback }
18-
type ContextType = { [event in DataViewEvent]: Subscriptions };
17+
type EventSubscriptions = { [event in DataViewEvent]: Subscriptions };
1918
type Subscribe = (event: DataViewEvent, callback: Callback) => () => void;
19+
type Trigger = (event: DataViewEvent, ...payload: any[]) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
2020

21-
export const DataViewEventsContext = createContext<{
21+
export interface DataViewEventsContextValue {
2222
subscribe: Subscribe;
23-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24-
trigger: (event: DataViewEvent, ...payload: any[]) => void;
25-
}>({
26-
subscribe: () => () => null,
27-
trigger: () => null
28-
});
23+
trigger: Trigger;
24+
}
25+
26+
export const DataViewEventsContext = createContext<DataViewEventsContextValue>({
27+
subscribe: () => () => null,
28+
trigger: () => null
29+
});
2930

3031
export const DataViewEventsProvider = ({ children }: PropsWithChildren) => {
31-
const [ subscriptions, setSubscriptions ] = useState<ContextType>({
32+
const [ subscriptions, setSubscriptions ] = useState<EventSubscriptions>({
3233
[EventTypes.rowClick]: {}
3334
});
3435

3536
const subscribe: Subscribe = (event, callback) => {
3637
const id = crypto.randomUUID();
3738

3839
// set new subscription
39-
setSubscriptions((prevSubscriptions) => ({
40+
setSubscriptions(prevSubscriptions => ({
4041
...prevSubscriptions,
4142
[event]: { ...prevSubscriptions[event], [id]: callback }
4243
}));
4344

4445
// return unsubscribe function
4546
return () => {
46-
setSubscriptions((prevSubscriptions) => {
47+
setSubscriptions(prevSubscriptions => {
4748
const updatedSubscriptions = { ...prevSubscriptions };
4849
delete updatedSubscriptions[event][id];
4950
return updatedSubscriptions;
5051
});
5152
};
5253
};
5354

54-
const trigger = useCallback(
55-
(event: DataViewEvent, ...payload: unknown[]) => {
56-
Object.values(subscriptions[event]).forEach((callback) => {
57-
callback(...payload);
58-
});
59-
},
60-
[ subscriptions ]
61-
);
55+
const trigger = useCallback((event: DataViewEvent, ...payload: unknown[]) => {
56+
Object.values(subscriptions[event]).forEach(callback => {
57+
callback(...payload);
58+
});
59+
}, [ subscriptions ]);
6260

6361
return (
6462
<DataViewEventsContext.Provider value={{ subscribe, trigger }}>
@@ -69,4 +67,4 @@ export const DataViewEventsProvider = ({ children }: PropsWithChildren) => {
6967

7068
export const useDataViewEventsContext = () => useContext(DataViewEventsContext);
7169

72-
export default DataViewEventsContext;
70+
export default DataViewEventsContext;

0 commit comments

Comments
 (0)