Skip to content

Commit 878c2ea

Browse files
committed
check for same sourceInfo in react template
This allows the template to determine whether the imported component comes from the same import source. Passing children from different import sources is difficult to handle unless treated with manual care.
1 parent c974026 commit 878c2ea

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

docs/source/javascript-components.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ adheres to the following interface:
7373
loadImportSource(source: string, sourceType: "NAME" | "URL") => Module;
7474
}
7575
76-
type ImportSource = {
76+
type SourceInfo = {
7777
source: string;
7878
sourceType: string;
7979
}
8080
81-
type bind = (node: HTMLElement, context: LayoutContext, source: ImportSource) => ({
81+
type bind = (node: HTMLElement, context: LayoutContext, source: SourceInfo) => ({
8282
render(component: any, props: Object, childModels: Array<any>): void;
8383
unmount(): void;
8484
});

src/idom/web/templates/react.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,71 @@ import {
1010
elementChildren,
1111
} from "$CDN/idom-client-react";
1212

13-
export function bind(node, config) {
13+
export function bind(node, config, sourceInfo) {
1414
return {
1515
render: (component, props, children) =>
16-
ReactDOM.render(createElement(config, component, props, children), node),
16+
ReactDOM.render(
17+
createElement(config, sourceInfo, component, props, children),
18+
node
19+
),
1720
unmount: () => ReactDOM.unmountComponentAtNode(node),
1821
};
1922
}
2023

21-
function createElement(config, component, props, children) {
24+
function createElement(config, sourceInfo, component, props, children) {
2225
return React.createElement(
2326
LayoutConfigContext.Provider,
2427
{ value: config },
25-
React.createElement(component, props, ...createChildren(children, config))
28+
React.createElement(
29+
component,
30+
props,
31+
...createChildren(config, sourceInfo, children)
32+
)
2633
);
2734
}
2835

29-
function createChildren(children, config) {
36+
function createChildren(config, sourceInfo, children) {
3037
if (!children) {
3138
return [];
3239
}
3340
return children.map((child) => {
3441
if (typeof child == "string") {
3542
return child;
3643
} else if (child.importSource) {
37-
return createElementFromThisImportSource(child, config);
44+
return createElementFromThisImportSource(config, sourceInfo, child);
3845
} else {
3946
return React.createElement(Element, { model: child });
4047
}
4148
});
4249
}
4350

44-
function createElementFromThisImportSource(model, config) {
45-
const Component = ThisImportSource[model.tagName];
46-
if (!Component) {
51+
function createElementFromThisImportSource(config, sourceInfo, model) {
52+
if (
53+
model.importSource.source != sourceInfo.source ||
54+
model.importSource.sourceType != sourceInfo.sourceType
55+
) {
4756
console.error(
4857
`Cannot create ${model.tagName} from different import source ` +
4958
`${model.importSource.source} (type: ${model.importSource.sourceType})`
5059
);
60+
return React.createElement("pre", {}, "error");
5161
}
5262
return React.createElement(
53-
Component,
63+
ThisImportSource[model.tagName],
5464
elementAttributes(model, (event) => {
55-
event.data = event.data.filter(value => {
65+
event.data = event.data.filter((value) => {
5666
try {
5767
JSON.stringify(value);
5868
} catch (err) {
59-
console.error(`Failed to serialize some event data for ${model.tagName}`)
69+
console.error(
70+
`Failed to serialize some event data for ${model.tagName}`
71+
);
6072
return false;
6173
}
6274
return true;
63-
})
75+
});
6476
config.sendEvent(event);
6577
}),
66-
...createChildren(model.children, config)
78+
...createChildren(config, sourceInfo, model.children)
6779
);
6880
}

0 commit comments

Comments
 (0)