Skip to content

Commit a461325

Browse files
author
Sergio Daniel Xalambrí
committed
[update] v1.0.1
0 parents  commit a461325

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

.babelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": [
3+
"transform-es2015-modules-commonjs",
4+
"transform-es2015-arrow-functions"
5+
]
6+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
build
3+
logs
4+
*.log

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
lib
3+
test
4+
logs
5+
*.log
6+
.babelrc

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# socket.io-redux
2+
Redux middleware to emit action via socket.io.
3+
4+
## API
5+
```javascript
6+
import io from 'socket.io-client';
7+
import { createStore, applyMiddleware } from 'redux';
8+
9+
import socketIO from 'socket.io-redux';
10+
11+
import reducer from './reducer';
12+
13+
const store = createStore(reducer, applyMiddleware(
14+
socketIO(io.connect(process.env.NODE_ENV))
15+
));
16+
```
17+
* `socketIO` receive a `socket` instance created by `io.connect(url)`.

lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const socketIO = socket => () => next => action => {
2+
if (action.meta && action.meta.socket && action.meta.socket.channel) {
3+
socket.emit(action.meta.socket.channel, action);
4+
}
5+
6+
return next(action);
7+
}
8+
9+
export default socketIO;

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "socket.io-redux",
3+
"version": "1.0.0",
4+
"description": "Redux middleware to emit action via socket.io.",
5+
"main": "build/index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"scripts": {
10+
"build": "babel lib --out-dir build",
11+
"pretest": "npm run build",
12+
"prepublish": "npm run test",
13+
"test": "babel-node test/index.js | tap-spec"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/sergiodxa/socket.io-redux.git"
18+
},
19+
"keywords": [
20+
"redux",
21+
"middleware",
22+
"socket.io"
23+
],
24+
"author": "Sergio Daniel Xalambrí <[email protected]> (http://sergio.xalambri.com.ar/)",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/sergiodxa/socket.io-redux/issues"
28+
},
29+
"homepage": "http://sergio.xalambri.com.ar/socket.io-redux",
30+
"devDependencies": {
31+
"babel": "6.5.2",
32+
"babel-cli": "6.6.5",
33+
"babel-plugin-transform-es2015-arrow-functions": "6.5.2",
34+
"babel-plugin-transform-es2015-modules-commonjs": "6.7.0",
35+
"tap-spec": "4.1.1",
36+
"tape": "4.5.1"
37+
}
38+
}

test/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import test from 'tape';
2+
import socketIO from '../build/index';
3+
4+
function next(action) { return action; }
5+
6+
test('socket.io middleware', t => {
7+
t.plan(2);
8+
9+
const testAction = {
10+
type: 'ADD_NEW',
11+
payload: 'hello world!',
12+
meta: {
13+
socket: {
14+
channel: 'add:new',
15+
},
16+
},
17+
};
18+
19+
const socket = {
20+
emit(channel, data) {
21+
t.equals(channel, 'add:new', 'it should have the channel "add:new"');
22+
t.deepEquals(data, testAction, 'it should have the action as data');
23+
}
24+
}
25+
26+
socketIO(socket)()(next)(testAction);
27+
});

0 commit comments

Comments
 (0)