Pārlūkot izejas kodu

Free event delegate

Tero Paananen 4 gadus atpakaļ
vecāks
revīzija
7d65185294

+ 11
- 6
windows/ReactNativeWebView/ReactWebView.cpp Parādīt failu

@@ -32,6 +32,13 @@ namespace winrt::ReactNativeWebView::implementation {
32 32
         RegisterEvents();
33 33
     }
34 34
 
35
+    ReactWebView::~ReactWebView()
36
+    {
37
+      if (m_messagingEnabled) {
38
+        m_webBridge.MessagePostedEvent(m_message_token);
39
+      }
40
+    }
41
+
35 42
     void ReactWebView::RegisterEvents() {
36 43
         m_navigationStartingRevoker = m_webView.NavigationStarting(
37 44
             winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
@@ -88,11 +95,8 @@ namespace winrt::ReactNativeWebView::implementation {
88 95
         if (m_messagingEnabled) {
89 96
           auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
90 97
           m_webBridge = WebViewBridge::WebBridge(tag);
91
-          m_webBridge.MessagePostedEvent(winrt::auto_revoke, [ref = get_weak()](const int32_t& message) {
92
-            OutputDebugStringW(L"> ReactWebView:MessagePostedEvent");
93
-            if (auto self = ref.get()) {
94
-              self->OnMessagePosted(message);
95
-            }
98
+          m_message_token = m_webBridge.MessagePostedEvent([this](const int32_t& message) {
99
+            this->OnMessagePosted(message);
96 100
           });
97 101
           webView.AddWebAllowedObject(L"__RN_WEBVIEW_JS_BRIDGE", m_webBridge);
98 102
         }
@@ -100,9 +104,10 @@ namespace winrt::ReactNativeWebView::implementation {
100 104
 
101 105
     void ReactWebView::OnMessagePosted(const int32_t& message)
102 106
     {
107
+      OutputDebugStringW(L"> ReactWebView:OnMessagePosted received \n");
108
+
103 109
       // TODO: send to RN
104 110
       // PostMessage(winrt::hstring(args.Value()));
105
-      OutputDebugStringW(L"> ReactWebView:OnMessagePosted received");
106 111
     }
107 112
 
108 113
     void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {

+ 2
- 0
windows/ReactNativeWebView/ReactWebView.h Parādīt failu

@@ -16,12 +16,14 @@ namespace winrt::ReactNativeWebView::implementation {
16 16
         ReactWebView(Microsoft::ReactNative::IReactContext const& reactContext);
17 17
         void PostMessage(winrt::hstring const& message);
18 18
         void SetMessagingEnabled(bool enabled);
19
+        ~ReactWebView();
19 20
 
20 21
     private:
21 22
         bool m_messagingEnabled{ false };
22 23
         winrt::Windows::UI::Xaml::Controls::WebView m_webView{ nullptr };
23 24
         Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
24 25
         WebViewBridge::WebBridge m_webBridge{ nullptr };
26
+        winrt::event_token m_message_token;
25 27
         winrt::Windows::UI::Xaml::Controls::WebView::NavigationStarting_revoker m_navigationStartingRevoker{};
26 28
         winrt::Windows::UI::Xaml::Controls::WebView::NavigationCompleted_revoker m_navigationCompletedRevoker{};
27 29
         winrt::Windows::UI::Xaml::Controls::WebView::NavigationFailed_revoker m_navigationFailedRevoker{};

+ 1
- 5
windows/WebViewBridge/WebBridge.cpp Parādīt failu

@@ -8,10 +8,6 @@ using namespace Platform;
8 8
 
9 9
 using namespace Windows::UI::Core;
10 10
 
11
-// https://docs.microsoft.com/en-us/windows/uwp/winrt-components/create-a-windows-runtime-component-in-cppwinrt
12
-// https://github.com/microsoft/react-native-windows/blob/0.59-legacy/current/ReactWindows/ReactNativeWebViewBridge/WebViewBridge.cs
13
-// https://github.com/MicrosoftEdge/JSBrowser/blob/master/NativeListener
14
-
15 11
 WebBridge::WebBridge(int64 tag)
16 12
 {
17 13
   m_tag = tag;
@@ -24,7 +20,7 @@ void WebBridge::PostMessage(int message) {
24 20
     CoreDispatcherPriority::Normal,
25 21
     ref new DispatchedHandler([this, message]
26 22
       {
27
-        OutputDebugStringW(L"> WebBridge sending MessagePostedEvent");
23
+        OutputDebugStringW(L"> WebBridge sending MessagePostedEvent \n");
28 24
         MessagePostedEvent(message);
29 25
       }));
30 26
 }