|
1 | 1 | import { ReactPyDjangoClient } from "./client"; |
2 | 2 | import React from "react"; |
3 | | -import { render } from "react-dom"; |
| 3 | +import ReactDOM from "react-dom"; |
4 | 4 | import { Layout } from "@reactpy/client/src/components"; |
5 | 5 |
|
6 | 6 | export function mountComponent( |
7 | | - mountElement: HTMLElement, |
8 | | - host: string, |
9 | | - urlPrefix: string, |
10 | | - componentPath: string, |
11 | | - resolvedJsModulesPath: string, |
12 | | - reconnectStartInterval: number, |
13 | | - reconnectMaxInterval: number, |
14 | | - reconnectMaxRetries: number, |
15 | | - reconnectBackoffMultiplier: number, |
| 7 | + mountElement: HTMLElement, |
| 8 | + host: string, |
| 9 | + urlPrefix: string, |
| 10 | + componentPath: string, |
| 11 | + resolvedJsModulesPath: string, |
| 12 | + reconnectStartInterval: number, |
| 13 | + reconnectMaxInterval: number, |
| 14 | + reconnectMaxRetries: number, |
| 15 | + reconnectBackoffMultiplier: number, |
16 | 16 | ) { |
17 | | - // Protocols |
18 | | - let httpProtocol = window.location.protocol; |
19 | | - let wsProtocol = `ws${httpProtocol === "https:" ? "s" : ""}:`; |
| 17 | + // Protocols |
| 18 | + let httpProtocol = window.location.protocol; |
| 19 | + let wsProtocol = `ws${httpProtocol === "https:" ? "s" : ""}:`; |
20 | 20 |
|
21 | | - // WebSocket route (for Python components) |
22 | | - let wsOrigin: string; |
23 | | - if (host) { |
24 | | - wsOrigin = `${wsProtocol}//${host}`; |
25 | | - } else { |
26 | | - wsOrigin = `${wsProtocol}//${window.location.host}`; |
27 | | - } |
28 | | - |
29 | | - // HTTP route (for JavaScript modules) |
30 | | - let httpOrigin: string; |
31 | | - let jsModulesPath: string; |
32 | | - if (host) { |
33 | | - httpOrigin = `${httpProtocol}//${host}`; |
34 | | - jsModulesPath = `${urlPrefix}/web_module`; |
35 | | - } else { |
36 | | - httpOrigin = `${httpProtocol}//${window.location.host}`; |
37 | | - if (resolvedJsModulesPath) { |
38 | | - jsModulesPath = resolvedJsModulesPath; |
| 21 | + // WebSocket route (for Python components) |
| 22 | + let wsOrigin: string; |
| 23 | + if (host) { |
| 24 | + wsOrigin = `${wsProtocol}//${host}`; |
39 | 25 | } else { |
40 | | - jsModulesPath = `${urlPrefix}/web_module`; |
| 26 | + wsOrigin = `${wsProtocol}//${window.location.host}`; |
41 | 27 | } |
42 | | - } |
43 | 28 |
|
44 | | - // Embed the initial HTTP path into the WebSocket URL |
45 | | - let componentUrl = new URL(`${wsOrigin}/${urlPrefix}/${componentPath}`); |
46 | | - componentUrl.searchParams.append("http_pathname", window.location.pathname); |
47 | | - if (window.location.search) { |
48 | | - componentUrl.searchParams.append("http_search", window.location.search); |
49 | | - } |
| 29 | + // HTTP route (for JavaScript modules) |
| 30 | + let httpOrigin: string; |
| 31 | + let jsModulesPath: string; |
| 32 | + if (host) { |
| 33 | + httpOrigin = `${httpProtocol}//${host}`; |
| 34 | + jsModulesPath = `${urlPrefix}/web_module`; |
| 35 | + } else { |
| 36 | + httpOrigin = `${httpProtocol}//${window.location.host}`; |
| 37 | + if (resolvedJsModulesPath) { |
| 38 | + jsModulesPath = resolvedJsModulesPath; |
| 39 | + } else { |
| 40 | + jsModulesPath = `${urlPrefix}/web_module`; |
| 41 | + } |
| 42 | + } |
50 | 43 |
|
51 | | - // Configure a new ReactPy client |
52 | | - const client = new ReactPyDjangoClient({ |
53 | | - urls: { |
54 | | - componentUrl: componentUrl, |
55 | | - query: document.location.search, |
56 | | - jsModules: `${httpOrigin}/${jsModulesPath}`, |
57 | | - }, |
58 | | - reconnectOptions: { |
59 | | - startInterval: reconnectStartInterval, |
60 | | - maxInterval: reconnectMaxInterval, |
61 | | - backoffMultiplier: reconnectBackoffMultiplier, |
62 | | - maxRetries: reconnectMaxRetries, |
63 | | - }, |
64 | | - mountElement: mountElement, |
65 | | - prerenderElement: document.getElementById(mountElement.id + "-prerender"), |
66 | | - offlineElement: document.getElementById(mountElement.id + "-offline"), |
67 | | - }); |
| 44 | + // Embed the initial HTTP path into the WebSocket URL |
| 45 | + let componentUrl = new URL(`${wsOrigin}/${urlPrefix}/${componentPath}`); |
| 46 | + componentUrl.searchParams.append("http_pathname", window.location.pathname); |
| 47 | + if (window.location.search) { |
| 48 | + componentUrl.searchParams.append("http_search", window.location.search); |
| 49 | + } |
68 | 50 |
|
69 | | - // Replace the prerender element with the real element on the first layout update |
70 | | - if (client.prerenderElement) { |
71 | | - client.onMessage("layout-update", ({ path, model }) => { |
72 | | - if (client.prerenderElement) { |
73 | | - client.prerenderElement.replaceWith(client.mountElement); |
74 | | - client.prerenderElement = null; |
75 | | - } |
| 51 | + // Configure a new ReactPy client |
| 52 | + const client = new ReactPyDjangoClient({ |
| 53 | + urls: { |
| 54 | + componentUrl: componentUrl, |
| 55 | + query: document.location.search, |
| 56 | + jsModules: `${httpOrigin}/${jsModulesPath}`, |
| 57 | + }, |
| 58 | + reconnectOptions: { |
| 59 | + startInterval: reconnectStartInterval, |
| 60 | + maxInterval: reconnectMaxInterval, |
| 61 | + backoffMultiplier: reconnectBackoffMultiplier, |
| 62 | + maxRetries: reconnectMaxRetries, |
| 63 | + }, |
| 64 | + mountElement: mountElement, |
| 65 | + prerenderElement: document.getElementById( |
| 66 | + mountElement.id + "-prerender", |
| 67 | + ), |
| 68 | + offlineElement: document.getElementById(mountElement.id + "-offline"), |
76 | 69 | }); |
77 | | - } |
78 | 70 |
|
79 | | - // Start rendering the component |
80 | | - render(<Layout client={client} />, client.mountElement); |
| 71 | + // Replace the prerender element with the real element on the first layout update |
| 72 | + if (client.prerenderElement) { |
| 73 | + client.onMessage("layout-update", ({ path, model }) => { |
| 74 | + if (client.prerenderElement) { |
| 75 | + client.prerenderElement.replaceWith(client.mountElement); |
| 76 | + client.prerenderElement = null; |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + // Start rendering the component |
| 82 | + ReactDOM.render(<Layout client={client} />, client.mountElement); |
81 | 83 | } |
0 commit comments