Skip to content

Commit 336f1fc

Browse files
committed
Re-indent
1 parent 88bc348 commit 336f1fc

File tree

1 file changed

+91
-91
lines changed

1 file changed

+91
-91
lines changed

cpp/RNMultithreadingInstaller.cpp

Lines changed: 91 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
#if __has_include(<RNReanimated/Scheduler.h>)
77
#include <RNReanimated/Scheduler.h>
8-
#include <RNReanimated/ShareableValue.h>
9-
#include <RNReanimated/RuntimeManager.h>
10-
#include <RNReanimated/RuntimeDecorator.h>
11-
#include <RNReanimated/ErrorHandler.h>
8+
#include <RNReanimated/ShareableValue.h>
9+
#include <RNReanimated/RuntimeManager.h>
10+
#include <RNReanimated/RuntimeDecorator.h>
11+
#include <RNReanimated/ErrorHandler.h>
1212
#else
1313
#include "Scheduler.h"
1414
#include "ShareableValue.h"
@@ -22,93 +22,93 @@
2222
#endif
2323

2424
namespace mrousavy {
25-
namespace multithreading {
26-
27-
std::unique_ptr<reanimated::RuntimeManager> manager;
28-
29-
void install(jsi::Runtime& runtime,
30-
const std::function<std::unique_ptr<jsi::Runtime>()>& makeRuntime,
31-
const std::function<std::shared_ptr<reanimated::Scheduler>()>& makeScheduler,
32-
const std::function<std::shared_ptr<reanimated::ErrorHandler>(std::shared_ptr<reanimated::Scheduler>)>& makeErrorHandler) {
33-
auto pool = std::make_shared<ThreadPool>(1);
34-
35-
// Quickly setup the runtime - this is executed in parallel so we have to join this on the JS thread if spawnThread is called before this finishes.
36-
auto setupFutureSingle = pool->enqueue([makeScheduler, makeRuntime, makeErrorHandler]() {
37-
#ifdef ON_ANDROID
38-
// We need to attach this Thread to JNI because the Runtime is a HybridClass.
39-
jni::ThreadScope::WithClassLoader([makeRuntime, makeScheduler, makeErrorHandler]() {
40-
__unused jni::ThreadScope scope;
41-
#endif
42-
auto runtime = makeRuntime();
43-
reanimated::RuntimeDecorator::decorateRuntime(*runtime, "CUSTOM_THREAD_1");
44-
auto scheduler = makeScheduler();
45-
auto errorHandler = makeErrorHandler(scheduler);
46-
manager = std::make_unique<reanimated::RuntimeManager>(std::move(runtime),
47-
errorHandler,
48-
scheduler);
49-
#ifdef ON_ANDROID
25+
namespace multithreading {
26+
27+
std::unique_ptr<reanimated::RuntimeManager> manager;
28+
29+
void install(jsi::Runtime& runtime,
30+
const std::function<std::unique_ptr<jsi::Runtime>()>& makeRuntime,
31+
const std::function<std::shared_ptr<reanimated::Scheduler>()>& makeScheduler,
32+
const std::function<std::shared_ptr<reanimated::ErrorHandler>(std::shared_ptr<reanimated::Scheduler>)>& makeErrorHandler) {
33+
auto pool = std::make_shared<ThreadPool>(1);
34+
35+
// Quickly setup the runtime - this is executed in parallel so we have to join this on the JS thread if spawnThread is called before this finishes.
36+
auto setupFutureSingle = pool->enqueue([makeScheduler, makeRuntime, makeErrorHandler]() {
37+
#ifdef ON_ANDROID
38+
// We need to attach this Thread to JNI because the Runtime is a HybridClass.
39+
jni::ThreadScope::WithClassLoader([makeRuntime, makeScheduler, makeErrorHandler]() {
40+
__unused jni::ThreadScope scope;
41+
#endif
42+
auto runtime = makeRuntime();
43+
reanimated::RuntimeDecorator::decorateRuntime(*runtime, "CUSTOM_THREAD_1");
44+
auto scheduler = makeScheduler();
45+
auto errorHandler = makeErrorHandler(scheduler);
46+
manager = std::make_unique<reanimated::RuntimeManager>(std::move(runtime),
47+
errorHandler,
48+
scheduler);
49+
#ifdef ON_ANDROID
50+
});
51+
#endif
52+
});
53+
auto setupFuture = std::make_shared<std::future<void>>(std::move(setupFutureSingle));
54+
55+
// spawnThread(run: () => T): Promise<T>
56+
auto spawnThread = jsi::Function::createFromHostFunction(runtime,
57+
jsi::PropNameID::forAscii(runtime, "spawnThread"),
58+
1, // run
59+
[setupFuture, pool](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
60+
if (!arguments[0].isObject())
61+
throw jsi::JSError(runtime, "spawnThread: First argument has to be a function!");
62+
63+
if (setupFuture->valid())
64+
setupFuture->get(); // clears future, makes invalid
65+
66+
auto worklet = reanimated::ShareableValue::adapt(runtime, arguments[0], manager.get());
67+
68+
auto spawnThreadCallback = jsi::Function::createFromHostFunction(runtime,
69+
jsi::PropNameID::forAscii(runtime, "spawnThreadCallback"),
70+
2,
71+
[worklet, pool](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
72+
auto resolverValue = std::make_shared<jsi::Value>((arguments[0].asObject(runtime)));
73+
auto rejecterValue = std::make_shared<jsi::Value>((arguments[1].asObject(runtime)));
74+
75+
auto resolver = [&runtime, resolverValue](const std::shared_ptr<reanimated::ShareableValue>& shareableValue) {
76+
manager->scheduler->scheduleOnJS([&runtime, resolverValue, shareableValue] () {
77+
resolverValue->asObject(runtime).asFunction(runtime).call(runtime, shareableValue->getValue(runtime));
78+
});
79+
};
80+
auto rejecter = [&runtime, rejecterValue](const std::string& message) {
81+
manager->scheduler->scheduleOnJS([&runtime, rejecterValue, message] () {
82+
rejecterValue->asObject(runtime).asFunction(runtime).call(runtime, jsi::JSError(runtime, message).value());
83+
});
84+
};
85+
86+
pool->enqueue([resolver, rejecter, worklet]() {
87+
try {
88+
auto& runtime = *manager->runtime;
89+
90+
auto function = worklet->getValue(runtime).asObject(runtime).asFunction(runtime);
91+
auto result = function.getFunction(runtime).callWithThis(runtime, function);
92+
93+
auto shareableResult = reanimated::ShareableValue::adapt(runtime, result, manager.get());
94+
resolver(shareableResult);
95+
} catch (std::exception& exc) {
96+
rejecter(exc.what());
97+
}
5098
});
51-
#endif
99+
return jsi::Value::undefined();
52100
});
53-
auto setupFuture = std::make_shared<std::future<void>>(std::move(setupFutureSingle));
54-
55-
// spawnThread(run: () => T): Promise<T>
56-
auto spawnThread = jsi::Function::createFromHostFunction(runtime,
57-
jsi::PropNameID::forAscii(runtime, "spawnThread"),
58-
1, // run
59-
[setupFuture, pool](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
60-
if (!arguments[0].isObject())
61-
throw jsi::JSError(runtime, "spawnThread: First argument has to be a function!");
62-
63-
if (setupFuture->valid())
64-
setupFuture->get(); // clears future, makes invalid
65-
66-
auto worklet = reanimated::ShareableValue::adapt(runtime, arguments[0], manager.get());
67-
68-
auto spawnThreadCallback = jsi::Function::createFromHostFunction(runtime,
69-
jsi::PropNameID::forAscii(runtime, "spawnThreadCallback"),
70-
2,
71-
[worklet, pool](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
72-
auto resolverValue = std::make_shared<jsi::Value>((arguments[0].asObject(runtime)));
73-
auto rejecterValue = std::make_shared<jsi::Value>((arguments[1].asObject(runtime)));
74-
75-
auto resolver = [&runtime, resolverValue](const std::shared_ptr<reanimated::ShareableValue>& shareableValue) {
76-
manager->scheduler->scheduleOnJS([&runtime, resolverValue, shareableValue] () {
77-
resolverValue->asObject(runtime).asFunction(runtime).call(runtime, shareableValue->getValue(runtime));
78-
});
79-
};
80-
auto rejecter = [&runtime, rejecterValue](const std::string& message) {
81-
manager->scheduler->scheduleOnJS([&runtime, rejecterValue, message] () {
82-
rejecterValue->asObject(runtime).asFunction(runtime).call(runtime, jsi::JSError(runtime, message).value());
83-
});
84-
};
85-
86-
pool->enqueue([resolver, rejecter, worklet]() {
87-
try {
88-
auto& runtime = *manager->runtime;
89-
90-
auto function = worklet->getValue(runtime).asObject(runtime).asFunction(runtime);
91-
auto result = function.getFunction(runtime).callWithThis(runtime, function);
92-
93-
auto shareableResult = reanimated::ShareableValue::adapt(runtime, result, manager.get());
94-
resolver(shareableResult);
95-
} catch (std::exception& exc) {
96-
rejecter(exc.what());
97-
}
98-
});
99-
return jsi::Value::undefined();
100-
});
101-
102-
auto newPromise = runtime.global().getProperty(runtime, "Promise");
103-
auto promise = newPromise
104-
.asObject(runtime)
105-
.asFunction(runtime)
106-
.callAsConstructor(runtime, spawnThreadCallback);
107-
108-
return promise;
109-
});
110-
runtime.global().setProperty(runtime, "spawnThread", std::move(spawnThread));
111-
}
112-
113-
} // namespace multithreading
101+
102+
auto newPromise = runtime.global().getProperty(runtime, "Promise");
103+
auto promise = newPromise
104+
.asObject(runtime)
105+
.asFunction(runtime)
106+
.callAsConstructor(runtime, spawnThreadCallback);
107+
108+
return promise;
109+
});
110+
runtime.global().setProperty(runtime, "spawnThread", std::move(spawnThread));
111+
}
112+
113+
} // namespace multithreading
114114
} // namespace mrousavy

0 commit comments

Comments
 (0)