Last active
September 30, 2021 03:54
-
-
Save dillonhafer/c7cb80bdcd3c3696e7437f634d4ef00d to your computer and use it in GitHub Desktop.
Patch bugs in Rails' action cable to be able to use with ReactNative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Just "import from './RN-actioncable-PATCH'" before using @rails/actioncable | |
import { AppState } from 'react-native'; | |
let appState = AppState.currentState; | |
let sub: { remove(): void } | null = null; | |
// @ts-ignore | |
global.document = { visibilityState: 'visible' }; | |
// @ts-ignore | |
global.addEventListener = (name: string, visibilityDidChange: () => void) => { | |
if (name !== 'visibilitychange') { | |
return; | |
} | |
if (sub === null) { | |
// @ts-ignore | |
sub = AppState.addEventListener('change', (nextAppState) => { | |
if (appState.match(/inactive|background/) && nextAppState === 'active') { | |
console.log('App has come to the foreground!'); | |
// @ts-ignore | |
global.document = { visibilityState: 'visible' }; | |
} else { | |
// @ts-ignore | |
global.document = { visibilityState: 'hidden' }; | |
} | |
appState = nextAppState; | |
console.log('calling action cable add'); | |
visibilityDidChange(); | |
}); | |
} | |
}; | |
// @ts-ignore | |
global.removeEventListener = ( | |
name: string, | |
visibilityDidChange: () => void, | |
) => { | |
if (name !== 'visibilitychange') { | |
return; | |
} | |
if (sub !== null) { | |
sub.remove(); | |
sub = null; | |
} | |
console.log('calling action cable remove'); | |
visibilityDidChange(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment