Skip to content

Commit

Permalink
feat: Add global option to toggle connection
Browse files Browse the repository at this point in the history
Implements: #32
  • Loading branch information
SvenKirschbaum committed Jun 23, 2024
1 parent 48bb360 commit a177f3f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
16 changes: 16 additions & 0 deletions example/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ import {
Button,
Card,
CardContent,
Checkbox,
Container,
FormControlLabel,
Grid,
TextField,
Typography,
} from "@mui/material";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";

export function App() {
const [enabled, setEnabled] = useState(true);

return (
//Initialize Stomp connection, will use sockjs, as the protocol specified is https
//The Connection can be used by all child components via the hooks or hocs.
Expand All @@ -34,8 +38,20 @@ export function App() {
debug={(str) => {
console.log(str);
}}
//The enabled prop can be used to enable/disable the connection. Defaults to true
enabled={enabled}
>
<CssBaseline />
<FormControlLabel
className="enable-checkbox"
control={
<Checkbox
checked={enabled}
onChange={(event) => setEnabled(event.target.checked)}
/>
}
label="Enabled"
/>
<Container>
<Card style={{ margin: "3em" }}>
<CardContent>
Expand Down
6 changes: 6 additions & 0 deletions example/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}

.enable-checkbox {
position: absolute;
bottom: 0;
right: 0;
}
8 changes: 5 additions & 3 deletions src/components/StompSessionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { StompSessionSubscription } from "../interfaces/StompSessionSubscription
* Please consult the @stomp/stompjs documentation for more information.
*/
function StompSessionProvider(props: StompSessionProviderProps) {
const { url, children, ...stompOptions } = props;
const { url, children, enabled, ...stompOptions } = props;

const [client, setClient] = useState<Client | undefined>(undefined);
const subscriptionRequests = useRef(new Map());
Expand Down Expand Up @@ -68,12 +68,14 @@ function StompSessionProvider(props: StompSessionProviderProps) {
};
}

_client.activate();
if (enabled !== false) {
_client.activate();
}

return () => {
_client.deactivate();
};
}, [url, ...Object.values(stompOptions)]);
}, [url, enabled, ...Object.values(stompOptions)]);

const subscribe = (
destination: string,
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/StompSessionProviderProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { ReactNode } from "react";
export interface StompSessionProviderProps extends StompConfig {
url: string;
children: ReactNode;
enabled?: boolean;
}

0 comments on commit a177f3f

Please sign in to comment.