Skip to content

Commit

Permalink
[update] v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Daniel Xalambrí committed Mar 18, 2016
0 parents commit a461325
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [
"transform-es2015-modules-commonjs",
"transform-es2015-arrow-functions"
]
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
build
logs
*.log
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
lib
test
logs
*.log
.babelrc
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# socket.io-redux
Redux middleware to emit action via socket.io.

## API
```javascript
import io from 'socket.io-client';
import { createStore, applyMiddleware } from 'redux';

import socketIO from 'socket.io-redux';

import reducer from './reducer';

const store = createStore(reducer, applyMiddleware(
socketIO(io.connect(process.env.NODE_ENV))
));
```
* `socketIO` receive a `socket` instance created by `io.connect(url)`.
9 changes: 9 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const socketIO = socket => () => next => action => {
if (action.meta && action.meta.socket && action.meta.socket.channel) {
socket.emit(action.meta.socket.channel, action);
}

return next(action);
}

export default socketIO;
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "socket.io-redux",
"version": "1.0.0",
"description": "Redux middleware to emit action via socket.io.",
"main": "build/index.js",
"directories": {
"test": "test"
},
"scripts": {
"build": "babel lib --out-dir build",
"pretest": "npm run build",
"prepublish": "npm run test",
"test": "babel-node test/index.js | tap-spec"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sergiodxa/socket.io-redux.git"
},
"keywords": [
"redux",
"middleware",
"socket.io"
],
"author": "Sergio Daniel Xalambrí <[email protected]> (http://sergio.xalambri.com.ar/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/sergiodxa/socket.io-redux/issues"
},
"homepage": "http://sergio.xalambri.com.ar/socket.io-redux",
"devDependencies": {
"babel": "6.5.2",
"babel-cli": "6.6.5",
"babel-plugin-transform-es2015-arrow-functions": "6.5.2",
"babel-plugin-transform-es2015-modules-commonjs": "6.7.0",
"tap-spec": "4.1.1",
"tape": "4.5.1"
}
}
27 changes: 27 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import test from 'tape';
import socketIO from '../build/index';

function next(action) { return action; }

test('socket.io middleware', t => {
t.plan(2);

const testAction = {
type: 'ADD_NEW',
payload: 'hello world!',
meta: {
socket: {
channel: 'add:new',
},
},
};

const socket = {
emit(channel, data) {
t.equals(channel, 'add:new', 'it should have the channel "add:new"');
t.deepEquals(data, testAction, 'it should have the action as data');
}
}

socketIO(socket)()(next)(testAction);
});

0 comments on commit a461325

Please sign in to comment.