File tree Expand file tree Collapse file tree 2 files changed +26
-8
lines changed
src/client/packages/idom-client-react/src Expand file tree Collapse file tree 2 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import ReactDOM from "react-dom";
33import htm from "htm" ;
44
55import { useJsonPatchCallback } from "./json-patch.js" ;
6- import { loadModelImportSource } from "./import-source.js" ;
6+ import { useImportSource } from "./import-source.js" ;
77import {
88 createElementAttributes ,
99 createElementChildren ,
@@ -101,14 +101,9 @@ function ImportedElement({ model }) {
101101 const layoutContext = React . useContext ( LayoutContext ) ;
102102
103103 const importSourceFallback = model . importSource . fallback ;
104- const [ importSource , setImportSource ] = React . useState ( null ) ;
104+ const importSource = useImportSource ( model . importSource ) ;
105105
106106 if ( ! importSource ) {
107- // load the import source in the background
108- loadModelImportSource ( layoutContext , model . importSource ) . then (
109- setImportSource
110- ) ;
111-
112107 // display a fallback if one was given
113108 if ( ! importSourceFallback ) {
114109 return html `< div /> ` ;
Original file line number Diff line number Diff line change 1+ import React from "react" ;
2+
13import {
24 createElementAttributes ,
35 createElementChildren ,
46} from "./element-utils.js" ;
57
6- export function loadModelImportSource ( layoutContext , importSource ) {
8+ export function useImportSource ( importSource ) {
9+ const layoutContext = React . useContext ( LayoutContext ) ;
10+ const [ importSource , setImportSource ] = React . useState ( null ) ;
11+
12+ useEffect ( ( ) => {
13+ let unmounted = false ;
14+
15+ loadModelImportSource ( layoutContext , model . importSource ) . then ( ( src ) => {
16+ if ( ! unmounted ) {
17+ setImportSource ( src ) ;
18+ }
19+ } ) ;
20+
21+ return ( ) => {
22+ unmounted = true ;
23+ } ;
24+ } , [ layoutContext , model . importSource , setImportSource ] ) ;
25+
26+ return importSource ;
27+ }
28+
29+ function loadModelImportSource ( layoutContext , importSource ) {
730 return layoutContext
831 . loadImportSource ( importSource . source , importSource . sourceType )
932 . then ( ( module ) => {
You can’t perform that action at this time.
0 commit comments