Skip to content

Commit e649211

Browse files
committed
load import source in effect
1 parent 3f8803e commit e649211

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

docs/source/about/changelog.rst

+4-9
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@ scheme for the project adheres to `Semantic Versioning <https://semver.org/>`__.
1616
Unreleased
1717
----------
1818

19-
While this release doesn't warrant a minor version bump, the last release should have
20-
been. Thus, to make up for that, this release will be a minor bump. It includes misc
21-
bug fixes, and several feature adds, the most important of which is the addition of the
22-
``use_context`` hook.
23-
2419
Added:
2520

2621
- Support for keys in HTML fragments - :issue:`682`
22+
- Use Context Hook - :pull:`585`
2723

28-
**Merged Pull Requests**
24+
Fixed:
2925

30-
- reset schedule_render_later flag after triggering - :pull:`688`
31-
- support keys in HTML fragments - :pull:`683`
32-
- Add Use Context Hook - :pull:`585`
26+
- React warning about set state in unmounted component - :issue:`690`
27+
- Missing reset of schedule_render_later flag - :pull:`688`
3328

3429
----
3530

src/client/packages/idom-client-react/src/components.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import ReactDOM from "react-dom";
33
import htm from "htm";
44

55
import { useJsonPatchCallback } from "./json-patch.js";
6-
import { loadModelImportSource } from "./import-source.js";
6+
import { useImportSource } from "./import-source.js";
7+
import { LayoutContext } from "./contexts.js";
8+
79
import {
810
createElementAttributes,
911
createElementChildren,
1012
} from "./element-utils.js";
1113

1214
const html = htm.bind(React.createElement);
13-
export const LayoutContext = React.createContext({
14-
sendEvent: undefined,
15-
loadImportSource: undefined,
16-
});
1715

1816
export function Layout({ saveUpdateHook, sendEvent, loadImportSource }) {
1917
const [model, patchModel] = useJsonPatchCallback({});
@@ -101,14 +99,9 @@ function ImportedElement({ model }) {
10199
const layoutContext = React.useContext(LayoutContext);
102100

103101
const importSourceFallback = model.importSource.fallback;
104-
const [importSource, setImportSource] = React.useState(null);
102+
const importSource = useImportSource(model.importSource);
105103

106104
if (!importSource) {
107-
// load the import source in the background
108-
loadModelImportSource(layoutContext, model.importSource).then(
109-
setImportSource
110-
);
111-
112105
// display a fallback if one was given
113106
if (!importSourceFallback) {
114107
return html`<div />`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from "react";
2+
3+
export const LayoutContext = React.createContext({
4+
sendEvent: undefined,
5+
loadImportSource: undefined,
6+
});

src/client/packages/idom-client-react/src/import-source.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1+
import React from "react";
2+
3+
import { LayoutContext } from "./contexts.js";
4+
15
import {
26
createElementAttributes,
37
createElementChildren,
48
} from "./element-utils.js";
59

6-
export function loadModelImportSource(layoutContext, importSource) {
10+
export function useImportSource(modelImportSource) {
11+
const layoutContext = React.useContext(LayoutContext);
12+
const [importSource, setImportSource] = React.useState(null);
13+
14+
React.useEffect(() => {
15+
let unmounted = false;
16+
17+
loadModelImportSource(layoutContext, modelImportSource).then((src) => {
18+
if (!unmounted) {
19+
setImportSource(src);
20+
}
21+
});
22+
23+
return () => {
24+
unmounted = true;
25+
};
26+
}, [layoutContext, modelImportSource, setImportSource]);
27+
28+
return importSource;
29+
}
30+
31+
function loadModelImportSource(layoutContext, importSource) {
732
return layoutContext
833
.loadImportSource(importSource.source, importSource.sourceType)
934
.then((module) => {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./mount.js";
2+
export * from "./contexts";
23
export * from "./components.js";
34
export * from "./server.js";

0 commit comments

Comments
 (0)