@@ -3,41 +3,48 @@ import { webViewRootId } from "../Internal/Environment";
33import { getStylesheets } from "./Common" ;
44import { ViewMetadata } from "./ViewMetadata" ;
55import { ViewSharedContext } from "../Public/ViewSharedContext" ;
6+ import { addView , deleteView } from "./ViewsCollection" ;
7+ import { notifyViewDestroyed , notifyViewInitialized } from "./NativeAPI" ;
8+ import { handleError } from "./ErrorHandler" ;
69
710export type ViewLifecycleEventHandler = ( view : ViewMetadata ) => void ;
811export type ViewErrorHandler = ( view : ViewMetadata , error : Error ) => void ;
912
1013export interface IViewPortalProps {
1114 view : ViewMetadata
12- viewMounted : ViewLifecycleEventHandler ;
13- viewUnmounted : ViewLifecycleEventHandler ;
14- viewErrorRaised : ViewErrorHandler ;
15+ shadowRoot : Element ;
1516}
1617
1718interface IViewPortalState {
1819 component : React . ReactElement ;
1920}
2021
22+ export function onChildViewAdded ( childView : ViewMetadata ) {
23+ addView ( childView . name , childView ) ;
24+ notifyViewInitialized ( childView . name ) ;
25+ }
26+
27+ export function onChildViewRemoved ( childView : ViewMetadata ) {
28+ deleteView ( childView . name ) ;
29+ notifyViewDestroyed ( childView . name ) ;
30+ }
31+
32+ export function onChildViewErrorRaised ( childView : ViewMetadata , error : Error ) {
33+ handleError ( error , childView ) ;
34+ }
35+
2136/**
2237 * A ViewPortal is were a view is rendered. The view DOM is then moved into the appropriate placeholder.
2338 * This way we avoid a view being recreated (and losing state) when its ViewFrame is moved in the tree.
24- *
25- * A View Frame notifies its sibling view collection when a new instance is mounted.
26- * Upon mount, a View Portal is created and it will be responsible for rendering its view component in the shadow dom.
27- * A view portal is persisted until its View Frame counterpart disappears.
28- * */
39+ */
2940export class ViewPortal extends React . Component < IViewPortalProps , IViewPortalState > {
30-
31- private head : Element ;
32- private shadowRoot : HTMLElement ;
41+ private head : HTMLElement ;
3342
3443 constructor ( props : IViewPortalProps , context : any ) {
3544 super ( props , context ) ;
3645
3746 this . state = { component : null ! } ;
3847
39- this . shadowRoot = props . view . placeholder . attachShadow ( { mode : "open" } ) . getRootNode ( ) as HTMLElement ;
40-
4148 props . view . renderHandler = component => this . renderPortal ( component ) ;
4249 }
4350
@@ -67,16 +74,16 @@ export class ViewPortal extends React.Component<IViewPortalProps, IViewPortalSta
6774 const stylesheets = getStylesheets ( document . head ) . filter ( s => s . dataset . sticky === "true" ) ;
6875 stylesheets . forEach ( s => this . head . appendChild ( document . importNode ( s , true ) ) ) ;
6976
70- this . props . viewMounted ( this . props . view ) ;
77+ onChildViewAdded ( this . props . view ) ;
7178 }
7279
7380 public componentWillUnmount ( ) {
74- this . props . viewUnmounted ( this . props . view ) ;
81+ onChildViewRemoved ( this . props . view ) ;
7582 }
7683
7784 public componentDidCatch ( error : Error , errorInfo : React . ErrorInfo ) {
7885 // execute error handling inside promise, to avoid the error handler to rethrow exception inside componentDidCatch
79- Promise . resolve ( null ) . then ( ( ) => this . props . viewErrorRaised ( this . props . view , error ) ) ;
86+ Promise . resolve ( null ) . then ( ( ) => onChildViewErrorRaised ( this . props . view , error ) ) ;
8087 }
8188
8289 public render ( ) : React . ReactNode {
@@ -90,6 +97,6 @@ export class ViewPortal extends React.Component<IViewPortalProps, IViewPortalSta
9097 </ div >
9198 </ body >
9299 </ > ,
93- this . shadowRoot ) ;
100+ this . props . shadowRoot ) ;
94101 }
95- }
102+ }
0 commit comments