Created
August 28, 2015 18:23
-
-
Save dhrrgn/c6c806f22dca6d7fa6eb to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* @providesModule PusherClient | |
*/ | |
"use strict"; | |
import React from 'react-native'; | |
var { | |
NativeAppEventEmitter, | |
NativeModules: { | |
PusherClient, | |
} | |
} = React; | |
import { Config, } from '../Utils/Constants'; | |
import Channels from './Channels'; | |
var PusherWillConnect = "pusherWillConnect", | |
PusherDidConnect = "pusherDidConnect", | |
PusherDidDisconnectWithError = "pusherDidDisconnectWithError", | |
PusherFailedWithError = "pusherFailedWithError", | |
PusherWillAutomaticallyReconnect = "pusherWillAutomaticallyReconnect"; | |
var noop = () => {}; | |
class Client { | |
constructor(apiKey) { | |
this.socketId = null; | |
this.connected = false; | |
this.channels = new Channels(); | |
this._setupEventListeners(); | |
PusherClient.connect(apiKey); | |
} | |
subscribe(name) { | |
var channel = this.channels.add(name); | |
channel.subscribe(); | |
return channel; | |
} | |
unsubscribe(name) { | |
this.channels.remove(name); | |
return this; | |
} | |
channel(name) { | |
var channel = this.rooms.channel(name); | |
if (!channel) { | |
channel = this.subscribe(name); | |
} | |
return channel; | |
} | |
onWillConnect() { | |
// noop | |
} | |
onDidConnect(socketId) { | |
this.socketId = socketId; | |
this.connected = true; | |
} | |
onDidDisconnectWithError(error) { | |
this.socketId = null; | |
this.connected = false; | |
} | |
onFailedWithError(error) { | |
this.connected = false; | |
} | |
_setupEventListeners() { | |
NativeAppEventEmitter.addListener(PusherWillConnect, this.onWillConnect.bind(this)); | |
NativeAppEventEmitter.addListener(PusherDidConnect, this.onDidConnect.bind(this)); | |
NativeAppEventEmitter.addListener(PusherDidDisconnectWithError, this.onDidDisconnectWithError.bind(this)); | |
NativeAppEventEmitter.addListener(PusherFailedWithError, this.onFailedWithError.bind(this)); | |
} | |
} | |
export default new Client(Config.pusher_api_key); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment