Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to dispatch a custom event from Fabric components #34072

Closed
justblender opened this issue Jun 25, 2022 · 8 comments
Closed

Unable to dispatch a custom event from Fabric components #34072

justblender opened this issue Jun 25, 2022 · 8 comments
Labels
Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)

Comments

@justblender
Copy link

Description

I've set up a custom Codegen component with a property of onProgress, which infers DirectEventHandler<Readonly<...>> type.

Codegen correctly identifies its type and generates an event emitter for this component, but dispatching it from Objective-C++ results in always receiving an Unsupported top level event type topXYZ is dispatched error.

Simulator Screen Shot - iPhone 13 - 2022-06-25 at 20 45 43

Version

0.69.0

Output of npx react-native info

System:
    OS: macOS 12.4
    CPU: (8) arm64 Apple M1
    Memory: 270.31 MB / 8.00 GB
    Shell: 3.3.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 17.8.0 - /opt/homebrew/bin/node
    Yarn: 3.2.0 - /opt/homebrew/bin/yarn
    npm: 8.5.5 - /opt/homebrew/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK: Not Found
  IDEs:
    Android Studio: 2021.2 AI-212.5712.43.2112.8609683
    Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.15 - /opt/homebrew/Cellar/openjdk@11/11.0.14.1/libexec/openjdk.jdk/Contents/Home/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.0.0 => 18.0.0
    react-native: 0.69.0 => 0.69.0
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to reproduce

  1. Declare native Codegen component with any onEventName event handler prop.
  2. On the native side, acquire an instance of the generated EventEmitter class and dispatch any generated event.

Snack, code example, screenshot, or link to a repository

  1. Declare codegen native component TestComponent with onProgress event handler prop.
// @ts-ignore TODO: remove once there is a .d.ts file with definitions
import codegenNativeComponentUntyped from 'react-native/Libraries/Utilities/codegenNativeComponent';
import type { ViewProps, HostComponent } from 'react-native';
import type {
  Int32,
  WithDefault,
  DirectEventHandler,
  // @ts-ignore TODO: remove once there is a .d.ts file with definitions
} from 'react-native/Libraries/Types/CodegenTypes';

interface NativeProps extends ViewProps {
  onProgress?: DirectEventHandler<
    Readonly<{
      bytesWritten: Int32;
      bytesExpected: Int32;
    }>
  >;
}

const codegenNativeComponent = codegenNativeComponentUntyped as <T extends {}>(
  name: string
) => HostComponent<T>;

export default codegenNativeComponent<NativeProps>('TestComponent');
  1. On the native side, acquire an instance of the generated TestComponentEventEmitter class:
  - (std::shared_ptr<const TestComponentEventEmitter>)getEventEmitter
{
  if (!self->_eventEmitter) {
    return nullptr;
  }

  assert(std::dynamic_pointer_cast<TestComponentEventEmitter const>(self->_eventEmitter));
  return std::static_pointer_cast<TestComponentEventEmitter const>(self->_eventEmitter);
}
  1. Dispatch onProgress event:
const auto eventEmitter = [self getEventEmitter];
if (eventEmitter) {
  eventEmitter->onProgress(TestComponentEventEmitter::OnProgress{
    .bytesWritten = bytesWritten,
    .bytesExpected = bytesExpected
  });
}
@justblender justblender added Needs: Triage 🔍 Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules) labels Jun 25, 2022
@andrestone
Copy link
Contributor

Seems like this is not implemented for TypeScript.

const libraryName = basename(filename).replace(/NativeComponent\.js$/, '');

@justblender
Copy link
Author

Seems like this is not implemented for TypeScript.

@andrestone I've tried to rewrite Codegen types to Flow and it didn't help either, so it doesn't seem like TypeScript is the issue here.

Nonetheless, I was able to find a bit of free time today and "fix" this problem on my behalf. Strangely enough, using RCT_EXPORT_VIEW_PROPERTY on an inherited RCTViewManager class does the job, but I'm not sure why I have to touch old architecture files in order to enable direct event handlers for Fabric.

Feels super strange, so I'll probably keep this issue open.

@andrestone
Copy link
Contributor

andrestone commented Jul 24, 2022

I think the way you've solved it is probably the "recommended" way. See this example.

Writing the spec in flow (not in TS) also registers the view with the correct ViewConfig if you do this:

// index.js
// ...
import { setRuntimeConfigProvider } from 'react-native/Libraries/NativeComponent/NativeComponentRegistry';

setRuntimeConfigProvider((name) => {
  return name === 'MyView' ? { native: false, strict: false, verify: false } : null;
});

// ...

@cortinico
Copy link
Contributor

Nonetheless, I was able to find a bit of free time today and "fix" this problem on my behalf. Strangely enough, using RCT_EXPORT_VIEW_PROPERTY on an inherited RCTViewManager class does the job, but I'm not sure why I have to touch old architecture files in order to enable direct event handlers for Fabric.

cc @sammy-SC @cipolleschi is this related to Static View Config by any chance?

@github-actions
Copy link

github-actions bot commented Feb 1, 2023

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 1, 2023
@github-actions
Copy link

github-actions bot commented Feb 9, 2023

This issue was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions bot closed this as completed Feb 9, 2023
@facebook facebook locked as resolved and limited conversation to collaborators Feb 9, 2023
@sammy-SC sammy-SC reopened this Feb 9, 2023
@sammy-SC
Copy link
Contributor

sammy-SC commented Feb 9, 2023

Hey @justblender

do you still experience this in 0.71?

@cortinico cortinico removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Oct 17, 2023
@cipolleschi
Copy link
Contributor

We can close this. I verified that events are properly dispatched with no ViewManager. The app must run on Bridgeless mode, of course.

Screen.Recording.2024-01-24.at.16.09.20.mov

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: New Architecture Issues and PRs related to new architecture (Fabric/Turbo Modules)
Projects
None yet
Development

No branches or pull requests

5 participants