File tree Expand file tree Collapse file tree 5 files changed +56
-7
lines changed Expand file tree Collapse file tree 5 files changed +56
-7
lines changed Original file line number Diff line number Diff line change 22cd src/idom/client
33npm install
44npm run snowpack
5+ cd ../../../
Original file line number Diff line number Diff line change 11function serializeEvent ( event ) {
22 const data = { } ;
3- if ( event . target . hasOwnProperty ( "value" ) ) {
3+ if ( "value" in event . target ) {
44 data . value = event . target . value ;
55 }
6- if ( eventTransforms . hasOwnProperty ( event . type ) ) {
6+ if ( event . type in eventTransforms ) {
77 Object . assign ( data , eventTransforms [ event . type ] ( event ) ) ;
88 }
99 return data ;
Original file line number Diff line number Diff line change @@ -6,7 +6,10 @@ import {
66 useMemo ,
77} from "../web_modules/preact/hooks.js" ;
88import htm from "../web_modules/htm.js" ;
9+ import { Suspense } from "../web_modules/preact/compat.js" ;
10+
911import serializeEvent from "./event-to-object.js" ;
12+ import lazyComponent from "./lazy-component.js" ;
1013
1114const html = htm . bind ( h ) ;
1215
@@ -87,8 +90,8 @@ function Element({ model, sendEvent }) {
8790 if ( model . importSource ) {
8891 const lazy = lazyComponent ( model ) ;
8992 return html `
90- < Suspense fallback ="{model.importSource.fallback} ">
91- {React.createElement (lazy, attributes, children)}
93+ < Suspense fallback ="$ {model . importSource . fallback } ">
94+ ${ h ( lazy , attributes , children ) }
9295 </ Suspense >
9396 ` ;
9497 } else if ( model . children && model . children . length ) {
@@ -109,7 +112,10 @@ function elementChildren(model, sendEvent) {
109112 < ${ DynamicElement } elementId =${ child . data } sendEvent=${ sendEvent } />
110113 ` ;
111114 case "obj" :
112- return html `< ${ Element } model =${ child . data } sendEvent=${ sendEvent } /> ` ;
115+ return html `< ${ Element }
116+ model =${ child . data }
117+ sendEvent=${ sendEvent }
118+ /> ` ;
113119 case "str" :
114120 return child . data ;
115121 }
Original file line number Diff line number Diff line change 1+ import { h , Component } from "../web_modules/preact.js" ;
2+ import htm from "../web_modules/htm.js" ;
3+ import { lazy } from "../web_modules/preact/compat.js" ;
4+
5+ const html = htm . bind ( h ) ;
6+
7+ function lazyComponent ( model ) {
8+ return lazy ( ( ) => {
9+ return eval ( `import('${ model . importSource . source } ')` ) . then (
10+ ( pkg ) => {
11+ pkg = pkg . default ? pkg . default : pkg ;
12+ const cmpt = model . tagName ? getPathProperty ( pkg , model . tagName ) : pkg ;
13+ return { default : cmpt } ;
14+ } ,
15+ ( error ) => {
16+ function Catch ( ) {
17+ return html `
18+ < pre >
19+ < code > ${ error . stack } </ code >
20+ </ pre
21+ >
22+ ` ;
23+ }
24+ return { default : Catch } ;
25+ }
26+ ) ;
27+ } ) ;
28+ }
29+
30+ function getPathProperty ( obj , prop ) {
31+ // properties may be dot seperated strings
32+ const path = prop . split ( "." ) ;
33+ const firstProp = path . shift ( ) ;
34+ let value = obj [ firstProp ] ;
35+ for ( let i = 0 ; i < path . length ; i ++ ) {
36+ value = value [ path [ i ] ] ;
37+ }
38+ return value ;
39+ }
40+
41+ export default lazyComponent ;
Original file line number Diff line number Diff line change 66 "license" : " MIT" ,
77 "main" : " index.js" ,
88 "scripts" : {
9- "snowpack" : " ./node-modules /.bin/snowpack" ,
9+ "snowpack" : " ./node_modules /.bin/snowpack" ,
1010 "lint" : " ./node_modules/.bin/prettier --write ."
1111 },
1212 "devDependencies" : {
2121 "webDependencies" : [
2222 " htm" ,
2323 " preact" ,
24- " preact/hooks"
24+ " preact/hooks" ,
25+ " preact/compat"
2526 ]
2627 }
2728}
You can’t perform that action at this time.
0 commit comments