-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ReactContext.h
112 lines (87 loc) · 3.68 KB
/
ReactContext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#ifndef MICROSOFT_REACTNATIVE_REACTCONTEXT
#define MICROSOFT_REACTNATIVE_REACTCONTEXT
#include <string_view>
#include "JSValueWriter.h"
#include "winrt/Microsoft.ReactNative.h"
namespace winrt::Microsoft::ReactNative {
// Represents a context of execution for the Native Module.
// It wraps up the IReactContext and adds convenience methods for
// working with C++ types.
struct ReactContext {
ReactContext(IReactContext const &context) noexcept;
IReactContext const &ContextAbi() const noexcept;
explicit operator bool() noexcept;
template <class... TArgs>
void CallJSFunction(std::wstring_view moduleName, std::wstring_view methodName, TArgs &&... args) noexcept;
void CallJSFunction(
std::wstring_view moduleName,
std::wstring_view methodName,
JSValueArgWriter const ¶msArgWriter) noexcept;
template <class... TArgs>
void EmitJSEvent(std::wstring_view eventEmitterName, std::wstring_view eventName, TArgs &&... args) noexcept;
void EmitJSEvent(
std::wstring_view eventEmitterName,
std::wstring_view eventName,
JSValueArgWriter const ¶msArgWriter) noexcept;
template <class... TArgs>
void DispatchEvent(
winrt::Windows::UI::Xaml::FrameworkElement const &view,
std::wstring_view eventName,
TArgs &&... args) noexcept;
void DispatchEvent(
winrt::Windows::UI::Xaml::FrameworkElement const &view,
std::wstring_view eventName,
JSValueArgWriter const ¶msArgWriter) noexcept;
private:
const IReactContext m_context;
};
//==============================================================================
// ReactContext inline implementation
//==============================================================================
inline ReactContext::ReactContext(IReactContext const &context) noexcept : m_context{context} {}
inline IReactContext const &ReactContext::ContextAbi() const noexcept {
return m_context;
}
inline ReactContext::operator bool() noexcept {
return m_context != nullptr;
}
template <class... TArgs>
inline void
ReactContext::CallJSFunction(std::wstring_view moduleName, std::wstring_view methodName, TArgs &&... args) noexcept {
m_context.CallJSFunction(moduleName, methodName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
}
inline void ReactContext::CallJSFunction(
std::wstring_view moduleName,
std::wstring_view methodName,
JSValueArgWriter const ¶msArgWriter) noexcept {
m_context.CallJSFunction(moduleName, methodName, paramsArgWriter);
}
template <class... TArgs>
inline void
ReactContext::EmitJSEvent(std::wstring_view eventEmitterName, std::wstring_view eventName, TArgs &&... args) noexcept {
m_context.EmitJSEvent(eventEmitterName, eventName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
}
inline void ReactContext::EmitJSEvent(
std::wstring_view eventEmitterName,
std::wstring_view eventName,
JSValueArgWriter const ¶msArgWriter) noexcept {
m_context.EmitJSEvent(eventEmitterName, eventName, paramsArgWriter);
}
template <class... TArgs>
inline void ReactContext::DispatchEvent(
winrt::Windows::UI::Xaml::FrameworkElement const &view,
std::wstring_view eventName,
TArgs &&... args) noexcept {
m_context.DispatchEvent(view, eventName, MakeJSValueArgWriter(std::forward<TArgs>(args)...));
}
inline void ReactContext::DispatchEvent(
winrt::Windows::UI::Xaml::FrameworkElement const &view,
std::wstring_view eventName,
JSValueArgWriter const ¶msArgWriter) noexcept {
m_context.DispatchEvent(view, eventName, paramsArgWriter);
}
} // namespace winrt::Microsoft::ReactNative
#endif // MICROSOFT_REACTNATIVE_REACTCONTEXT