Skip to content

Commit

Permalink
[Fabric] Add a MainComponentsRegistry for the application
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico committed Feb 28, 2022
1 parent 8afa19e commit 5e7d88f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.rnnewarchitectureapp.components;

import com.facebook.common.internal.DoNotStrip;
import com.facebook.jni.HybridData;
import com.facebook.react.fabric.ComponentFactory;
import com.facebook.soloader.SoLoader;

@DoNotStrip
public class MainComponentsRegistry {
static {
SoLoader.loadLibrary("fabricjni");
}

@DoNotStrip private final HybridData mHybridData;

@DoNotStrip
private native HybridData initHybrid(ComponentFactory componentFactory);

@DoNotStrip
private MainComponentsRegistry(ComponentFactory componentFactory) {
mHybridData = initHybrid(componentFactory);
}

@DoNotStrip
public static MainComponentsRegistry register(ComponentFactory componentFactory) {
return new MainComponentsRegistry(componentFactory);
}
}
62 changes: 62 additions & 0 deletions android/app/src/main/jni/MainComponentsRegistry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "MainComponentsRegistry.h"

#include <CoreComponentsRegistry.h>
#include <fbjni/fbjni.h>
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
#include <react/renderer/components/answersolver/ComponentDescriptors.h>
#include <react/renderer/components/rncore/ComponentDescriptors.h>

namespace facebook {
namespace react {

MainComponentsRegistry::MainComponentsRegistry(
ComponentFactory *delegate)
: delegate_(delegate) {}

std::shared_ptr<ComponentDescriptorProviderRegistry const>
MainComponentsRegistry::sharedProviderRegistry() {
auto providerRegistry = CoreComponentsRegistry::sharedProviderRegistry();

providerRegistry->add(concreteComponentDescriptorProvider<
AnswerViewerComponentDescriptor>());

return providerRegistry;
}

jni::local_ref<MainComponentsRegistry::jhybriddata>
MainComponentsRegistry::initHybrid(
jni::alias_ref<jclass>,
ComponentFactory *delegate) {
auto instance = makeCxxInstance(delegate);

auto buildRegistryFunction =
[](EventDispatcher::Weak const &eventDispatcher,
ContextContainer::Shared const &contextContainer)
-> ComponentDescriptorRegistry::Shared {
auto registry = MainComponentsRegistry::sharedProviderRegistry()
->createComponentDescriptorRegistry(
{eventDispatcher, contextContainer});

auto mutableRegistry =
std::const_pointer_cast<ComponentDescriptorRegistry>(registry);

mutableRegistry->setFallbackComponentDescriptor(
std::make_shared<UnimplementedNativeViewComponentDescriptor>(
ComponentDescriptorParameters{
eventDispatcher, contextContainer, nullptr}));

return registry;
};

delegate->buildRegistryFunction = buildRegistryFunction;
return instance;
}

void MainComponentsRegistry::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", MainComponentsRegistry::initHybrid),
});
}

} // namespace react
} // namespace facebook
35 changes: 35 additions & 0 deletions android/app/src/main/jni/MainComponentsRegistry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <ComponentFactory.h>
#include <fbjni/fbjni.h>
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
#include <react/renderer/componentregistry/ComponentDescriptorRegistry.h>

namespace facebook {
namespace react {

class MainComponentsRegistry
: public facebook::jni::HybridClass<MainComponentsRegistry> {
public:
constexpr static auto kJavaDescriptor =
"Lcom/rnnewarchitectureapp/components/MainComponentsRegistry;";

static void registerNatives();

MainComponentsRegistry(ComponentFactory *delegate);

private:
friend HybridBase;

static std::shared_ptr<ComponentDescriptorProviderRegistry const>
sharedProviderRegistry();

const ComponentFactory *delegate_;

static jni::local_ref<jhybriddata> initHybrid(
jni::alias_ref<jclass>,
ComponentFactory *delegate);
};

} // namespace react
} // namespace facebook
2 changes: 2 additions & 0 deletions android/app/src/main/jni/OnLoad.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <fbjni/fbjni.h>
#include "MainApplicationTurboModuleManagerDelegate.h"
#include "MainComponentsRegistry.h"

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
return facebook::jni::initialize(vm, [] {
facebook::react::MainApplicationTurboModuleManagerDelegate::registerNatives();
facebook::react::MainComponentsRegistry::registerNatives();
});
}

0 comments on commit 5e7d88f

Please sign in to comment.