Skip to content

Commit ff1c090

Browse files
committed
༼ つ ◕_◕ ༽つ Give Reanimated 2
1 parent 757de23 commit ff1c090

File tree

213 files changed

+8680
-974
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

213 files changed

+8680
-974
lines changed

.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ module.exports = {
1717
'react/jsx-uses-vars': 2,
1818
'react/jsx-uses-react': 2,
1919
},
20+
settings: {
21+
'import/core-modules': ['react-native-reanimated'],
22+
},
2023
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#include "NativeReanimatedModule.h"
2+
#include <memory>
3+
#include "Logger.h"
4+
#include <functional>
5+
#include <thread>
6+
#include "SpeedChecker.h"
7+
#include "Shareable.h"
8+
#include "MapperRegistry.h"
9+
#include "Mapper.h"
10+
#include "RuntimeDecorator.h"
11+
#include "EventHandlerRegistry.h"
12+
#include "EventHandler.h"
13+
14+
using namespace facebook;
15+
16+
namespace reanimated {
17+
18+
void extractMutables(jsi::Runtime &rt,
19+
std::shared_ptr<ShareableValue> sv,
20+
std::vector<std::shared_ptr<MutableValue>> &res) {
21+
switch (sv->type) {
22+
case MutableValueType:
23+
res.push_back(sv->mutableValue);
24+
break;
25+
case ArrayType:
26+
for (auto &it : sv->frozenArray) {
27+
extractMutables(rt, it, res);
28+
}
29+
break;
30+
case RemoteObjectType:
31+
case ObjectType:
32+
for (auto &it : sv->frozenObject->map) {
33+
extractMutables(rt, it.second, res);
34+
}
35+
break;
36+
default:
37+
break;
38+
}
39+
}
40+
41+
std::vector<std::shared_ptr<MutableValue>> extractMutablesFromArray(jsi::Runtime &rt, const jsi::Array &array, NativeReanimatedModule *module) {
42+
std::vector<std::shared_ptr<MutableValue>> res;
43+
for (size_t i = 0, size = array.size(rt); i < size; i++) {
44+
auto shareable = ShareableValue::adapt(rt, array.getValueAtIndex(rt, i), module);
45+
extractMutables(rt, shareable, res);
46+
}
47+
return res;
48+
}
49+
50+
NativeReanimatedModule::NativeReanimatedModule(std::shared_ptr<CallInvoker> jsInvoker,
51+
std::shared_ptr<Scheduler> scheduler,
52+
std::unique_ptr<jsi::Runtime> rt,
53+
std::function<void(std::function<void(double)>)> requestRender,
54+
std::function<void(jsi::Runtime&, int, const jsi::Object&)> propUpdater):
55+
NativeReanimatedModuleSpec(jsInvoker),
56+
scheduler(scheduler),
57+
runtime(std::move(rt)),
58+
mapperRegistry(new MapperRegistry()),
59+
eventHandlerRegistry(new EventHandlerRegistry()),
60+
requestRender(requestRender) {
61+
RuntimeDecorator::addNativeObjects(*runtime, propUpdater, [=](FrameCallback callback) {
62+
frameCallbacks.push_back(callback);
63+
maybeRequestRender();
64+
});
65+
}
66+
67+
bool NativeReanimatedModule::isUIRuntime(jsi::Runtime &rt) {
68+
return runtime.get() == &rt;
69+
}
70+
71+
bool NativeReanimatedModule::isHostRuntime(jsi::Runtime &rt) {
72+
return !isUIRuntime(rt);
73+
}
74+
75+
void NativeReanimatedModule::installCoreFunctions(jsi::Runtime &rt, const jsi::Value &valueSetter) {
76+
this->valueSetter = ShareableValue::adapt(rt, valueSetter, this);
77+
}
78+
79+
jsi::Value NativeReanimatedModule::makeShareable(jsi::Runtime &rt, const jsi::Value &value) {
80+
return ShareableValue::adapt(rt, value, this)->getValue(rt);
81+
}
82+
83+
jsi::Value NativeReanimatedModule::makeMutable(jsi::Runtime &rt, const jsi::Value &value) {
84+
return ShareableValue::adapt(rt, value, this, MutableValueType)->getValue(rt);
85+
}
86+
87+
jsi::Value NativeReanimatedModule::makeRemote(jsi::Runtime &rt, const jsi::Value &value) {
88+
return ShareableValue::adapt(rt, value, this, RemoteObjectType)->getValue(rt);
89+
}
90+
91+
jsi::Value NativeReanimatedModule::startMapper(jsi::Runtime &rt, const jsi::Value &worklet, const jsi::Value &inputs, const jsi::Value &outputs) {
92+
static unsigned long MAPPER_ID = 1;
93+
94+
unsigned long newMapperId = MAPPER_ID++;
95+
auto mapperShareable = ShareableValue::adapt(rt, worklet, this);
96+
auto inputMutables = extractMutablesFromArray(rt, inputs.asObject(rt).asArray(rt), this);
97+
auto outputMutables = extractMutablesFromArray(rt, outputs.asObject(rt).asArray(rt), this);
98+
99+
scheduler->scheduleOnUI([=] {
100+
auto mapperFunction = mapperShareable->getValue(*runtime).asObject(*runtime).asFunction(*runtime);
101+
auto mapper = std::make_shared<Mapper>(this, newMapperId, std::move(mapperFunction), inputMutables, outputMutables);
102+
mapperRegistry->startMapper(mapper);
103+
maybeRequestRender();
104+
});
105+
106+
return jsi::Value((double)newMapperId);
107+
}
108+
109+
void NativeReanimatedModule::stopMapper(jsi::Runtime &rt, const jsi::Value &mapperId) {
110+
unsigned long id = mapperId.asNumber();
111+
scheduler->scheduleOnUI([=] {
112+
mapperRegistry->stopMapper(id);
113+
});
114+
}
115+
116+
jsi::Value NativeReanimatedModule::registerEventHandler(jsi::Runtime &rt, const jsi::Value &eventHash, const jsi::Value &worklet) {
117+
static unsigned long EVENT_HANDLER_ID = 1;
118+
119+
unsigned long newRegistrationId = EVENT_HANDLER_ID++;
120+
auto eventName = eventHash.asString(rt).utf8(rt);
121+
auto handlerShareable = ShareableValue::adapt(rt, worklet, this);
122+
123+
scheduler->scheduleOnUI([=] {
124+
auto handlerFunction = handlerShareable->getValue(*runtime).asObject(*runtime).asFunction(*runtime);
125+
auto handler = std::make_shared<EventHandler>(newRegistrationId, eventName, std::move(handlerFunction));
126+
eventHandlerRegistry->registerEventHandler(handler);
127+
});
128+
129+
return jsi::Value((double)newRegistrationId);
130+
}
131+
132+
void NativeReanimatedModule::unregisterEventHandler(jsi::Runtime &rt, const jsi::Value &registrationId) {
133+
unsigned long id = registrationId.asNumber();
134+
scheduler->scheduleOnUI([=] {
135+
eventHandlerRegistry->unregisterEventHandler(id);
136+
});
137+
}
138+
139+
void NativeReanimatedModule::onEvent(std::string eventName, std::string eventAsString) {
140+
eventHandlerRegistry->processEvent(*runtime, eventName, eventAsString);
141+
}
142+
143+
void NativeReanimatedModule::maybeRequestRender() {
144+
if (!renderRequested) {
145+
renderRequested = true;
146+
requestRender([this](double timestampMs) {
147+
this->renderRequested = false;
148+
this->onRender(timestampMs);
149+
});
150+
}
151+
}
152+
153+
void NativeReanimatedModule::onRender(double timestampMs) {
154+
mapperRegistry->execute(*runtime);
155+
156+
std::vector<FrameCallback> callbacks = frameCallbacks;
157+
frameCallbacks.clear();
158+
for (auto callback : callbacks) {
159+
callback(timestampMs);
160+
}
161+
162+
if (mapperRegistry->needRunOnRender()) {
163+
maybeRequestRender();
164+
}
165+
}
166+
167+
NativeReanimatedModule::~NativeReanimatedModule() {
168+
// noop
169+
}
170+
171+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "NativeReanimatedModuleSpec.h"
2+
3+
namespace reanimated {
4+
5+
static jsi::Value __hostFunction_NativeReanimatedModuleSpec_installCoreFunctions(
6+
jsi::Runtime &rt,
7+
TurboModule &turboModule,
8+
const jsi::Value *args,
9+
size_t count) {
10+
static_cast<NativeReanimatedModuleSpec *>(&turboModule)
11+
->installCoreFunctions(rt, std::move(args[0]));
12+
return jsi::Value::undefined();
13+
}
14+
15+
// SharedValue
16+
17+
static jsi::Value __hostFunction_NativeReanimatedModuleSpec_makeShareable(
18+
jsi::Runtime &rt,
19+
TurboModule &turboModule,
20+
const jsi::Value *args,
21+
size_t count) {
22+
return static_cast<NativeReanimatedModuleSpec *>(&turboModule)
23+
->makeShareable(rt, std::move(args[0]));
24+
}
25+
26+
static jsi::Value __hostFunction_NativeReanimatedModuleSpec_makeMutable(
27+
jsi::Runtime &rt,
28+
TurboModule &turboModule,
29+
const jsi::Value *args,
30+
size_t count) {
31+
return static_cast<NativeReanimatedModuleSpec *>(&turboModule)
32+
->makeMutable(rt, std::move(args[0]));
33+
}
34+
35+
static jsi::Value __hostFunction_NativeReanimatedModuleSpec_makeRemote(
36+
jsi::Runtime &rt,
37+
TurboModule &turboModule,
38+
const jsi::Value *args,
39+
size_t count) {
40+
return static_cast<NativeReanimatedModuleSpec *>(&turboModule)
41+
->makeRemote(rt, std::move(args[0]));
42+
}
43+
44+
static jsi::Value __hostFunction_NativeReanimatedModuleSpec_startMapper(
45+
jsi::Runtime &rt,
46+
TurboModule &turboModule,
47+
const jsi::Value *args,
48+
size_t count) {
49+
return static_cast<NativeReanimatedModuleSpec *>(&turboModule)
50+
->startMapper(rt, std::move(args[0]), std::move(args[1]), std::move(args[2]));
51+
}
52+
53+
static jsi::Value __hostFunction_NativeReanimatedModuleSpec_stopMapper(
54+
jsi::Runtime &rt,
55+
TurboModule &turboModule,
56+
const jsi::Value *args,
57+
size_t count) {
58+
static_cast<NativeReanimatedModuleSpec *>(&turboModule)
59+
->stopMapper(rt, std::move(args[0]));
60+
return jsi::Value::undefined();
61+
}
62+
63+
static jsi::Value __hostFunction_NativeReanimatedModuleSpec_registerEventHandler(
64+
jsi::Runtime &rt,
65+
TurboModule &turboModule,
66+
const jsi::Value *args,
67+
size_t count) {
68+
return static_cast<NativeReanimatedModuleSpec *>(&turboModule)
69+
->registerEventHandler(rt, std::move(args[0]), std::move(args[1]));
70+
}
71+
72+
static jsi::Value __hostFunction_NativeReanimatedModuleSpec_unregisterEventHandler(
73+
jsi::Runtime &rt,
74+
TurboModule &turboModule,
75+
const jsi::Value *args,
76+
size_t count) {
77+
static_cast<NativeReanimatedModuleSpec *>(&turboModule)
78+
->unregisterEventHandler(rt, std::move(args[0]));
79+
return jsi::Value::undefined();
80+
}
81+
82+
NativeReanimatedModuleSpec::NativeReanimatedModuleSpec(std::shared_ptr<CallInvoker> jsInvoker)
83+
: TurboModule("NativeReanimated", jsInvoker) {
84+
methodMap_["installCoreFunctions"] = MethodMetadata{
85+
1, __hostFunction_NativeReanimatedModuleSpec_installCoreFunctions};
86+
87+
88+
methodMap_["makeShareable"] = MethodMetadata{
89+
1, __hostFunction_NativeReanimatedModuleSpec_makeShareable};
90+
methodMap_["makeMutable"] = MethodMetadata{
91+
1, __hostFunction_NativeReanimatedModuleSpec_makeMutable};
92+
methodMap_["makeRemote"] = MethodMetadata{
93+
1, __hostFunction_NativeReanimatedModuleSpec_makeRemote};
94+
95+
96+
methodMap_["startMapper"] = MethodMetadata{
97+
3, __hostFunction_NativeReanimatedModuleSpec_startMapper};
98+
methodMap_["stopMapper"] = MethodMetadata{
99+
1, __hostFunction_NativeReanimatedModuleSpec_stopMapper};
100+
101+
methodMap_["registerEventHandler"] = MethodMetadata{
102+
2, __hostFunction_NativeReanimatedModuleSpec_registerEventHandler};
103+
methodMap_["unregisterEventHandler"] = MethodMetadata{
104+
1, __hostFunction_NativeReanimatedModuleSpec_unregisterEventHandler};
105+
}
106+
107+
}
108+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "EventHandlerRegistry.h"
2+
3+
#include "EventHandler.h"
4+
5+
namespace reanimated {
6+
7+
static jsi::Value eval(jsi::Runtime &rt, const char *code) {
8+
return rt.global().getPropertyAsFunction(rt, "eval").call(rt, code);
9+
}
10+
11+
void EventHandlerRegistry::registerEventHandler(std::shared_ptr<EventHandler> eventHandler) {
12+
eventMappings[eventHandler->eventName][eventHandler->id] = eventHandler;
13+
eventHandlers[eventHandler->id] = eventHandler;
14+
}
15+
16+
void EventHandlerRegistry::unregisterEventHandler(unsigned long id) {
17+
auto handlerIt = eventHandlers.find(id);
18+
if (handlerIt != eventHandlers.end()) {
19+
eventMappings[handlerIt->second->eventName].erase(id);
20+
}
21+
}
22+
23+
void EventHandlerRegistry::processEvent(jsi::Runtime &rt, std::string eventName, std::string eventPayload) {
24+
auto handlersIt = eventMappings.find(eventName);
25+
if (handlersIt != eventMappings.end()) {
26+
// TODO: use jsi::Value::createFromJsonUtf8
27+
auto eventObject = eval(rt, ("(" + eventPayload + ")").c_str()).asObject(rt).getProperty(rt, "NativeMap");
28+
eventObject.asObject(rt).setProperty(rt, "eventName", jsi::String::createFromUtf8(rt, eventName));
29+
for (auto handler : handlersIt->second) {
30+
handler.second->process(rt, eventObject);
31+
}
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)