@unitedstatespowersquadrons/components 1.2.8 → 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.
@@ -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(dispatch, "ExampleChannel");
7
+ const channel = useAppCableSubscription({ channel: "ExampleChannel", dispatch });
8
8
 
9
9
  const handleDoSomething = () => {
10
10
  const newSomething = "changed";
@@ -1,4 +1,4 @@
1
1
  import { AppState } from "./types";
2
- import buildUnreducibleState from "../buildUnreducibleState";
2
+ import buildUnreducibleState from "../useBuildUnreducibleState";
3
3
 
4
4
  export const { AppProvider, useAppState } = buildUnreducibleState<AppState>();
package/Reducer/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
1
  export { default as wrapDispatch } from "./wrapDispatch";
2
- export { default as buildReducer } from "./buildReducer";
3
- export { default as buildUnreducibleState } from "./buildUnreducibleState";
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 { CableAction, DispatchFunc, ReducerHandler } from "./types";
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 buildReducer<AppState, AppAction>(
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(dispatch: DispatchFunc<CableAction>, channel: string) {
64
+ function useAppCableSubscription(options: ChannelSubscriptionOptions) {
64
65
  const appCable = useAppCable();
65
66
 
66
- return(
67
- appCable.subscriptions.create(
68
- { channel },
69
- { received: (data: object) => dispatch({ data, type: "receivedCable" } as CableAction) }
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 buildUnreducibleState<AppState>() {
8
+ export default function useBuildUnreducibleState<AppState>() {
9
9
  const StateContext = createContext<AppState | null>(null);
10
10
 
11
11
  const AppProvider = (props: InitialAppProps<AppState>) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unitedstatespowersquadrons/components",
3
- "version": "1.2.8",
3
+ "version": "1.2.9-pre.1",
4
4
  "description": "USPS shared React components library",
5
5
  "main": "index.tsx",
6
6
  "scripts": {