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";
3
3
import htm from "htm" ;
4
4
5
5
import { useJsonPatchCallback } from "./json-patch.js" ;
6
- import { loadModelImportSource } from "./import-source.js" ;
6
+ import { useImportSource } from "./import-source.js" ;
7
7
import {
8
8
createElementAttributes ,
9
9
createElementChildren ,
@@ -101,14 +101,9 @@ function ImportedElement({ model }) {
101
101
const layoutContext = React . useContext ( LayoutContext ) ;
102
102
103
103
const importSourceFallback = model . importSource . fallback ;
104
- const [ importSource , setImportSource ] = React . useState ( null ) ;
104
+ const importSource = useImportSource ( model . importSource ) ;
105
105
106
106
if ( ! importSource ) {
107
- // load the import source in the background
108
- loadModelImportSource ( layoutContext , model . importSource ) . then (
109
- setImportSource
110
- ) ;
111
-
112
107
// display a fallback if one was given
113
108
if ( ! importSourceFallback ) {
114
109
return html `< div /> ` ;
Original file line number Diff line number Diff line change
1
+ import React from "react" ;
2
+
1
3
import {
2
4
createElementAttributes ,
3
5
createElementChildren ,
4
6
} from "./element-utils.js" ;
5
7
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 ) {
7
30
return layoutContext
8
31
. loadImportSource ( importSource . source , importSource . sourceType )
9
32
. then ( ( module ) => {
You can’t perform that action at this time.
0 commit comments