@@ -13,15 +13,6 @@ import lazyComponent from "./lazy-component.js";
1313
1414const html = htm . bind ( h ) ;
1515
16- const allUpdateTriggers = { } ;
17- const allModels = { } ;
18-
19- function updateDynamicElement ( elementId ) {
20- if ( allUpdateTriggers . hasOwnProperty ( elementId ) ) {
21- allUpdateTriggers [ elementId ] ( ) ;
22- }
23- }
24-
2516function Layout ( { endpoint } ) {
2617 // handle relative endpoint URI
2718 if ( endpoint . startsWith ( "." ) || endpoint . startsWith ( "/" ) ) {
@@ -43,18 +34,13 @@ function Layout({ endpoint }) {
4334 return new WebSocket ( endpoint ) ;
4435 } , [ endpoint ] ) ;
4536
46- const [ root , setRoot ] = useState ( null ) ;
37+ const [ modelState , setModelState ] = useState ( { root : null , models : { } } ) ;
4738
4839 socket . onmessage = ( event ) => {
4940 const msg = JSON . parse ( event . data ) ;
50- Object . assign ( allModels , msg . body . render . new ) ;
51- msg . body . render . old . forEach ( ( elementId ) => {
52- delete allModels [ elementId ] ;
53- } ) ;
54- updateDynamicElement ( msg . body . render . src ) ;
55- if ( ! root ) {
56- setRoot ( msg . body . render . root ) ;
57- }
41+ const newModels = { ...modelState . models , ...msg . body . render . new } ;
42+ msg . body . render . old . forEach ( ( elementId ) => delete newModels [ elementId ] ) ;
43+ setModelState ( { root : msg . body . render . root , models : newModels } ) ;
5844 } ;
5945
6046 const sendMsg = ( msg ) => {
@@ -67,25 +53,19 @@ function Layout({ endpoint }) {
6753 } ) ;
6854 } ;
6955
70- if ( root ) {
71- return html `< ${ DynamicElement } elementId =${ root } sendEvent=${ sendEvent } /> ` ;
56+ if ( modelState . root ) {
57+ return html `< ${ Element }
58+ modelState =${ modelState }
59+ model=${ modelState . models [ modelState . root ] }
60+ sendEvent=${ sendEvent }
61+ /> ` ;
7262 } else {
7363 return html `< div /> ` ;
7464 }
7565}
7666
77- function DynamicElement ( { elementId, sendEvent } ) {
78- allUpdateTriggers [ elementId ] = useForceUpdate ( ) ;
79- const model = allModels [ elementId ] ;
80- if ( model ) {
81- return html `< ${ Element } model =${ model } sendEvent=${ sendEvent } /> ` ;
82- } else {
83- return html `< div /> ` ;
84- }
85- }
86-
87- function Element ( { model, sendEvent } ) {
88- const children = elementChildren ( model , sendEvent ) ;
67+ function Element ( { modelState, model, sendEvent } ) {
68+ const children = elementChildren ( modelState , model , sendEvent ) ;
8969 const attributes = elementAttributes ( model , sendEvent ) ;
9070 if ( model . importSource ) {
9171 const lazy = lazyComponent ( model ) ;
@@ -95,27 +75,34 @@ function Element({ model, sendEvent }) {
9575 </ Suspense >
9676 ` ;
9777 } else if ( model . children && model . children . length ) {
98- return h ( model . tagName , attributes , children ) ;
78+ return html ` < ${ model . tagName } ... ${ attributes } > ${ children } </ /> ` ;
9979 } else {
100- return h ( model . tagName , attributes ) ;
80+ return html ` < ${ model . tagName } ... ${ attributes } /> ` ;
10181 }
10282}
10383
104- function elementChildren ( model , sendEvent ) {
84+ function elementChildren ( modelState , model , sendEvent ) {
10585 if ( ! model . children ) {
10686 return [ ] ;
10787 } else {
10888 return model . children . map ( ( child ) => {
10989 switch ( child . type ) {
11090 case "ref" :
11191 return html `
112- < ${ DynamicElement } elementId =${ child . data } sendEvent=${ sendEvent } />
92+ < ${ Element }
93+ modelState =${ modelState }
94+ model=${ modelState . models [ child . data ] }
95+ sendEvent=${ sendEvent }
96+ />
11397 ` ;
11498 case "obj" :
115- return html `< ${ Element }
116- model =${ child . data }
117- sendEvent=${ sendEvent }
118- /> ` ;
99+ return html `
100+ < ${ Element }
101+ modelState =${ modelState }
102+ model=${ child . data }
103+ sendEvent=${ sendEvent }
104+ />
105+ ` ;
119106 case "str" :
120107 return child . data ;
121108 }
@@ -157,12 +144,5 @@ function elementAttributes(model, sendEvent) {
157144
158145 return attributes ;
159146}
160- function useForceUpdate ( ) {
161- const [ , setState ] = useState ( true ) ;
162- const forceUpdate = ( ) => {
163- setState ( ( state ) => ! state ) ;
164- } ;
165- return forceUpdate ;
166- }
167147
168148export default Layout ;
0 commit comments