forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount.tsx
More file actions
40 lines (36 loc) · 1.34 KB
/
mount.tsx
File metadata and controls
40 lines (36 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { render } from "preact";
import { ReactPyClient } from "./client";
import { Layout } from "./components";
import type { MountProps } from "./types";
export function mountReactPy(props: MountProps) {
// WebSocket route for component rendering
const wsProtocol = `ws${window.location.protocol === "https:" ? "s" : ""}:`;
const wsOrigin = `${wsProtocol}//${window.location.host}`;
const componentUrl = new URL(
`${wsOrigin}${props.pathPrefix}${props.componentPath || ""}`,
);
// Embed the initial HTTP path into the WebSocket URL
componentUrl.searchParams.append("http_pathname", window.location.pathname);
if (window.location.search) {
componentUrl.searchParams.append(
"http_query_string",
window.location.search,
);
}
// Configure a new ReactPy client
const client = new ReactPyClient({
urls: {
componentUrl: componentUrl,
jsModulesPath: `${window.location.origin}${props.pathPrefix}modules/`,
},
reconnectOptions: {
interval: props.reconnectInterval || 750,
maxInterval: props.reconnectMaxInterval || 60000,
maxRetries: props.reconnectMaxRetries || 150,
backoffMultiplier: props.reconnectBackoffMultiplier || 1.25,
},
mountElement: props.mountElement,
});
// Start rendering the component
render(<Layout client={client} />, props.mountElement);
}