-
Notifications
You must be signed in to change notification settings - Fork 1.2k
ReactNotificationService to allow communication between native modules #4953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ad9d24b
696d8c6
5f4a386
e14b449
6644c3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "ReactNotificationService to allow communications between native modules", | ||
| "packageName": "react-native-windows", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch", | ||
| "date": "2020-05-19T20:43:32.803Z" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
| #ifndef MICROSOFT_REACTNATIVE_REACTDISPATCHER | ||
| #define MICROSOFT_REACTNATIVE_REACTDISPATCHER | ||
|
|
||
| #include <winrt/Microsoft.ReactNative.h> | ||
| #include "ReactHandleHelper.h" | ||
|
|
||
| namespace winrt::Microsoft::ReactNative { | ||
|
|
||
| // Represents a dispatcher queue to invoke work item asynchronously. | ||
| // It wraps up the IReactDispatcher and adds convenience methods for | ||
| // working with C++ types. | ||
| struct ReactDispatcher { | ||
| ReactDispatcher(std::nullptr_t = nullptr) noexcept {} | ||
|
|
||
| explicit ReactDispatcher(IReactDispatcher const &handle) noexcept : m_handle{handle} {} | ||
|
|
||
| IReactDispatcher const &Handle() const noexcept { | ||
| return m_handle; | ||
| } | ||
|
|
||
| explicit operator bool() const noexcept { | ||
| return m_handle ? true : false; | ||
| } | ||
|
|
||
| void Post(ReactDispatcherCallback const &callback) const noexcept { | ||
| if (m_handle) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
(nit) can you add comment of why we would ever want this to be null? #Resolved
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class supports move semantic. It can be null if its content is moved out, or nullptr is given at construction. In reply to: 427625320 [](ancestors = 427625320) |
||
| m_handle.Post(callback); | ||
| } | ||
| } | ||
|
|
||
| bool HasThreadAccess() const noexcept { | ||
| return m_handle ? m_handle.HasThreadAccess() : false; | ||
| } | ||
|
|
||
| static ReactDispatcher CreateSerialDispatcher() noexcept { | ||
| return ReactDispatcher{ReactDispatcherHelper::CreateSerialDispatcher()}; | ||
| } | ||
|
|
||
| private: | ||
| IReactDispatcher m_handle; | ||
| }; | ||
|
|
||
| } // namespace winrt::Microsoft::ReactNative | ||
|
|
||
| #endif // MICROSOFT_REACTNATIVE_REACTDISPATCHER | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
| #ifndef MICROSOFT_REACTNATIVE_REACTHANDLEHELPER | ||
| #define MICROSOFT_REACTNATIVE_REACTHANDLEHELPER | ||
|
|
||
| // | ||
| // Helper methods for types that have Handle() method that return | ||
| // an IInspectable-inherited value. | ||
| // | ||
|
|
||
| #include <winrt/Microsoft.ReactNative.h> | ||
| #include <type_traits> | ||
|
|
||
| namespace winrt::Microsoft::ReactNative { | ||
|
|
||
| namespace Internal { | ||
| template <class T> | ||
| auto TestHandle(int) -> decltype(std::declval<T>().Handle()); | ||
| template <class> | ||
| auto TestHandle(int *) -> void; | ||
| } // namespace Internal | ||
|
|
||
| template <class T> | ||
| inline constexpr bool HasHandleV = | ||
| std::is_base_of_v<Windows::Foundation::IInspectable, std::decay_t<decltype(Internal::TestHandle<T>(0))>>; | ||
|
|
||
| // True if two types with Handle() have the same handle. | ||
| template <class T, std::enable_if_t<HasHandleV<T>, int> = 0> | ||
| inline bool operator==(T const &left, T const &right) noexcept { | ||
| return left.Handle() == right.Handle(); | ||
| } | ||
|
|
||
| // True if two types with Handle() have different handles. | ||
| template <class T, std::enable_if_t<HasHandleV<T>, int> = 0> | ||
| inline bool operator!=(T const &left, T const &right) noexcept { | ||
| return !(left.Handle() == right.Handle()); | ||
| } | ||
|
|
||
| // True if handle of left is null. | ||
| template <class T, std::enable_if_t<HasHandleV<T>, int> = 0> | ||
| inline bool operator==(T const &left, std::nullptr_t) noexcept { | ||
| return !static_cast<bool>(left.Handle()); | ||
| } | ||
|
|
||
| // True if handle of left is not null. | ||
| template <class T, std::enable_if_t<HasHandleV<T>, int> = 0> | ||
| inline bool operator!=(T const &left, std::nullptr_t) noexcept { | ||
| return static_cast<bool>(left.Handle()); | ||
| } | ||
|
|
||
| // True if handle of right is null. | ||
| template <class T, std::enable_if_t<HasHandleV<T>, int> = 0> | ||
| inline bool operator==(std::nullptr_t, T const &right) noexcept { | ||
| return !static_cast<bool>(right.Handle()); | ||
| } | ||
|
|
||
| // True if handle of left is not null. | ||
| template <class T, std::enable_if_t<HasHandleV<T>, int> = 0> | ||
| inline bool operator!=(std::nullptr_t, T const &right) noexcept { | ||
| return static_cast<bool>(right.Handle()); | ||
| } | ||
|
|
||
| } // namespace winrt::Microsoft::ReactNative | ||
|
|
||
| #endif // MICROSOFT_REACTNATIVE_REACTHANDLEHELPER |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) Since we are doing assignment anyways, wouldn't && be better here? #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is the C++/WinRT thing. It always gives us const &.
In reply to: 427624661 [](ancestors = 427624661)