-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
JsiApiContext.h
64 lines (54 loc) · 2.48 KB
/
JsiApiContext.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#ifndef MICROSOFT_REACTNATIVE_JSI_JSIAPI
#define MICROSOFT_REACTNATIVE_JSI_JSIAPI
#include "../ReactContext.h"
#include "JsiAbiApi.h"
#include "ReactPromise.h"
namespace winrt::Microsoft::ReactNative {
// Try to get JSI Runtime for the current JS dispatcher thread.
// If it is not found, then create it based on context JSI runtime and store it in the context.Properties().
// The function returns nullptr if the current context does not have JSI runtime.
// It makes sure that the JSI runtime holder is removed when the instance is unloaded.
facebook::jsi::Runtime *TryGetOrCreateContextRuntime(ReactContext const &context) noexcept;
// Calls TryGetOrCreateContextRuntime to get JSI runtime.
// It crashes when TryGetOrCreateContextRuntime returns null.
// Note: deprecated in favor of TryGetOrCreateContextRuntime.
[[deprecated]] facebook::jsi::Runtime &GetOrCreateContextRuntime(ReactContext const &context) noexcept;
// Call provided lambda with the facebook::jsi::Runtime& parameter.
// For example: ExecuteJsi(context, [](facebook::jsi::Runtime& runtime){...})
// The code is executed synchronously if it is already in JSDispatcher, or asynchronously otherwise.
template <class TCodeWithRuntime>
void ExecuteJsi(ReactContext const &context, TCodeWithRuntime const &code, ReactPromise<void> *callStatus = nullptr) {
ReactDispatcher jsDispatcher = context.JSDispatcher();
auto callCode = [](ReactContext const &context, TCodeWithRuntime const &code, ReactPromise<void> *callStatus) {
facebook::jsi::Runtime *runtime = TryGetOrCreateContextRuntime(context);
if (runtime) {
code(*runtime);
}
// Report status of the call
if (callStatus) {
if (runtime) {
callStatus->Resolve();
} else {
callStatus->Reject("No JSI runtime");
}
}
};
if (jsDispatcher.HasThreadAccess()) {
// Execute immediately if we are in JS thread.
callCode(context, code, callStatus);
} else {
// Otherwise, schedule work in JS thread.
jsDispatcher.Post([callCode,
context,
code,
callStatus = callStatus ? std::make_unique<ReactPromise<void>>(*callStatus)
: std::unique_ptr<ReactPromise<void>>(nullptr)]() noexcept {
callCode(context, code, callStatus.get());
});
}
}
} // namespace winrt::Microsoft::ReactNative
#endif // MICROSOFT_REACTNATIVE_JSI_JSIAPI