MCPcopy
hub / github.com/reflex-dev/reflex / _patch_state

Function _patch_state

reflex/istate/shared.py:77–113  ·  view source on GitHub ↗

Patch the linked state into the original state's tree, restoring it afterward. Args: original_state: The original shared state. linked_state: The linked shared state. full_delta: If True, mark all Vars in linked_state dirty and resolve the delta from the root

(
    original_state: BaseState, linked_state: BaseState, full_delta: bool = False
)

Source from the content-addressed store, hash-verified

75
76@contextlib.asynccontextmanager
77async def _patch_state(
78 original_state: BaseState, linked_state: BaseState, full_delta: bool = False
79):
80 """Patch the linked state into the original state's tree, restoring it afterward.
81
82 Args:
83 original_state: The original shared state.
84 linked_state: The linked shared state.
85 full_delta: If True, mark all Vars in linked_state dirty and resolve
86 the delta from the root. This option is used when linking or unlinking
87 to ensure that other computed vars in the tree pick up the newly
88 linked/unlinked values.
89 """
90 if (original_parent_state := original_state.parent_state) is None:
91 msg = "Cannot patch root state as linked state."
92 raise ReflexRuntimeError(msg)
93
94 state_name = original_state.get_name()
95 original_parent_state.substates[state_name] = linked_state
96 linked_parent_state = linked_state.parent_state
97 linked_state.parent_state = original_parent_state
98 try:
99 if full_delta:
100 linked_state.dirty_vars.update(linked_state.base_vars)
101 linked_state.dirty_vars.update(linked_state._backend_vars)
102 linked_state.dirty_vars.update(linked_state.computed_vars)
103 linked_state._mark_dirty()
104 # Apply the updates into the existing state tree for rehydrate.
105 root_state = original_state._get_root_state()
106 root_state.dirty_vars.add("router")
107 root_state.dirty_vars.add(ROUTER_DATA)
108 root_state._mark_dirty()
109 await root_state._get_resolved_delta()
110 yield
111 finally:
112 original_parent_state.substates[state_name] = original_state
113 linked_state.parent_state = linked_parent_state
114
115
116class SharedStateBaseInternal(State):

Callers 2

_unlinkMethod · 0.85

Calls 6

ReflexRuntimeErrorClass · 0.90
get_nameMethod · 0.80
_get_root_stateMethod · 0.80
_get_resolved_deltaMethod · 0.80
updateMethod · 0.45
_mark_dirtyMethod · 0.45

Tested by

no test coverage detected