1.0.1 • Published 5 years ago

redux-phoenix-middleware v1.0.1

Weekly downloads
4
License
MIT OR GPL-3.0-or...
Repository
github
Last release
5 years ago

redux-phoenix-middleware

Declarative automatic bidirectional maping between Redux Actions and Phoenix Channel Messages

Build Status Coverage Status

Example

import {
	createPhoenixReducer,
	createPhoenixMiddleware,
	phoenixSocketStateSelector,
	phoenixChannelStateSelector,
	phoenixEventActionType,
} from 'redux-phoenix-middleware';

const phoenixReducer = createPhoenixReducer();
const phoenixMiddleware = createPhoenixMiddleware({
	sockets: {
		socket: {
			endPoint: BASE_URL + '/socket',
			channels: {
				'room:lobby': true,
			},
		},
	},
});

// Keeps a log of messages sent to the `lobby` room
const messagesReducer = (state = [], action) => {
	// Got a channel event, could've been sent like `broadcast!(socket, "message", %{ ... })`
	if (action.type === phoenixEventActionType('socket', 'room:lobby', 'message')) {
		return state.concat(action.payload);
	}

	return state;
};

const store = createStore(combineReducers({
	messages: messagesReducer,
	phoenix: phoenixReducer,
}), initialState, applyMiddleware(phoenixMiddleware));

// Somwhere else:

const state = store.getState();

phoenixSocketStateSelector(state.phoenix, 'socket') === 'open'; // or 'closed'
phoenixChannelStateSelector(state.phoenix, 'socket', 'room:lobby') === 'joined'; // or 'closed', 'errored'

Install

yarn add redux-phoenix-middleware