Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Created August 28, 2015 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhrrgn/c6c806f22dca6d7fa6eb to your computer and use it in GitHub Desktop.
Save dhrrgn/c6c806f22dca6d7fa6eb to your computer and use it in GitHub Desktop.
/**
* @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