opentok-react-native
Advanced tools
Comparing version 0.3.1 to 0.3.2
{ | ||
"name": "opentok-react-native", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "React Native components for OpenTok iOS and Android SDKs", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -7,13 +7,2 @@ import { NativeModules, NativeEventEmitter, PermissionsAndroid } from 'react-native'; | ||
const createSession = data => new Promise((resolve, reject) => { | ||
OT.initSession(data.apiKey, data.sessionId); | ||
OT.connect(data.token, (error) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
const checkAndroidPermissions = () => new Promise((resolve, reject) => { | ||
@@ -43,28 +32,2 @@ PermissionsAndroid.requestMultiple([ | ||
const createPublisher = properties => new Promise((resolve, reject) => { | ||
OT.initPublisher(properties, (initPublisherError) => { | ||
if (initPublisherError) { | ||
reject(initPublisherError); | ||
} else { | ||
OT.publish((publishError) => { | ||
if (publishError) { | ||
reject(publishError); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
const disconnectSession = () => new Promise((resolve, reject) => { | ||
OT.disconnectSession((disconnectError) => { | ||
if (disconnectError) { | ||
reject(disconnectError); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
const setNativeEvents = (events) => { | ||
@@ -90,7 +53,4 @@ const eventNames = Object.keys(events); | ||
export { | ||
createSession, | ||
createPublisher, | ||
OT, | ||
nativeEvents, | ||
disconnectSession, | ||
checkAndroidPermissions, | ||
@@ -97,0 +57,0 @@ setNativeEvents, |
@@ -15,3 +15,7 @@ import React, { Component } from 'react'; | ||
}; | ||
this.streamDestroyed = Platform.OS === 'android' ? 'publisher:onStreamDestroyed' : 'publisher:streamDestroyed'; | ||
this.componentEvents = { | ||
streamDestroyed: Platform.OS === 'android' ? 'publisher:onStreamDestroyed' : 'publisher:streamDestroyed', | ||
sessionConnected: Platform.OS === 'android' ? 'session:onConnected' : 'session:sessionDidConnect', | ||
}; | ||
this.componentEventsArray = Object.values(this.componentEvents); | ||
} | ||
@@ -21,5 +25,9 @@ componentWillMount() { | ||
setNativeEvents(publisherEvents); | ||
this.createPublisher(); | ||
this.setEventListeners(); | ||
OT.setJSComponentEvents(this.componentEventsArray); | ||
this.sessionConnected = nativeEvents.addListener(this.componentEvents.sessionConnected, () => this.sessionConnectedHandler()); | ||
this.streamDestroyed = nativeEvents.addListener(this.componentEvents.streamDestroyed, () => this.streamDestroyedHandler()); | ||
} | ||
componentDidMount() { | ||
this.createPublisher(); | ||
} | ||
componentDidUpdate(previousProps) { | ||
@@ -46,3 +54,3 @@ const useDefault = (value, defaultValue) => (value === undefined ? defaultValue : value); | ||
if (!error) { | ||
this.streamChanged.remove(); | ||
this.streamDestroyed.remove(); | ||
const events = sanitizePublisherEvents(this.props.eventHandlers); | ||
@@ -54,13 +62,21 @@ removeNativeEvents(events); | ||
}); | ||
OT.removeJSComponentEvents(this.componentEventsArray); | ||
this.sessionConnected.remove(); | ||
} | ||
setEventListeners() { | ||
this.streamChanged = nativeEvents.addListener( | ||
this.streamDestroyed, | ||
() => { | ||
sessionConnectedHandler = () => { | ||
OT.publish((publishError) => { | ||
if (publishError) { | ||
handleError(error); | ||
} else { | ||
this.setState({ | ||
publisher: null, | ||
}); | ||
}, | ||
); | ||
publisher: true, | ||
}) | ||
} | ||
}); | ||
} | ||
streamDestroyedHandler = () => { | ||
this.setState({ | ||
publisher: null, | ||
}); | ||
} | ||
createPublisher() { | ||
@@ -81,17 +97,9 @@ if (Platform.OS === 'android') { | ||
const publisherProperties = sanitizeProperties(this.props.properties); | ||
createPublisher(publisherProperties) | ||
.then(() => { | ||
this.setState({ | ||
publisher: true, | ||
}); | ||
}) | ||
.catch((error) => { | ||
handleError(error); | ||
}); | ||
OT.initPublisher(publisherProperties); | ||
} | ||
render() { | ||
if (!this.state.publisher) { | ||
return <View />; | ||
if (this.state.publisher) { | ||
return <OTPublisherView {...this.props} />; | ||
} | ||
return <OTPublisherView {...this.props} />; | ||
return <View />; | ||
} | ||
@@ -98,0 +106,0 @@ } |
import React, { Component, Children, cloneElement } from 'react'; | ||
import { View } from 'react-native'; | ||
import PropTypes from 'prop-types'; | ||
import { createSession, disconnectSession, setNativeEvents, OT } from './OT'; | ||
import { setNativeEvents, OT } from './OT'; | ||
import { sanitizeSessionEvents, sanitizeSignalData } from './helpers/OTSessionHelper'; | ||
@@ -13,7 +13,5 @@ import { logOT } from './helpers/OTHelper'; | ||
this.state = { | ||
isConnected: false, | ||
sessionInfo: null, | ||
}; | ||
} | ||
componentWillMount() { | ||
@@ -23,3 +21,3 @@ const sessionEvents = sanitizeSessionEvents(this.props.eventHandlers); | ||
this.createSession(); | ||
logOT(this.props.apiKey, this.props.sessionId); | ||
logOT(this.props.apiKey, this.props.sessionId); | ||
} | ||
@@ -48,32 +46,27 @@ componentDidUpdate(previousProps) { | ||
createSession() { | ||
createSession({ | ||
apiKey: this.props.apiKey, | ||
sessionId: this.props.sessionId, | ||
token: this.props.token, | ||
}) | ||
.then(() => { | ||
OT.initSession(this.props.apiKey, this.props.sessionId); | ||
OT.connect(this.props.token, (error) => { | ||
if (error) { | ||
handleError(error); | ||
} else { | ||
OT.getSessionInfo((sessionInfo) => { | ||
this.setState({ | ||
isConnected: true, | ||
sessionInfo, | ||
}); | ||
const signalData = sanitizeSignalData(this.props.signal); | ||
OT.sendSignal(signalData, signalData.errorHandler); | ||
}); | ||
const signalData = sanitizeSignalData(this.props.signal); | ||
OT.sendSignal(signalData, signalData.errorHandler); | ||
}) | ||
.catch((error) => { | ||
handleError(error); | ||
}); | ||
} | ||
}); | ||
} | ||
disconnectSession() { | ||
disconnectSession() | ||
.then(() => { | ||
OT.disconnectSession((disconnectError) => { | ||
if (disconnectError) { | ||
this.setState({ | ||
isConnected: false, | ||
sessionInfo: null, | ||
}); | ||
}) | ||
.catch((error) => { | ||
} else { | ||
handleError(error); | ||
}); | ||
} | ||
}); | ||
} | ||
@@ -84,3 +77,3 @@ getSessionInfo() { | ||
render() { | ||
if (this.state.isConnected && this.props.children) { | ||
if (this.props.children) { | ||
const childrenWithProps = Children.map( | ||
@@ -87,0 +80,0 @@ this.props.children, |
@@ -22,5 +22,8 @@ import React, { Component } from 'react'; | ||
componentWillMount() { | ||
const subscriberProperties = sanitizeProperties(this.props.properties); | ||
this.streamCreated = nativeEvents.addListener(this.componentEvents.streamCreated, stream => this.streamCreatedHandler(stream, subscriberProperties)); | ||
this.streamDestroyed = nativeEvents.addListener(this.componentEvents.streamDestroyed, stream => this.streamDestroyedHandler(stream)); | ||
const subscriberEvents = sanitizeSubscriberEvents(this.props.eventHandlers); | ||
OT.setJSComponentEvents(this.componentEventsArray); | ||
setNativeEvents(subscriberEvents); | ||
this.setEventListeners(); | ||
} | ||
@@ -32,43 +35,28 @@ componentWillUnmount() { | ||
} | ||
setEventListeners() { | ||
OT.setJSComponentEvents(this.componentEventsArray); | ||
this.streamCreated = nativeEvents.addListener( | ||
this.componentEvents.streamCreated, | ||
(stream) => { | ||
const subscriberProperties = sanitizeProperties(this.props.properties); | ||
OT.subscribeToStream(stream.streamId, subscriberProperties, (error) => { | ||
if (error) { | ||
handleError(error); | ||
} else { | ||
const oldStreams = this.state.streams; | ||
const streams = [...oldStreams, stream.streamId]; | ||
this.setState({ | ||
streams, | ||
}); | ||
} | ||
streamCreatedHandler = (stream, subscriberProperties) => { | ||
OT.subscribeToStream(stream.streamId, subscriberProperties, (error) => { | ||
if (error) { | ||
handleError(error); | ||
} else { | ||
this.setState({ | ||
streams: [...this.state.streams, stream.streamId], | ||
}); | ||
}, | ||
); | ||
this.streamDestroyed = nativeEvents.addListener( | ||
this.componentEvents.streamDestroyed, | ||
(stream) => { | ||
OT.removeSubscriber(stream.streamId, (error) => { | ||
if (error) { | ||
handleError(error); | ||
} else { | ||
const indexOfStream = this.state.streams.indexOf(stream.streamId); | ||
const newState = this.state.streams.slice(); | ||
newState.splice(indexOfStream, 1); | ||
this.setState({ | ||
streams: newState, | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
streamDestroyedHandler = (stream) => { | ||
OT.removeSubscriber(stream.streamId, (error) => { | ||
if (error) { | ||
handleError(error); | ||
} else { | ||
const indexOfStream = this.state.streams.indexOf(stream.streamId); | ||
const newState = this.state.streams.slice(); | ||
newState.splice(indexOfStream, 1); | ||
this.setState({ | ||
streams: newState, | ||
}); | ||
}, | ||
); | ||
} | ||
}); | ||
} | ||
render() { | ||
if (this.state.streams.length < 1) { | ||
return <View />; | ||
} | ||
const childrenWithStreams = this.state.streams.map(streamId => | ||
@@ -75,0 +63,0 @@ <OTSubscriberView key={streamId} streamId={streamId} style={this.props.style} />); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89817
652