Skip to content
Open
Show file tree
Hide file tree
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
193 changes: 193 additions & 0 deletions app/api/faye.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import Faye from 'react-native-halley'
const noop = () => {}
import {NetInfo} from 'react-native'

class ClientAuthExt {
constructor(token) {
this._token = token
}

outgoing(message, cb) {
if (message.channel === '/meta/handshake') {
if (!message.ext) { message.ext = {}; }
message.ext.token = this._token
message.ext.realtimeLibrary = 'halley'
}

cb(message);
}

incoming(message, cb) {
if (message.channel === '/meta/handshake') {
if (message.successful) {
console.log('Successfuly subscribed: ', message);
} else {
console.log('Something went wrong: ', message.error);
}
}

cb(message)
}
}

class LogExt {
outgoing(message, cb) {
console.log('Log outgoing message: ', message)
cb(message)
}

incoming(message, cb) {
console.log('Log incoming message: ', message)
cb(message)
}
}

class SnapshotExt {
constructor(fn) {
this._handler = fn
}
incoming(message, cb) {
if (message.channel === '/meta/subscribe' && message.ext && message.ext.snapshot) {
this._handler(message)
}

cb(message)
}
}


export default class HalleyClient {
constructor({token, snapshotHandler}) {
this._client = new Faye.Client('https://ws.gitter.im/bayeux', {
timeout: 3000,
retry: 3000,
interval: 0,
maxNetworkDelay: 3000,
connectTimeout: 3000,
disconnectTimeout: 1000
})
this._token = token
this._snapshotHandler = snapshotHandler
this._subsciptions = []
this._isConnectedToNetwork = true
this._transport = true

this.setupNetworkListeners()
this.setupInternetListener()
}

setToken(token) {
this._token = token
}

setSnapshotHandler(fn) {
this._snapshotHandler = fn
}

setup() {
if (!this._token) {
throw new Error('You need to add token')
}

this._snapshotExt = new SnapshotExt(this._snapshotHandler || noop)
this._authExt = new ClientAuthExt(this._token)
}

create() {
if (!this._token) {
throw new Error('You need to add token')
}
this._client.addExtension(this._authExt)
this._client.addExtension(this._snapshotExt)
this._client.addExtension(new LogExt())
}

subscribe({type, url, handler}) {
return new Promise((res, rej) => {
if (this._checkSubscriptionAlreadyExist({type, url})) {
rej(`Subscription with type ${type} and url ${url} already exist.`)
}

const subscriptionObject = this._client.subscribe(url, handler)

subscriptionObject
.then(() => {
this._subsciptions.push({
type,
url,
handler,
subscriptionObject
})
res(true)
})
// .catch(err => {
// rej(err)
// })
})
}

unsubscribe({type, url}) {
return new Promise((res, rej) => {
const subscription = this._findSubscription({type, url})
if (!subscription) {
rej(`There is no subscription with type ${type} and url ${url}`)
} else {
subscription.subscriptionObject.unsubscribe()
.then(() => {
this._removeSubscription(subscription)
res(true)
})
// .catch(err => rej(err))
}
})
}

setupNetworkListeners() {
// this._client.on('transport:down', () => {
// this._transport = false
// })
//
// this._client.on('transport:up', () => {
// this._transport = true
// })
}

setupInternetListener() {
// NetInfo.isConnected.addEventListener(
// 'change',
// () => {
// debugger
// NetInfo.isConnected.fetch()
// .then(isConnected => {
// if (isConnected) {
// this._client.connect()
// // this._isConnectedToNetwork = true
// } else {
// // this._isConnectedToNetwork = false
// }
// })
// }
// )
}

_checkSubscriptionAlreadyExist(subscriptionOptions) {
const subscription = this._findSubscription(subscriptionOptions)
return !!subscription
}

_findSubscription({type, url}) {
return this._subsciptions.find(
item => item.type === type && item.url === url
)
}

_removeSubscription({type, url}) {
const index = this._subsciptions.indexOf(
item => item.type === type && item.url === url
)

if (index !== -1) {
this._subsciptions.splice(index, 1)
}
}
}
25 changes: 13 additions & 12 deletions app/modules/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {getRooms, getSuggestedRooms} from './rooms'
import {initializeUi} from './ui'
import {NetInfo, AppState} from 'react-native'
import {
setupFayeEvents,
// setupFayeEvents,
setupFaye,
onNetStatusChangeFaye,
subscribeToChatMessages,
Expand Down Expand Up @@ -43,7 +43,7 @@ export function init() {
return
}

dispatch(setupFayeEvents())
// dispatch(setupFayeEvents())
dispatch({ type: INITIALIZED, token })

// getting base current user's information
Expand All @@ -55,6 +55,7 @@ export function init() {
])

rootNavigator.startAppWithScreen({screen: 'gm.Home', showDrawer: true})
dispatch(subscribeToRooms())
dispatch(setupNetStatusListener())
// if you need debug room screen, just comment nevigation to 'hone'
// and uncomment navigation to 'room'
Expand All @@ -75,7 +76,7 @@ function setupNetStatusListener() {
NetInfo.isConnected.addEventListener('change',
async status => {
dispatch({type: CHANGE_NET_STATUS, payload: status})
await dispatch(onNetStatusChangeFaye(status))
// await dispatch(onNetStatusChangeFaye(status))
}
);
}
Expand All @@ -93,15 +94,15 @@ function setupAppStatusListener() {
if (!token.length || !netStatus) {
return
}
const {fayeConnected} = getState().app
if (!fayeConnected) {
await setupFaye()
}

const {activeRoom} = getState().rooms
if (!!activeRoom) {
dispatch(subscribeToChatMessages(activeRoom))
}
// const {fayeConnected} = getState().app
// if (!fayeConnected) {
// await setupFaye()
// }

// const {activeRoom} = getState().rooms
// if (!!activeRoom) {
// dispatch(subscribeToChatMessages(activeRoom))
// }
dispatch(subscribeToRooms())
} catch (error) {
// console.log(error)
Expand Down
Loading