Skip to content

Commit a4d439c

Browse files
committed
reconnecting WS (broken)
1 parent ef9895b commit a4d439c

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

src/django_idom/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
IDOM_BASE_URL = getattr(settings, "IDOM_BASE_URL", "_idom/")
1111
IDOM_WEBSOCKET_URL = IDOM_BASE_URL + "websocket/"
1212
IDOM_WEB_MODULES_URL = IDOM_BASE_URL + "web_module/"
13+
IDOM_WS_RECONNECT = getattr(settings, "IDOM_WS_RECONNECT", True)
1314

1415
_CACHES = getattr(settings, "CACHES", {})
1516
if _CACHES:

src/django_idom/templates/idom/component.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
mountPoint,
88
"{{ idom_websocket_url }}",
99
"{{ idom_web_modules_url }}",
10+
parseInt("{% if idom_ws_reconnect %}604800{% else %}0{% endif %}"),
1011
"{{ idom_component_id }}",
1112
"{{ idom_component_params }}"
1213
);
13-
</script>
14+
</script>

src/django_idom/templatetags/idom.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
IDOM_REGISTERED_COMPONENTS,
1111
IDOM_WEB_MODULES_URL,
1212
IDOM_WEBSOCKET_URL,
13+
IDOM_WS_RECONNECT,
1314
)
1415

1516

@@ -27,6 +28,7 @@ def idom_component(_component_id_, **kwargs):
2728
"class": class_,
2829
"idom_websocket_url": IDOM_WEBSOCKET_URL,
2930
"idom_web_modules_url": IDOM_WEB_MODULES_URL,
31+
"idom_ws_reconnect": IDOM_WS_RECONNECT,
3032
"idom_mount_uuid": uuid4().hex,
3133
"idom_component_id": _component_id_,
3234
"idom_component_params": urlencode({"kwargs": json_kwargs}),
@@ -36,12 +38,12 @@ def idom_component(_component_id_, **kwargs):
3638
def _register_component(full_component_name: str) -> None:
3739
module_name, component_name = full_component_name.rsplit(".", 1)
3840

39-
try:
40-
module = import_module(module_name)
41-
except ImportError as error:
42-
raise RuntimeError(
43-
f"Failed to import {module_name!r} while loading {component_name!r}"
44-
) from error
41+
try:
42+
module = import_module(module_name)
43+
except ImportError as error:
44+
raise RuntimeError(
45+
f"Failed to import {module_name!r} while loading {component_name!r}"
46+
) from error
4547

4648
try:
4749
component = getattr(module, component_name)

src/js/src/index.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,34 @@ import { mountLayoutWithWebSocket } from "idom-client-react";
44
let LOCATION = window.location;
55
let WS_PROTOCOL = "";
66
if (LOCATION.protocol == "https:") {
7-
WS_PROTOCOL = "wss://";
7+
WS_PROTOCOL = "wss://";
88
} else {
9-
WS_PROTOCOL = "ws://";
9+
WS_PROTOCOL = "ws://";
1010
}
1111
let WS_ENDPOINT_URL = WS_PROTOCOL + LOCATION.host + "/";
1212

1313
export function mountViewToElement(
14-
mountPoint,
15-
idomWebsocketUrl,
16-
idomWebModulesUrl,
17-
viewId,
18-
queryParams
14+
mountPoint,
15+
idomWebsocketUrl,
16+
idomWebModulesUrl,
17+
maxReconnectTimeout,
18+
viewId,
19+
queryParams
1920
) {
20-
const fullWebsocketUrl =
21-
WS_ENDPOINT_URL + idomWebsocketUrl + viewId + "/?" + queryParams;
21+
const fullWebsocketUrl =
22+
WS_ENDPOINT_URL + idomWebsocketUrl + viewId + "/?" + queryParams;
2223

23-
const fullWebModulesUrl = LOCATION.origin + "/" + idomWebModulesUrl
24-
const loadImportSource = (source, sourceType) => {
25-
return import(
26-
sourceType == "NAME" ? `${fullWebModulesUrl}${source}` : source
27-
);
28-
};
24+
const fullWebModulesUrl = LOCATION.origin + "/" + idomWebModulesUrl;
25+
const loadImportSource = (source, sourceType) => {
26+
return import(
27+
sourceType == "NAME" ? `${fullWebModulesUrl}${source}` : source
28+
);
29+
};
2930

30-
mountLayoutWithWebSocket(mountPoint, fullWebsocketUrl, loadImportSource);
31+
mountLayoutWithWebSocket(
32+
mountPoint,
33+
fullWebsocketUrl,
34+
loadImportSource,
35+
maxReconnectTimeout
36+
);
3137
}

0 commit comments

Comments
 (0)