Skip to content

Commit

Permalink
load import source in effect
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Feb 28, 2022
1 parent 3f8803e commit e649211
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
13 changes: 4 additions & 9 deletions docs/source/about/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@ scheme for the project adheres to `Semantic Versioning <https://semver.org/>`__.
Unreleased
----------

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

Added:

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

**Merged Pull Requests**
Fixed:

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

----

Expand Down
15 changes: 4 additions & 11 deletions src/client/packages/idom-client-react/src/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import ReactDOM from "react-dom";
import htm from "htm";

import { useJsonPatchCallback } from "./json-patch.js";
import { loadModelImportSource } from "./import-source.js";
import { useImportSource } from "./import-source.js";
import { LayoutContext } from "./contexts.js";

import {
createElementAttributes,
createElementChildren,
} from "./element-utils.js";

const html = htm.bind(React.createElement);
export const LayoutContext = React.createContext({
sendEvent: undefined,
loadImportSource: undefined,
});

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

const importSourceFallback = model.importSource.fallback;
const [importSource, setImportSource] = React.useState(null);
const importSource = useImportSource(model.importSource);

if (!importSource) {
// load the import source in the background
loadModelImportSource(layoutContext, model.importSource).then(
setImportSource
);

// display a fallback if one was given
if (!importSourceFallback) {
return html`<div />`;
Expand Down
6 changes: 6 additions & 0 deletions src/client/packages/idom-client-react/src/contexts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from "react";

export const LayoutContext = React.createContext({
sendEvent: undefined,
loadImportSource: undefined,
});
27 changes: 26 additions & 1 deletion src/client/packages/idom-client-react/src/import-source.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import React from "react";

import { LayoutContext } from "./contexts.js";

import {
createElementAttributes,
createElementChildren,
} from "./element-utils.js";

export function loadModelImportSource(layoutContext, importSource) {
export function useImportSource(modelImportSource) {
const layoutContext = React.useContext(LayoutContext);
const [importSource, setImportSource] = React.useState(null);

React.useEffect(() => {
let unmounted = false;

loadModelImportSource(layoutContext, modelImportSource).then((src) => {
if (!unmounted) {
setImportSource(src);
}
});

return () => {
unmounted = true;
};
}, [layoutContext, modelImportSource, setImportSource]);

return importSource;
}

function loadModelImportSource(layoutContext, importSource) {
return layoutContext
.loadImportSource(importSource.source, importSource.sourceType)
.then((module) => {
Expand Down
1 change: 1 addition & 0 deletions src/client/packages/idom-client-react/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./mount.js";
export * from "./contexts";
export * from "./components.js";
export * from "./server.js";

0 comments on commit e649211

Please sign in to comment.