Skip to content

Commit

Permalink
Implement responsive local state management for immediate user feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
niikkhilsharma committed Jan 28, 2024
1 parent b5040c8 commit 004c8f0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions app-examples/gallery/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.db
*.py[cod]
*.web
.web
__pycache__/
4 changes: 2 additions & 2 deletions nextpy/backend/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,8 +1473,8 @@ def _var_set_state(self, state: Type[BaseState] | str) -> Any:
state_name = state if isinstance(state, str) else state.get_full_name()
new_var_data = VarData(
state=state_name,
hooks={
"const {0} = useContext(StateContexts.{0})".format(
hooks = {
"const {0} = useContext(StateContexts.{0});\nconst dispatchers = useContext(DispatchContext)".format(
format.format_state_name(state_name)
)
},
Expand Down
2 changes: 2 additions & 0 deletions nextpy/frontend/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,8 @@ def _get_memoized_event_triggers(
if isinstance(rendered_chain, str):
rendered_chain = rendered_chain.strip("{}")

rendered_chain = rendered_chain+'}'

# Hash the rendered EventChain to get a deterministic function name.
chain_hash = md5(str(rendered_chain).encode("utf-8")).hexdigest()
memo_name = f"{event_trigger}_{chain_hash}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Purpose:
{# ==============================
LIBRARY IMPORTS BLOCK
============================== #}
import { DispatchContext } from 'utils/context'
{#
Purpose:
- Renders all required library imports for the current page or component.
Expand Down
23 changes: 22 additions & 1 deletion nextpy/utils/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,28 @@ def format_prop(

chain = ",".join([format_event(event) for event in prop.events])
event = f"addEvents([{chain}], {arg_def}, {json_dumps(prop.event_actions)})"
prop = f"{arg_def} => {event}"

if isinstance(event, str):
event = event.strip("{}")

parts = chain.split('.')
formatted_chain = f'{parts[0]} . {parts[1]}.{parts[2]}'

# Extract "_e0.target.value"
value_match = re.search(r"value:([^,)}]+)", event)
if value_match:
value = value_match.group(1)


# Extract "state.state"
message_match = re.search(r"addEvents\(\[\S+?\(\"([^.]+?\.[^.]+)", event)
if message_match:
message = message_match.group(1)

dispatcher_line = f"const dispatcher = dispatchers['{message}'];\n" \
f"dispatcher({{ message: {value} }});"

prop = f"{arg_def} =>{{ {dispatcher_line}\n{event} }}"

# Handle other types.
elif isinstance(prop, str):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nextpy"
version = "0.3.3"
version = "0.3.5"
description = "鈿he Pure Python Framework for Web Apps, Meticulously Optimized for 馃AI agents馃. World's first AMS馃"
license = "Apache-2.0"
authors = ["Team dotagent <anurag@dotagent.ai>"]
Expand Down

0 comments on commit 004c8f0

Please sign in to comment.