@@ -100,6 +100,7 @@ export struct WebView {
100100 requestUrl: string = "";
101101 messagingEnabled: boolean = false;
102102 hasRegisterJavaScriptProxy: boolean = false;
103+ controllerAttached: boolean = false;
103104
104105 aboutToAppear() {
105106 this.descriptor = this.ctx.descriptorRegistry.getDescriptor<WebViewViewDescriptor>(this.tag)
@@ -113,24 +114,30 @@ export struct WebView {
113114 if (this.html != "" && this.html != this.source.html) {
114115 Logger.debug(TAG, "[RNOH] html is update")
115116 this.html = this.source.html
116- try {
117- this.controller.loadData(
118- this.source.html,
119- "text/html",
120- "UTF-8",
121- this.source.baseUrl,
122- " "
123- );
124- } catch (error) {
125- Logger.error(TAG, "error: " + error)
117+ if (this.controllerAttached) {
118+ try {
119+ this.controller.loadData(
120+ this.source.html,
121+ "text/html",
122+ "UTF-8",
123+ this.source.baseUrl,
124+ " "
125+ );
126+ } catch (error) {
127+ Logger.error(TAG, "error: " + error)
128+ }
126129 }
127130 } else if (this.source.uri != "" && this.url != this.source.uri) {
128131 Logger.debug(TAG, `[RNOH] newDescriptor props update uri: ` + this.source.uri);
129132 this.url = this.source.uri
130- this.controller.loadUrl(this.descriptor.props.newSource.uri)
133+ if (this.controllerAttached) {
134+ this.controller.loadUrl(this.descriptor.props.newSource.uri)
135+ }
131136 } else {
132137 Logger.debug(TAG, `[RNOH] newDescriptor props refresh`);
133- this.controller.refresh()
138+ if (this.controllerAttached) {
139+ this.controller.refresh()
140+ }
134141 }
135142 }
136143 )
@@ -225,14 +232,26 @@ export struct WebView {
225232 switch (command) {
226233 case "injectJavaScript":
227234 Logger.debug(TAG,`[RNOH] injectJavaScript,${JSON.stringify(args)}`)
228- this.controller.runJavaScript(args[0])
235+ if (this.controllerAttached) {
236+ try {
237+ this.controller.runJavaScript(args[0])
238+ } catch (error) {
239+ Logger.error(TAG, "error: " + error)
240+ }
241+ }
229242 break
230243 case "postMessage":
231244 Logger.debug(TAG,`[RNOH] postMessage,${JSON.stringify(args)}`)
232245 break
233246 case "reload":
234247 Logger.debug(TAG,`[RNOH] reload,${JSON.stringify(args)}`)
235- this.controller.refresh();
248+ if (this.controllerAttached) {
249+ try {
250+ this.controller.refresh();
251+ } catch (error) {
252+ Logger.error(TAG, "error: " + error)
253+ }
254+ }
236255 break
237256 default:
238257 break
@@ -297,7 +316,7 @@ export struct WebView {
297316
298317 runInjectedJavaScript() {
299318 let injectedJS = this.descriptor.props.injectedJavaScript
300- if (this.javaScriptEnable && injectedJS != "") {
319+ if (this.javaScriptEnable && injectedJS != "" && this.controllerAttached ) {
301320 try {
302321 this.controller.runJavaScript("(function() {\n" + injectedJS + ";\n})();")
303322 .then((result) => {
@@ -383,6 +402,7 @@ export struct WebView {
383402 return true
384403 })
385404 .onControllerAttached(() => {
405+ this.controllerAttached = true;
386406 Logger.debug(TAG,"[RNOH] onControllerAttached")
387407 let baseUrl = this.source.baseUrl
388408 let uri = this.source.uri
0 commit comments