-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWinRTTurboModule.cpp
More file actions
82 lines (66 loc) · 2.33 KB
/
WinRTTurboModule.cpp
File metadata and controls
82 lines (66 loc) · 2.33 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "WinRTTurboModule.h"
#include <rnwinrt/base.cpp>
using namespace facebook;
using namespace rnwinrt;
jsi::Value WinRTTurboModuleSpecJSI_initialize(
jsi::Runtime& runtime, react::TurboModule& turboModule, const jsi::Value*, size_t);
thread_local runtime_context* current_thread_context = nullptr;
WinRTTurboModule::WinRTTurboModule(std::shared_ptr<react::CallInvoker> invoker) :
TurboModule("WinRTTurboModule", invoker), m_invoker(std::move(invoker))
{
methodMap_["initialize"] = MethodMetadata{ 0, WinRTTurboModuleSpecJSI_initialize };
APTTYPE type;
APTTYPEQUALIFIER typeQualifier;
if (::CoGetApartmentType(&type, &typeQualifier) == CO_E_NOTINITIALIZED)
{
winrt::terminate();
}
}
WinRTTurboModule::~WinRTTurboModule()
{
if (auto ptr = std::exchange(current_thread_context, nullptr))
{
ptr->release();
}
}
// Functions exposed to JS
void WinRTTurboModule::initialize(jsi::Runtime& runtime)
{
if (!m_initialized)
{
m_initialized = true;
assert(!current_thread_context);
current_thread_context = new runtime_context(
runtime, [invoker = m_invoker](std::function<void()> fn) { invoker->invokeAsync(std::move(fn)); });
auto global = runtime.global();
for (auto data : root_namespaces)
{
global.setProperty(runtime, make_string(runtime, data->name), data->create(runtime));
}
}
}
runtime_context* rnwinrt::current_runtime_context()
{
auto result = current_thread_context;
if (!result)
{
// WinRT module not initialized for the current thread. This is likely because of a logic error in the runtime
// where we are trying to access data from a background thread instead of caching the context on creation
winrt::terminate();
}
return result;
}
jsi::Value WinRTTurboModuleSpecJSI_initialize(
jsi::Runtime& runtime, react::TurboModule& turboModule, const jsi::Value*, size_t)
{
static_cast<WinRTTurboModule*>(&turboModule)->initialize(runtime);
return jsi::Value::undefined();
}
std::shared_ptr<react::TurboModule> CreateWinRTTurboModule(
const std::shared_ptr<facebook::react::CallInvoker>& jsInvoker)
{
return std::make_shared<WinRTTurboModule>(jsInvoker);
}