-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Create separate context providing components for each state #6181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -201,7 +201,7 @@ def app_root_template( | |
| return f""" | ||
| {imports_str} | ||
| {dynamic_imports_str} | ||
| import {{ EventLoopProvider, StateProvider, defaultColorMode }} from "$/utils/context"; | ||
| import {{ EventLoopProvider, StatesProvider, defaultColorMode }} from "$/utils/context"; | ||
| import {{ ThemeProvider }} from '$/utils/react-theme'; | ||
| import {{ Layout as AppLayout }} from './_document'; | ||
| import {{ Outlet }} from 'react-router'; | ||
|
|
@@ -226,7 +226,7 @@ def app_root_template( | |
|
|
||
| return jsx(AppLayout, {{}}, | ||
| jsx(ThemeProvider, {{defaultTheme: defaultColorMode, attribute: "class"}}, | ||
| jsx(StateProvider, {{}}, | ||
| jsx(StatesProvider, {{}}, | ||
| jsx(EventLoopProvider, {{}}, | ||
| jsx(AppWrap, {{}}, children) | ||
| ) | ||
|
|
@@ -327,22 +327,12 @@ def context_template( | |
| """ | ||
| ) | ||
|
|
||
| state_reducer_str = "\n".join( | ||
| rf'const [{format_state_name(state_name)}, dispatch_{format_state_name(state_name)}] = useReducer(applyDelta, initialState["{state_name}"])' | ||
| for state_name in initial_state | ||
| ) | ||
|
|
||
| create_state_contexts_str = "\n".join( | ||
| rf"createElement(StateContexts.{format_state_name(state_name)},{{value: {format_state_name(state_name)}}}," | ||
| rf"createElement(StateProvider, {{state_name: '{state_name}', ctx_name: '{format_state_name(state_name)}'}}, " | ||
| for state_name in initial_state | ||
| ) | ||
|
|
||
| dispatchers_str = "\n".join( | ||
| f'"{state_name}": dispatch_{format_state_name(state_name)},' | ||
| for state_name in initial_state | ||
| ) | ||
|
|
||
| return rf"""import {{ createContext, useContext, useMemo, useReducer, useState, createElement, useEffect }} from "react" | ||
| return rf"""import {{ createContext, useContext, useMemo, useReducer, useRef, useState, createElement, useEffect }} from "react" | ||
| import {{ applyDelta, ReflexEvent, hydrateClientStorage, useEventLoop, refs }} from "$/utils/state" | ||
| import {{ jsx }} from "@emotion/react"; | ||
|
|
||
|
|
@@ -402,19 +392,35 @@ def context_template( | |
| ); | ||
| }} | ||
|
|
||
| export function StateProvider({{ children }}) {{ | ||
| {state_reducer_str} | ||
| const dispatchers = useMemo(() => {{ | ||
| return {{ | ||
| {dispatchers_str} | ||
| }} | ||
| }}, []) | ||
| const DispatchProvider = ({{ children }}) => {{ | ||
| const dispatchers = useRef({{}}); | ||
| return useMemo(() => createElement(DispatchContext, {{ value: dispatchers }}, children), [children, dispatchers]); | ||
| }} | ||
|
|
||
| return ( | ||
| {create_state_contexts_str} | ||
| createElement(DispatchContext, {{value: dispatchers}}, children) | ||
| const StateProvider = ({{ children, state_name, ctx_name }}) => {{ | ||
| const dispatchers = useContext(DispatchContext); | ||
| const [state, dispatch_state] = useReducer(applyDelta, initialState[state_name]); | ||
| useEffect(() => {{ | ||
| dispatchers[state_name] = dispatch_state; | ||
| return () => delete dispatchers[state_name]; | ||
| }}, [dispatchers, dispatch_state, state_name]); | ||
| return useMemo( | ||
| () => | ||
| createElement( | ||
| StateContexts[ctx_name], | ||
| {{ value: state }}, | ||
| children, | ||
| ), | ||
| [children, state, ctx_name], | ||
| ); | ||
| }} | ||
|
Comment on lines
+400
to
+416
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dispatcher registration via useEffect creates a timing gap Each In practice, the websocket connection is async so dispatchers are likely registered by the time the first "event" message arrives. However, if the socket connects very quickly (e.g. reconnect scenario), Consider using |
||
|
|
||
| export function StatesProvider({{ children }}) {{ | ||
| return useMemo(() => ( | ||
| createElement(DispatchProvider, {{}}, | ||
| {create_state_contexts_str}children | ||
| {")" * len(initial_state)} | ||
| ) | ||
| )), [children]) | ||
| }}""" | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ref object used as plain dictionary
DispatchProviderpasses theuseRef({})return value (a ref object with shape{ current: {} }) as the context value. Then inStateProvider(line 404), dispatchers are set directly on this ref object (dispatchers[state_name] = dispatch_state) rather than ondispatchers.current. Similarly,EventLoopProviderandstate.jsaccessdispatch[substate]directly.This works because the ref object is a stable mutable JS object, but it's unconventional — the
.currentproperty initialized to{}goes unused, and properties are set directly on the ref itself. A plainuseRef(null)oruseMemo(() => ({}), [])would make the intent clearer. Alternatively, if.currentis meant to be the container, both the reads and writes should go through.current:…and update
StateProviderto match (dispatchers.current[state_name] = ...).Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!