@unitedstatespowersquadrons/components 1.2.9-pre.2 → 1.2.9

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({ channel: "ExampleChannel", dispatch });
7
+ const channel = useAppCableSubscription(dispatch, "ExampleChannel");
8
8
 
9
9
  const handleDoSomething = () => {
10
10
  const newSomething = "changed";
@@ -1,8 +1,8 @@
1
- import React, { createContext, useContext, useEffect, useMemo } from "react";
1
+ import React, { createContext, useContext, useMemo } 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 { ChannelCallbacks, ChannelSubscriptionOptions, ReducerHandler } from "./types";
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,23 +61,15 @@ export default function buildReducer<AppState, AppAction>(
61
61
  return context;
62
62
  }
63
63
 
64
- function useAppCableSubscription(options: ChannelSubscriptionOptions) {
64
+ function useAppCableSubscription(dispatch: DispatchFunc<CableAction>, channel: string) {
65
65
  const appCable = useAppCable();
66
66
 
67
- useEffect(() => {
68
- const { channel, dispatch, ...rest } = options;
69
-
70
- subscription = appCable.subscriptions.create(
71
- { channel, ...rest } as ChannelSubscriptionOptions,
72
- {
73
- received: (data: object) => dispatch({ data, type: "receivedCable" }),
74
- } as ChannelCallbacks
75
- );
76
-
77
- return () => subscription.unsubscribe();
78
- }, [appCable.subscriptions, options]);
79
-
80
- return(cable!);
67
+ return(
68
+ appCable.subscriptions.create(
69
+ { channel },
70
+ { received: (data: object) => dispatch({ data, type: "receivedCable" } as CableAction) }
71
+ )
72
+ );
81
73
  }
82
74
 
83
75
  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" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unitedstatespowersquadrons/components",
3
- "version": "1.2.9-pre.2",
3
+ "version": "1.2.9",
4
4
  "description": "USPS shared React components library",
5
5
  "main": "index.tsx",
6
6
  "scripts": {