@@ -9,7 +9,6 @@ import React, {
99 Fragment ,
1010 ComponentType ,
1111 MutableRefObject ,
12- SyntheticEvent ,
1312 ChangeEvent ,
1413} from "react" ;
1514// @ts -ignore
@@ -20,35 +19,37 @@ import {
2019 ReactPyComponent ,
2120 createChildren ,
2221 createAttributes ,
23- ReactPyVdomImportSource ,
2422 loadImportSource ,
2523 ImportSourceBinding ,
2624} from "./reactpy-vdom" ;
2725import { ReactPyClient } from "./reactpy-client" ;
2826
29- const LayoutServer = createContext < ReactPyClient > ( null as any ) ;
27+ const ClientContext = createContext < ReactPyClient > ( null as any ) ;
3028
31- export function Layout ( props : { server : ReactPyClient } ) : JSX . Element {
29+ export function Layout ( props : { client : ReactPyClient } ) : JSX . Element {
3230 const currentModel : ReactPyVdom = useState ( { tagName : "" } ) [ 0 ] ;
3331 const forceUpdate = useForceUpdate ( ) ;
3432
3533 useEffect ( ( ) => {
36- props . server
37- . receiveMessage < LayoutUpdateMessage > ( "layout-update" )
38- . then ( ( { path, model } ) => {
34+ props . client . onMessage < LayoutUpdateMessage > (
35+ "layout-update" ,
36+ ( { path, model } ) => {
3937 if ( path === "" ) {
4038 Object . assign ( currentModel , model ) ;
4139 } else {
4240 setJsonPointer ( currentModel , path , model ) ;
4341 }
4442 forceUpdate ( ) ;
45- } ) ;
46- } , [ currentModel , props . server ] ) ;
43+ } ,
44+ ) ;
45+ props . client . start ( ) ;
46+ return ( ) => props . client . stop ( ) ;
47+ } , [ currentModel , props . client ] ) ;
4748
4849 return (
49- < LayoutServer . Provider value = { props . server } >
50+ < ClientContext . Provider value = { props . client } >
5051 < Element model = { currentModel } />
51- </ LayoutServer . Provider >
52+ </ ClientContext . Provider >
5253 ) ;
5354}
5455
@@ -75,54 +76,46 @@ export function Element({ model }: { model: ReactPyVdom }): JSX.Element | null {
7576}
7677
7778function StandardElement ( { model } : { model : ReactPyVdom } ) {
78- const server = React . useContext ( LayoutServer ) ;
79-
80- let type : string | ComponentType < any > ;
81- if ( model . tagName == "" ) {
82- type = Fragment ;
83- } else {
84- type = model . tagName ;
85- }
86-
79+ const client = React . useContext ( ClientContext ) ;
8780 // Use createElement here to avoid warning about variable numbers of children not
88- // having keys. Warning about this must now be the responsibility of the server
81+ // having keys. Warning about this must now be the responsibility of the client
8982 // providing the models instead of the client rendering them.
9083 return createElement (
91- type ,
92- createAttributes ( model , server ) ,
93- ...createChildren ( model , ( model ) => (
94- < Element model = { model } key = { model . key } />
95- ) ) ,
84+ model . tagName === "" ? Fragment : model . tagName ,
85+ createAttributes ( model , client ) ,
86+ ...createChildren ( model , ( child ) => {
87+ return < Element model = { child } key = { child . key } /> ;
88+ } ) ,
9689 ) ;
9790}
9891
9992function UserInputElement ( { model } : { model : ReactPyVdom } ) : JSX . Element {
100- const server = useContext ( LayoutServer ) ;
101- const props = createAttributes ( model , server ) ;
93+ const client = useContext ( ClientContext ) ;
94+ const props = createAttributes ( model , client ) ;
10295 const [ value , setValue ] = React . useState ( props . value ) ;
10396
104- // honor changes to value from the server via props
97+ // honor changes to value from the client via props
10598 React . useEffect ( ( ) => setValue ( props . value ) , [ props . value ] ) ;
10699
107100 const givenOnChange = props . onChange ;
108101 if ( typeof givenOnChange === "function" ) {
109102 props . onChange = ( event : ChangeEvent < any > ) => {
110103 // immediately update the value to give the user feedback
111104 setValue ( event . target . value ) ;
112- // allow the server to respond (and possibly change the value)
105+ // allow the client to respond (and possibly change the value)
113106 givenOnChange ( event ) ;
114107 } ;
115108 }
116109
117110 // Use createElement here to avoid warning about variable numbers of children not
118- // having keys. Warning about this must now be the responsibility of the server
111+ // having keys. Warning about this must now be the responsibility of the client
119112 // providing the models instead of the client rendering them.
120- return React . createElement (
113+ return createElement (
121114 model . tagName ,
122115 // overwrite
123116 { ...props , value } ,
124- ...createChildren ( model , ( model ) => (
125- < Element model = { model } key = { model . key } />
117+ ...createChildren ( model , ( child ) => (
118+ < Element model = { child } key = { child . key } />
126119 ) ) ,
127120 ) ;
128121}
@@ -184,22 +177,22 @@ function ImportedElement({ model }: { model: ReactPyVdom }) {
184177}
185178
186179function useForceUpdate ( ) {
187- const [ state , setState ] = useState ( false ) ;
188- return useCallback ( ( ) => setState ( ! state ) , [ ] ) ;
180+ const [ , setState ] = useState ( false ) ;
181+ return ( ) => setState ( ( old ) => ! old ) ;
189182}
190183
191184function useImportSource ( model : ReactPyVdom ) : MutableRefObject < any > {
192185 const vdomImportSource = model . importSource ;
193186
194187 const mountPoint = useRef < HTMLElement > ( null ) ;
195- const server = React . useContext ( LayoutServer ) ;
188+ const client = React . useContext ( ClientContext ) ;
196189 const [ binding , setBinding ] = useState < ImportSourceBinding | null > ( null ) ;
197190
198191 React . useEffect ( ( ) => {
199192 let unmounted = false ;
200193
201194 if ( vdomImportSource ) {
202- loadImportSource ( vdomImportSource , server ) . then ( ( bind ) => {
195+ loadImportSource ( vdomImportSource , client ) . then ( ( bind ) => {
203196 if ( ! unmounted && mountPoint . current ) {
204197 setBinding ( bind ( mountPoint . current ) ) ;
205198 }
@@ -216,7 +209,7 @@ function useImportSource(model: ReactPyVdom): MutableRefObject<any> {
216209 binding . unmount ( ) ;
217210 }
218211 } ;
219- } , [ server , vdomImportSource , setBinding , mountPoint . current ] ) ;
212+ } , [ client , vdomImportSource , setBinding , mountPoint . current ] ) ;
220213
221214 // this effect must run every time in case the model has changed
222215 useEffect ( ( ) => {
0 commit comments