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

feat: move notifying observers to event dispatcher #44474

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ import memoize from 'memoize-one';
import nullthrows from 'nullthrows';
import * as React from 'react';

if (Platform.OS === 'ios') {
require('../../Renderer/shims/ReactNative'); // Force side effects to prevent T55744311
}

const {NativeHorizontalScrollViewTuple, NativeVerticalScrollViewTuple} =
Platform.OS === 'android'
? {
Expand Down
15 changes: 15 additions & 0 deletions packages/react-native/React/CoreModules/RCTEventDispatcher.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ - (void)initialize
_eventsDispatchScheduled = NO;
_observers = [NSHashTable weakObjectsHashTable];
_observersLock = [NSRecursiveLock new];

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:)
name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil];
}

- (void)sendViewEventWithName:(NSString *)name reactTag:(NSNumber *)reactTag
Expand Down Expand Up @@ -225,6 +231,15 @@ - (void)flushEventsQueue
}
}

- (void)_notifyEventDispatcherObserversOfEvent_DEPRECATED:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
id<RCTEvent> event = [userInfo objectForKey:@"event"];
if (event) {
[self notifyObserversOfEvent:event];
}
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,10 @@ static void RCTSendScrollEventForNativeAnimations_DEPRECATED(UIScrollView *scrol
scrollViewZoomScale:scrollView.zoomScale
userData:nil
coalescingKey:coalescingKey];
RCTBridge *bridge = [RCTBridge currentBridge];
if (bridge) {
[bridge.eventDispatcher sendEvent:scrollEvent];
} else {
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:scrollEvent, @"event", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil
userInfo:userInfo];
}
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:scrollEvent, @"event", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil
userInfo:userInfo];
}

@interface RCTScrollViewComponentView () <
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,6 @@ - (instancetype)initWithDelegate:(id<RCTInstanceDelegate>)delegate
}
_launchOptions = launchOptions;

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

[defaultCenter addObserver:self
selector:@selector(_notifyEventDispatcherObserversOfEvent_DEPRECATED:)
name:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil];

[self _start];
}
return self;
Expand Down Expand Up @@ -481,19 +474,6 @@ - (void)_loadScriptFromSource:(RCTSource *)source
}
}

- (void)_notifyEventDispatcherObserversOfEvent_DEPRECATED:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
id<RCTEvent> event = [userInfo objectForKey:@"event"];

RCTModuleRegistry *moduleRegistry = _bridgeModuleDecorator.moduleRegistry;
if (event && moduleRegistry) {
id<RCTEventDispatcherProtocol> legacyEventDispatcher = [moduleRegistry moduleForName:"EventDispatcher"
lazilyLoadIfNecessary:YES];
[legacyEventDispatcher notifyObserversOfEvent:event];
}
}

- (void)_handleJSError:(const JsErrorHandler::ParsedError &)error
{
NSString *message = [NSString stringWithCString:error.message.c_str() encoding:[NSString defaultCStringEncoding]];
Expand Down