@unitedstatespowersquadrons/components 1.2.9-pre.3 → 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/Example/SubComponent.tsx +2 -2
- package/Reducer/buildReducer.tsx +13 -23
- package/Reducer/types.ts +0 -16
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@ import { useAppCableSubscription, useAppDispatch, useAppState } from "./reducer"
|
|
|
4
4
|
const SubComponent = () => {
|
|
5
5
|
const { something } = useAppState();
|
|
6
6
|
const dispatch = useAppDispatch();
|
|
7
|
-
const channel = useAppCableSubscription(
|
|
7
|
+
const channel = useAppCableSubscription(dispatch, "ExampleChannel");
|
|
8
8
|
|
|
9
9
|
const handleDoSomething = () => {
|
|
10
10
|
const newSomething = "changed";
|
|
@@ -13,7 +13,7 @@ const SubComponent = () => {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
const sendUpstreamMessage = () => {
|
|
16
|
-
channel.send({ message: "upstream" });
|
|
16
|
+
if (channel) channel.send({ message: "upstream" });
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
return(
|
package/Reducer/buildReducer.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import React, { createContext, useContext, useEffect, 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";
|
|
5
|
-
import {
|
|
4
|
+
import { Cable, Channel, createConsumer } from "@rails/actioncable";
|
|
5
|
+
import { CableAction, DispatchFunc, ReducerHandler } from "./types";
|
|
6
6
|
|
|
7
7
|
interface InitialAppProps<AppState> {
|
|
8
8
|
initialState: AppState;
|
|
@@ -11,15 +11,15 @@ interface InitialAppProps<AppState> {
|
|
|
11
11
|
|
|
12
12
|
export default function buildReducer<AppState, AppAction>(
|
|
13
13
|
reducer: (draft: Draft<AppState>, action: AppAction) => void,
|
|
14
|
-
handlers: ReducerHandler<AppAction>[] = []
|
|
15
|
-
actionCableUrl?: string
|
|
14
|
+
handlers: ReducerHandler<AppAction>[] = []
|
|
16
15
|
) {
|
|
17
16
|
const StateContext = createContext<AppState | null>(null);
|
|
18
17
|
const DispatchContext = createContext<React.Dispatch<AppAction> | null>(null);
|
|
19
18
|
const CableContext = createContext<Cable | null>(null);
|
|
20
19
|
|
|
20
|
+
const actionCableMetaTag: HTMLMetaElement | null = document.querySelector('meta[name="action-cable-url"]');
|
|
21
|
+
const actionCableUrl = actionCableMetaTag?.content;
|
|
21
22
|
const cable = actionCableUrl ? createConsumer(actionCableUrl) : null;
|
|
22
|
-
let subscription: ChannelCallbacks;
|
|
23
23
|
|
|
24
24
|
function AppProvider(props: InitialAppProps<AppState>) {
|
|
25
25
|
const [providerState, dispatch] = useImmerReducer(reducer, props.initialState, (state: AppState) => {
|
|
@@ -61,28 +61,18 @@ export default function buildReducer<AppState, AppAction>(
|
|
|
61
61
|
return context;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
function useAppCableSubscription(
|
|
64
|
+
function useAppCableSubscription(dispatch: DispatchFunc<CableAction>, channel: string) {
|
|
65
65
|
const appCable = useAppCable();
|
|
66
|
+
const channelRef = useRef<Channel>(null);
|
|
66
67
|
|
|
67
68
|
useEffect(() => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
{ channel, ...rest } as ChannelSubscriptionOptions,
|
|
72
|
-
{
|
|
73
|
-
received: (data: object) => dispatch({ data, type: "receivedCable" }),
|
|
74
|
-
} as ChannelCallbacks
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
return(
|
|
78
|
-
() => {
|
|
79
|
-
console.log({ subscription: "stop" });
|
|
80
|
-
subscription.unsubscribe();
|
|
81
|
-
}
|
|
69
|
+
channelRef.current = appCable.subscriptions.create(
|
|
70
|
+
{ channel },
|
|
71
|
+
{ received: (data: object) => dispatch({ data, type: "receivedCable" } as CableAction) }
|
|
82
72
|
);
|
|
83
|
-
}, [appCable.subscriptions,
|
|
73
|
+
}, [appCable.subscriptions, channel, dispatch]);
|
|
84
74
|
|
|
85
|
-
return
|
|
75
|
+
return channelRef.current;
|
|
86
76
|
}
|
|
87
77
|
|
|
88
78
|
function useAppDispatch() {
|
package/Reducer/types.ts
CHANGED
|
@@ -23,22 +23,6 @@ export interface GenericAppAction {
|
|
|
23
23
|
|
|
24
24
|
export type CableAction = { data: object; type: "receivedCable" };
|
|
25
25
|
|
|
26
|
-
export interface ChannelSubscriptionOptions {
|
|
27
|
-
dispatch: DispatchFunc<CableAction>;
|
|
28
|
-
channel: string;
|
|
29
|
-
[key: string]: unknown;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface ChannelCallbacks {
|
|
33
|
-
connected?: () => void;
|
|
34
|
-
disconnected?: () => void;
|
|
35
|
-
received?: () => void;
|
|
36
|
-
rejected?: () => void;
|
|
37
|
-
unsubscribe: () => void;
|
|
38
|
-
[key: string]: () => void | undefined;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
26
|
export type RailsAppAction<R extends RailsResponse = RailsResponse> =
|
|
43
27
|
| { type: "saveComplete"; res: R }
|
|
44
28
|
| { type: "saveFailed" };
|