Procházet zdrojové kódy

Free event delegate

Tero Paananen před 4 roky
rodič
revize
7d65185294

+ 11
- 6
windows/ReactNativeWebView/ReactWebView.cpp Zobrazit soubor

32
         RegisterEvents();
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
     void ReactWebView::RegisterEvents() {
42
     void ReactWebView::RegisterEvents() {
36
         m_navigationStartingRevoker = m_webView.NavigationStarting(
43
         m_navigationStartingRevoker = m_webView.NavigationStarting(
37
             winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
44
             winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
88
         if (m_messagingEnabled) {
95
         if (m_messagingEnabled) {
89
           auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
96
           auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
90
           m_webBridge = WebViewBridge::WebBridge(tag);
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
           webView.AddWebAllowedObject(L"__RN_WEBVIEW_JS_BRIDGE", m_webBridge);
101
           webView.AddWebAllowedObject(L"__RN_WEBVIEW_JS_BRIDGE", m_webBridge);
98
         }
102
         }
100
 
104
 
101
     void ReactWebView::OnMessagePosted(const int32_t& message)
105
     void ReactWebView::OnMessagePosted(const int32_t& message)
102
     {
106
     {
107
+      OutputDebugStringW(L"> ReactWebView:OnMessagePosted received \n");
108
+
103
       // TODO: send to RN
109
       // TODO: send to RN
104
       // PostMessage(winrt::hstring(args.Value()));
110
       // PostMessage(winrt::hstring(args.Value()));
105
-      OutputDebugStringW(L"> ReactWebView:OnMessagePosted received");
106
     }
111
     }
107
 
112
 
108
     void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
113
     void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {

+ 2
- 0
windows/ReactNativeWebView/ReactWebView.h Zobrazit soubor

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

+ 1
- 5
windows/WebViewBridge/WebBridge.cpp Zobrazit soubor

8
 
8
 
9
 using namespace Windows::UI::Core;
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
 WebBridge::WebBridge(int64 tag)
11
 WebBridge::WebBridge(int64 tag)
16
 {
12
 {
17
   m_tag = tag;
13
   m_tag = tag;
24
     CoreDispatcherPriority::Normal,
20
     CoreDispatcherPriority::Normal,
25
     ref new DispatchedHandler([this, message]
21
     ref new DispatchedHandler([this, message]
26
       {
22
       {
27
-        OutputDebugStringW(L"> WebBridge sending MessagePostedEvent");
23
+        OutputDebugStringW(L"> WebBridge sending MessagePostedEvent \n");
28
         MessagePostedEvent(message);
24
         MessagePostedEvent(message);
29
       }));
25
       }));
30
 }
26
 }