Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pusher-redux

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pusher-redux - npm Package Compare versions

Comparing version 0.0.1 to 0.1.1

pusher-redux.js

114

index.js
'use strict';
var Pusher = require('pusher-js');
// on the one hand having global state is ugly, on the other it is easier to use it from anywhere
var config = {
socket: null,
store: null,
apiKey: null,
options: {},
subscriptions: {}
};
// create redux action
var pusherAction = function pusherAction(_ref) {
var actionType = _ref.actionType;
var channelName = _ref.channelName;
var eventName = _ref.eventName;
var data = _ref.data;
return {
type: actionType,
channel: channelName,
event: eventName,
data: data
};
};
// we need to wait before pusher connects until we can subscribe
// so gonna queue actions here
var pendingFunctions = [];
var isConnected = false;
var addToQueue = function addToQueue(func) {
pendingFunctions.push(func);
runPending();
};
var successfullyConnected = function successfullyConnected() {
isConnected = true;
runPending();
};
var runPending = function runPending() {
// that's like a promise, but I don't want to depend on promises
while (isConnected && pendingFunctions.length > 0) {
pendingFunctions.shift()();
}
};
module.exports.configurePusher = function (store, apiKey) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
config.socket = new Pusher(apiKey, options);
config.store = store;
config.apiKey = apiKey;
config.socket.connection.bind('connected', successfullyConnected);
};
module.exports.delayConfiguration = function (store, apiKey) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
config.store = store;
config.apiKey = apiKey;
Object.assign(config.options, options);
};
module.exports.startConfiguration = function () {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
config.socket = new Pusher(config.apiKey, Object.assign({}, config.options, options));
config.socket.connection.bind('connected', successfullyConnected);
};
module.exports.subscribe = function (channelName, eventName, actionType) {
addToQueue(function () {
var channel = config.socket.channel(channelName) || config.socket.subscribe(channelName);
if (!config.subscriptions[channelName]) {
config.subscriptions[channelName] = {};
}
var channelSubs = config.subscriptions[channelName];
if (!channelSubs[eventName]) {
channelSubs[eventName] = {};
}
var eventSubs = channelSubs[eventName];
if (!eventSubs[actionType]) {
eventSubs[actionType] = function (data) {
config.store.dispatch(pusherAction({ actionType: actionType, channelName: channelName, eventName: eventName, data: data }));
};
channel.bind(eventName, eventSubs[actionType]);
}
});
};
module.exports.unsubscribe = function (channelName, eventName, actionType) {
addToQueue(function () {
var channel = config.socket.channel(channelName);
if (!channel) {
console.log('Not subscribed to \'' + channelName + '\'');
return;
}
var channelSubs = config.subscriptions[channelName];
if (!channelSubs[eventName]) {
console.log('Not subscribed event \'' + eventName + '\' from \'' + channelName + '\'');
return;
}
var eventSubs = channelSubs[eventName];
if (!eventSubs[actionType]) {
console.log('Handler ' + actionType + ' not registered for event \'' + eventName + '\' from \'' + channelName + '\'');
return;
}
channel.unbind(eventName, eventSubs[actionType]);
delete eventSubs[actionType];
});
};
var pusherRedux = require('./pusher-redux');
pusherRedux.setPusherClient(Pusher);
module.exports = pusherRedux;
{
"name": "pusher-redux",
"version": "0.0.1",
"version": "0.1.1",
"description": "Integration of Pusher into Redux",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -67,3 +67,3 @@ pusher-redux

...
function orderReducer(state = initialState.orders), action) {
function orderReducer(state = initialState.orders, action) {
case NEW_ORDER:

@@ -120,2 +120,11 @@ return [...state, action.data.order];

## React Native
If you want to use react-native then replace ALL imports of `pusher-redux` with `pusher-redux/react-native`
e.g.
<pre>
<code>
import { startConfiguration } from 'pusher-redux/react-native';
</code>
</pre>
### Options

@@ -122,0 +131,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc