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

Clean session disconnect event even if an error occurs #625

Open
wants to merge 4 commits into
base: 0.21.4
Choose a base branch
from
Open
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
43 changes: 26 additions & 17 deletions src/OTSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { View } from 'react-native';
import { ViewPropTypes } from 'deprecated-react-native-prop-types';
import PropTypes from 'prop-types';
import { pick, isNull } from 'underscore';
import { setNativeEvents, removeNativeEvents, OT } from './OT';
import { nativeEvents, setNativeEvents, removeNativeEvents, OT } from './OT';
import { sanitizeSessionEvents, sanitizeSessionOptions, sanitizeSignalData,
sanitizeCredentials, getConnectionStatus } from './helpers/OTSessionHelper';
import { handleError } from './OTError';
Expand All @@ -16,17 +16,26 @@ export default class OTSession extends Component {
this.state = {
sessionInfo: null,
};
this.componentEvents = {
sessionDisconnected: Platform.OS === "android" ? "session:onDisconnected" : "session:sessionDidDisconnect",
};
this.componentEventsArray = Object.values(this.componentEvents);
this.otrnEventHandler = getOtrnErrorEventHandler(this.props.eventHandlers);
this.initComponent();
}
initComponent = () => {
const credentials = pick(this.props, ['apiKey', 'sessionId', 'token']);
const credentials = pick(this.props, ["apiKey", "sessionId", "token"]);
this.sanitizedCredentials = sanitizeCredentials(credentials);
if (Object.keys(this.sanitizedCredentials).length === 3) {
const sessionEvents = sanitizeSessionEvents(this.sanitizedCredentials.sessionId, this.props.eventHandlers);
setNativeEvents(sessionEvents);
OT.setJSComponentEvents(this.componentEventsArray);
this.sessionDisconnected = nativeEvents.addListener(`${this.props.sessionId}:${this.componentEvents.sessionDisconnected}`, (session) =>
this.sessionDisconnectedHandler(session)
);

}
}
};
componentDidMount() {
const sessionOptions = sanitizeSessionOptions(this.props.options);
const { apiKey, sessionId, token } = this.sanitizedCredentials;
Expand All @@ -52,7 +61,7 @@ export default class OTSession extends Component {
}
};

updateSessionProperty('signal', {});
updateSessionProperty("signal", {});
}
componentWillUnmount() {
this.disconnectSession();
Expand All @@ -67,11 +76,11 @@ export default class OTSession extends Component {
} else {
OT.getSessionInfo(sessionId, (session) => {
if (!isNull(session)) {
const sessionInfo = { ...session, connectionStatus: getConnectionStatus(session.connectionStatus)};
const sessionInfo = { ...session, connectionStatus: getConnectionStatus(session.connectionStatus) };
this.setState({
sessionInfo,
});
logOT({ apiKey, sessionId, action: 'rn_on_connect', proxyUrl: sessionOptions.proxyUrl, connectionId: session.connection.connectionId });
logOT({ apiKey, sessionId, action: "rn_on_connect", proxyUrl: sessionOptions.proxyUrl, connectionId: session.connection.connectionId });
if (Object.keys(signal).length > 0) {
this.signal(signal);
}
Expand All @@ -84,10 +93,9 @@ export default class OTSession extends Component {
OT.disconnectSession(this.props.sessionId, (disconnectError) => {
if (disconnectError) {
this.otrnEventHandler(disconnectError);
} else {
const events = sanitizeSessionEvents(this.props.sessionId, this.props.eventHandlers);
removeNativeEvents(events);
}
const events = sanitizeSessionEvents(this.props.sessionId, this.props.eventHandlers);
removeNativeEvents(events);
});
}
getSessionInfo() {
Expand All @@ -97,15 +105,19 @@ export default class OTSession extends Component {
const signalData = sanitizeSignalData(signal);
OT.sendSignal(this.props.sessionId, signalData.signal, signalData.errorHandler);
}

sessionDisconnectedHandler = (session) => {
const events = sanitizeSessionEvents(this.props.sessionId, this.props.eventHandlers);
removeNativeEvents(events);
};

render() {
const { style, children, sessionId, apiKey, token } = this.props;
const { sessionInfo } = this.state;
if (children && sessionId && apiKey && token) {
return (
<OTContext.Provider value={{ sessionId, sessionInfo }}>
<View style={style}>
{ children }
</View>
<View style={style}>{children}</View>
</OTContext.Provider>
);
}
Expand All @@ -117,10 +129,7 @@ OTSession.propTypes = {
apiKey: PropTypes.string.isRequired,
sessionId: PropTypes.string.isRequired,
token: PropTypes.string.isRequired,
children: PropTypes.oneOfType([
PropTypes.element,
PropTypes.arrayOf(PropTypes.element),
]),
children: PropTypes.oneOfType([PropTypes.element, PropTypes.arrayOf(PropTypes.element)]),
style: ViewPropTypes.style,
eventHandlers: PropTypes.object, // eslint-disable-line react/forbid-prop-types
options: PropTypes.object, // eslint-disable-line react/forbid-prop-types
Expand All @@ -132,6 +141,6 @@ OTSession.defaultProps = {
options: {},
signal: {},
style: {
flex: 1
flex: 1,
},
};