Skip to content

Commit 1440f0c

Browse files
committed
add back lazy component (even though not working)
1 parent ec1d8be commit 1440f0c

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

scripts/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
cd src/idom/client
33
npm install
44
npm run snowpack
5+
cd ../../../

src/idom/client/js/event-to-object.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function 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;

src/idom/client/js/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
useMemo,
77
} from "../web_modules/preact/hooks.js";
88
import htm from "../web_modules/htm.js";
9+
import { Suspense } from "../web_modules/preact/compat.js";
10+
911
import serializeEvent from "./event-to-object.js";
12+
import lazyComponent from "./lazy-component.js";
1013

1114
const 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
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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;

src/idom/client/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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": {
@@ -21,7 +21,8 @@
2121
"webDependencies": [
2222
"htm",
2323
"preact",
24-
"preact/hooks"
24+
"preact/hooks",
25+
"preact/compat"
2526
]
2627
}
2728
}

0 commit comments

Comments
 (0)