Skip to content

Commit 79fc109

Browse files
benedikt-bartschermasenf
authored andcommitted
move state.js to jinja, related to reflex-dev#3738
1 parent 4e76e4d commit 79fc109

File tree

6 files changed

+45
-3
lines changed

6 files changed

+45
-3
lines changed

reflex/.templates/web/utils/state.js reflex/.templates/jinja/web/utils/state.js.jinja2

+2-2
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ export const useEventLoop = (
810810
const vars = {};
811811
vars[storage_to_state_map[e.key]] = e.newValue;
812812
const event = Event(
813-
`${state_name}.{{ update_vars_internal }}`,
813+
`${state_name}.{{ const.update_vars_internal }}`,
814814
{ vars: vars }
815815
);
816816
addEvents([event], e);
@@ -824,7 +824,7 @@ export const useEventLoop = (
824824
// Route after the initial page hydration.
825825
useEffect(() => {
826826
const change_start = () => {
827-
const main_state_dispatch = dispatch["reflex___state____state"];
827+
const main_state_dispatch = dispatch["{{ const.state_name }}"];
828828
if (main_state_dispatch !== undefined) {
829829
main_state_dispatch({ is_hydrated: false });
830830
}

reflex/compiler/compiler.py

+21
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ def _compile_contexts(state: Optional[Type[BaseState]], theme: Component | None)
126126
)
127127

128128

129+
def _compile_state() -> str:
130+
"""Compile the state.
131+
132+
Returns:
133+
The compiled state.
134+
"""
135+
return templates.state().render()
136+
137+
129138
def _compile_page(
130139
component: BaseComponent,
131140
state: Type[BaseState] | None,
@@ -424,6 +433,18 @@ def compile_contexts(
424433
return output_path, _compile_contexts(state, theme)
425434

426435

436+
def compile_state() -> tuple[str, str]:
437+
"""Compile the state.
438+
439+
Returns:
440+
The path and code of the compiled state.
441+
"""
442+
output_path = utils.get_state_path()
443+
444+
code = _compile_state()
445+
return output_path, code
446+
447+
427448
def compile_page(
428449
path: str, component: BaseComponent, state: Type[BaseState] | None
429450
) -> tuple[str, str]:

reflex/compiler/templates.py

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(self) -> None:
1414
from reflex.state import (
1515
FrontendEventExceptionState,
1616
OnLoadInternalState,
17+
State,
1718
UpdateVarsInternalState,
1819
)
1920

@@ -48,6 +49,7 @@ def __init__(self) -> None:
4849
"set_color_mode": constants.ColorMode.SET,
4950
"use_color_mode": constants.ColorMode.USE,
5051
"hydrate": constants.CompileVars.HYDRATE,
52+
"state_name": State.get_name(),
5153
"on_load_internal": f"{OnLoadInternalState.get_name()}.on_load_internal",
5254
"update_vars_internal": f"{UpdateVarsInternalState.get_name()}.update_vars_internal",
5355
"frontend_exception_state": FrontendEventExceptionState.get_full_name(),
@@ -111,6 +113,15 @@ def context():
111113
return get_template("web/utils/context.js.jinja2")
112114

113115

116+
def state():
117+
"""Template for the state file.
118+
119+
Returns:
120+
Template: The template for the state file.
121+
"""
122+
return get_template("web/utils/state.js.jinja2")
123+
124+
114125
def tailwind_config():
115126
"""Template for Tailwind config.
116127

reflex/compiler/utils.py

+9
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,15 @@ def get_context_path() -> str:
399399
return str(get_web_dir() / (constants.Dirs.CONTEXTS_PATH + constants.Ext.JS))
400400

401401

402+
def get_state_path() -> str:
403+
"""Get the path of the state file.
404+
405+
Returns:
406+
The path of the state module.
407+
"""
408+
return str(get_web_dir() / (constants.Dirs.STATES_PATH + constants.Ext.JS))
409+
410+
402411
def get_components_path() -> str:
403412
"""Get the path of the compiled components.
404413

reflex/constants/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Dirs(SimpleNamespace):
3535
COMPONENTS_PATH = "/".join([UTILS, "components"])
3636
# The name of the contexts file.
3737
CONTEXTS_PATH = "/".join([UTILS, "context"])
38+
# The name of the states file.
39+
STATES_PATH = "/".join([UTILS, "state"])
3840
# The name of the output static directory.
3941
STATIC = "_static"
4042
# The name of the public html directory served at "/"

reflex/constants/compiler.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Compiler variables."""
22

33
import enum
4-
import os
54
from enum import Enum
65
from types import SimpleNamespace
76

0 commit comments

Comments
 (0)