Skip to content

Commit

Permalink
[RNMobile] Fix MaxListenersExceededWarning caused by dark-mode event …
Browse files Browse the repository at this point in the history
…emitter (#17186)

* Fix MaxListenersExceededWarning caused by dark-mode event emitter

* Checking for setMaxListeners trying to avoid CI error

* Adding remove listener to DarkMode HOC

* DarkMode: Binding this.onModeChanged to `this`

* DarkMode: Adding conditional needed to pass UI Tests on CI
  • Loading branch information
etoledom authored Aug 26, 2019
1 parent 316396c commit a7bb5b3
Showing 1 changed file with 19 additions and 3 deletions.
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

0 comments on commit a7bb5b3

Please sign in to comment.