@unitedstatespowersquadrons/components 1.2.9 → 1.2.10-pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Reducer/buildReducer.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import React, { createContext, useContext, useMemo } from "react";
|
|
1
|
+
import React, { createContext, useContext, useEffect, useMemo, useRef } from "react";
|
|
2
2
|
import { Draft, produce } from "immer";
|
|
3
3
|
import { useImmerReducer } from "use-immer";
|
|
4
|
-
import { Cable, createConsumer } from "@rails/actioncable";
|
|
4
|
+
import { Cable, Channel, createConsumer } from "@rails/actioncable";
|
|
5
5
|
import { CableAction, DispatchFunc, ReducerHandler } from "./types";
|
|
6
6
|
|
|
7
7
|
interface InitialAppProps<AppState> {
|
|
@@ -63,13 +63,16 @@ export default function buildReducer<AppState, AppAction>(
|
|
|
63
63
|
|
|
64
64
|
function useAppCableSubscription(dispatch: DispatchFunc<CableAction>, channel: string) {
|
|
65
65
|
const appCable = useAppCable();
|
|
66
|
+
const channelRef = useRef<Channel>(null);
|
|
66
67
|
|
|
67
|
-
|
|
68
|
-
appCable.subscriptions.create(
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
channelRef.current = appCable.subscriptions.create(
|
|
69
70
|
{ channel },
|
|
70
71
|
{ received: (data: object) => dispatch({ data, type: "receivedCable" } as CableAction) }
|
|
71
|
-
)
|
|
72
|
-
);
|
|
72
|
+
);
|
|
73
|
+
}, [appCable.subscriptions, channel, dispatch]);
|
|
74
|
+
|
|
75
|
+
return channelRef.current;
|
|
73
76
|
}
|
|
74
77
|
|
|
75
78
|
function useAppDispatch() {
|