Bläddra i källkod

Use bridge if messagingEnabled

Tero Paananen 4 år sedan
förälder
incheckning
55274858f2
2 ändrade filer med 12 tillägg och 5 borttagningar
  1. 11
    2
      windows/ReactNativeWebView/ReactWebView.cpp
  2. 1
    3
      windows/WebViewBridge/WebBridge.cpp

+ 11
- 2
windows/ReactNativeWebView/ReactWebView.cpp Visa fil

@@ -89,6 +89,7 @@ namespace winrt::ReactNativeWebView::implementation {
89 89
           auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
90 90
           m_webBridge = WebViewBridge::WebBridge(tag);
91 91
           m_webBridge.MessagePostedEvent(winrt::auto_revoke, [ref = get_weak()](const int32_t& message) {
92
+            OutputDebugStringW(L"> ReactWebView:MessagePostedEvent");
92 93
             if (auto self = ref.get()) {
93 94
               self->OnMessagePosted(message);
94 95
             }
@@ -100,6 +101,8 @@ namespace winrt::ReactNativeWebView::implementation {
100 101
     void ReactWebView::OnMessagePosted(const int32_t& message)
101 102
     {
102 103
       // TODO: send to RN
104
+      // PostMessage(winrt::hstring(args.Value()));
105
+      OutputDebugStringW(L"> ReactWebView:OnMessagePosted received");
103 106
     }
104 107
 
105 108
     void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
@@ -113,8 +116,14 @@ namespace winrt::ReactNativeWebView::implementation {
113 116
             });
114 117
 
115 118
         winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
116
-        winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
117
-        webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
119
+        if (m_messagingEnabled) {
120
+          winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {__RN_WEBVIEW_JS_BRIDGE.postMessage(String(data))}};";
121
+          webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
122
+        }
123
+        else {
124
+          winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
125
+          webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
126
+        }
118 127
     }
119 128
 
120 129
     void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {

+ 1
- 3
windows/WebViewBridge/WebBridge.cpp Visa fil

@@ -20,13 +20,11 @@ WebBridge::WebBridge(int64 tag)
20 20
 }
21 21
 
22 22
 void WebBridge::PostMessage(int message) {
23
-
24
-  
25 23
   m_dispatcher->RunAsync(
26 24
     CoreDispatcherPriority::Normal,
27 25
     ref new DispatchedHandler([this, message]
28 26
       {
27
+        OutputDebugStringW(L"> WebBridge sending MessagePostedEvent");
29 28
         MessagePostedEvent(message);
30 29
       }));
31
-  
32 30
 }