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

[RNMobile] Fix MaxListenersExceededWarning caused by dark-mode event emitter #17186

Merged
merged 5 commits into from
Aug 26, 2019
Merged
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
22 changes: 19 additions & 3 deletions packages/components/src/mobile/dark-mode/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import { eventEmitter, initialMode } from 'react-native-dark-mode';
import React from 'react';

// This was failing on CI
if ( eventEmitter.setMaxListeners ) {
eventEmitter.setMaxListeners( 150 );
}

export function useStyle( light, dark, theme ) {
const finalDark = {
...light,
Expand All @@ -19,15 +24,26 @@ export function withTheme( WrappedComponent ) {
constructor( props ) {
super( props );

this.onModeChanged = this.onModeChanged.bind( this );

this.state = {
mode: initialMode,
};
}

onModeChanged( newMode ) {
this.setState( { mode: newMode } );
}

componentDidMount() {
eventEmitter.on( 'currentModeChanged', ( newMode ) => {
this.setState( { mode: newMode } );
} );
this.subscription = eventEmitter.on( 'currentModeChanged', this.onModeChanged );
}

componentWillUnmount() {
// Conditional needed to pass UI Tests on CI
if ( eventEmitter.removeListener ) {
eventEmitter.removeListener( 'currentModeChanged', this.onModeChanged );
}
}

render() {
Expand Down