@unitedstatespowersquadrons/components 1.2.8-pre.2 → 1.2.9-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 +10 -4
- package/Reducer/Example/appstate.tsx +1 -1
- package/Reducer/Example/reducer.tsx +9 -2
- package/Reducer/index.tsx +2 -2
- package/Reducer/types.ts +16 -0
- package/Reducer/{buildReducer.tsx → useBuildReducer.tsx} +19 -10
- package/Reducer/{buildUnreducibleState.tsx → useBuildUnreducibleState.tsx} +1 -1
- package/package.json +1 -1
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import { useAppDispatch, useAppState } from "./reducer";
|
|
2
|
+
import { useAppCableSubscription, useAppDispatch, useAppState } from "./reducer";
|
|
3
3
|
|
|
4
4
|
const SubComponent = () => {
|
|
5
5
|
const { something } = useAppState();
|
|
6
6
|
const dispatch = useAppDispatch();
|
|
7
|
+
const channel = useAppCableSubscription({ channel: "ExampleChannel", dispatch });
|
|
7
8
|
|
|
8
9
|
const handleDoSomething = () => {
|
|
9
10
|
const newSomething = "changed";
|
|
@@ -11,10 +12,15 @@ const SubComponent = () => {
|
|
|
11
12
|
dispatch({ something: newSomething, type: "doSomething" });
|
|
12
13
|
};
|
|
13
14
|
|
|
15
|
+
const sendUpstreamMessage = () => {
|
|
16
|
+
channel.send({ message: "upstream" });
|
|
17
|
+
};
|
|
18
|
+
|
|
14
19
|
return(
|
|
15
|
-
|
|
16
|
-
{something}
|
|
17
|
-
|
|
20
|
+
<>
|
|
21
|
+
<div onClick={handleDoSomething}>{something}</div>
|
|
22
|
+
<div onClick={sendUpstreamMessage}>Send Upstream</div>
|
|
23
|
+
</>
|
|
18
24
|
);
|
|
19
25
|
};
|
|
20
26
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Draft } from "immer";
|
|
2
2
|
import * as ReducerHandlers from "./reducerHandlers";
|
|
3
3
|
import { AppState, ServerResponse } from "./types";
|
|
4
|
-
import { buildReducer, RailsAppAction } from "../";
|
|
4
|
+
import { buildReducer, CableAction, RailsAppAction } from "../";
|
|
5
5
|
|
|
6
6
|
// If you want to add additional data to the server response, use type `RailsAppAction<ServerResponse>`.
|
|
7
7
|
// Otherwise, just use type `RailsAppAction`.
|
|
@@ -9,6 +9,7 @@ import { buildReducer, RailsAppAction } from "../";
|
|
|
9
9
|
// If you need to access the available `type`s for your defined AppAction, use `AppActionType<AppAction>`
|
|
10
10
|
//
|
|
11
11
|
export type AppAction =
|
|
12
|
+
| CableAction
|
|
12
13
|
| RailsAppAction<ServerResponse>
|
|
13
14
|
| { type: "doSomething"; something: string };
|
|
14
15
|
|
|
@@ -20,10 +21,16 @@ export const reducer = (draft: Draft<AppState>, action: AppAction): void => {
|
|
|
20
21
|
draft.something = action.something;
|
|
21
22
|
|
|
22
23
|
break;
|
|
24
|
+
case "receivedCable": break;
|
|
23
25
|
case "saveComplete": break;
|
|
24
26
|
case "saveFailed": break;
|
|
25
27
|
default: return type satisfies never;
|
|
26
28
|
}
|
|
27
29
|
};
|
|
28
30
|
|
|
29
|
-
export const {
|
|
31
|
+
export const {
|
|
32
|
+
AppProvider,
|
|
33
|
+
useAppCableSubscription,
|
|
34
|
+
useAppDispatch,
|
|
35
|
+
useAppState,
|
|
36
|
+
} = buildReducer(reducer, Object.values(ReducerHandlers));
|
package/Reducer/index.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { default as wrapDispatch } from "./wrapDispatch";
|
|
2
|
-
export { default as buildReducer } from "./
|
|
3
|
-
export { default as buildUnreducibleState } from "./
|
|
2
|
+
export { default as buildReducer } from "./useBuildReducer";
|
|
3
|
+
export { default as buildUnreducibleState } from "./useBuildUnreducibleState";
|
|
4
4
|
export type * from "./types";
|
package/Reducer/types.ts
CHANGED
|
@@ -23,6 +23,22 @@ 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
|
+
|
|
26
42
|
export type RailsAppAction<R extends RailsResponse = RailsResponse> =
|
|
27
43
|
| { type: "saveComplete"; res: R }
|
|
28
44
|
| { type: "saveFailed" };
|
|
@@ -1,15 +1,15 @@
|
|
|
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
4
|
import { Cable, createConsumer } from "@rails/actioncable";
|
|
5
|
-
import {
|
|
5
|
+
import { ChannelCallbacks, ChannelSubscriptionOptions, ReducerHandler } from "./types";
|
|
6
6
|
|
|
7
7
|
interface InitialAppProps<AppState> {
|
|
8
8
|
initialState: AppState;
|
|
9
9
|
children: React.ReactNode;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export default function
|
|
12
|
+
export default function useBuildReducer<AppState, AppAction>(
|
|
13
13
|
reducer: (draft: Draft<AppState>, action: AppAction) => void,
|
|
14
14
|
handlers: ReducerHandler<AppAction>[] = [],
|
|
15
15
|
actionCableUrl?: string
|
|
@@ -19,6 +19,7 @@ export default function buildReducer<AppState, AppAction>(
|
|
|
19
19
|
const CableContext = createContext<Cable | null>(null);
|
|
20
20
|
|
|
21
21
|
const cable = actionCableUrl ? createConsumer(actionCableUrl) : null;
|
|
22
|
+
const cableRef = useRef<ChannelCallbacks>(null);
|
|
22
23
|
|
|
23
24
|
function AppProvider(props: InitialAppProps<AppState>) {
|
|
24
25
|
const [providerState, dispatch] = useImmerReducer(reducer, props.initialState, (state: AppState) => {
|
|
@@ -60,15 +61,23 @@ export default function buildReducer<AppState, AppAction>(
|
|
|
60
61
|
return context;
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
function useAppCableSubscription(
|
|
64
|
+
function useAppCableSubscription(options: ChannelSubscriptionOptions) {
|
|
64
65
|
const appCable = useAppCable();
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
const { channel, dispatch, ...rest } = options;
|
|
69
|
+
|
|
70
|
+
cableRef.current = appCable.subscriptions.create(
|
|
71
|
+
{ channel, ...rest } as ChannelSubscriptionOptions,
|
|
72
|
+
{
|
|
73
|
+
received: (data: object) => dispatch({ data, type: "receivedCable" }),
|
|
74
|
+
} as ChannelCallbacks
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return () => cableRef.current!.unsubscribe();
|
|
78
|
+
}, [appCable.subscriptions, options]);
|
|
79
|
+
|
|
80
|
+
return(cable!);
|
|
72
81
|
}
|
|
73
82
|
|
|
74
83
|
function useAppDispatch() {
|
|
@@ -5,7 +5,7 @@ interface InitialAppProps<AppState> {
|
|
|
5
5
|
children: React.ReactNode;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export default function
|
|
8
|
+
export default function useBuildUnreducibleState<AppState>() {
|
|
9
9
|
const StateContext = createContext<AppState | null>(null);
|
|
10
10
|
|
|
11
11
|
const AppProvider = (props: InitialAppProps<AppState>) => {
|