Skip to content

Commit 018bba7

Browse files
Eduardo Garcia Sanzntamas
Eduardo Garcia Sanz
authored andcommitted
1.0.0-beta.0
1 parent f7bba3b commit 018bba7

File tree

12 files changed

+192
-382
lines changed

12 files changed

+192
-382
lines changed

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": [
3+
"react",
4+
"es2015",
5+
"stage-2"
6+
]
7+
}

.eslintrc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"parser": "babel-eslint",
3+
"env" : {
4+
"browser": true
5+
},
6+
"rules" : {
7+
"no-class-assign" : 2,
8+
"no-confusing-arrow" : 0,
9+
"no-const-assign" : 2,
10+
"no-dupe-class-members" : 2,
11+
"no-new-symbol" : 2,
12+
"no-this-before-super" : 2,
13+
"no-useless-constructor" : 2,
14+
"no-var" : 2,
15+
"arrow-body-style" : [2, "as-needed"],
16+
"arrow-parens" : 0,
17+
"arrow-spacing" : [
18+
2,
19+
{
20+
"before": true,
21+
"after" : true
22+
}
23+
],
24+
"constructor-super" : 2,
25+
"object-shorthand" : [2, "always"],
26+
"prefer-arrow-callback" : 2,
27+
"prefer-const" : 2,
28+
"prefer-rest-params" : 2,
29+
"prefer-spread" : 2,
30+
"prefer-template" : 1,
31+
"template-curly-spacing" : [2, "always"],
32+
"strict" : 0,
33+
"new-cap" : 0,
34+
"eqeqeq" : 2,
35+
"curly" : 2,
36+
"quotes" : [1, "single"],
37+
"no-unreachable" : 2,
38+
"space-before-blocks" : 2,
39+
"space-before-function-paren": 2,
40+
"no-multi-spaces" : 0,
41+
"key-spacing" : 0,
42+
"no-mixed-requires" : 0
43+
}
44+
}

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
node_modules
2+
.DS_Store
3+
.idea
24
*.log
5+
.nyc_output
6+
.nyc_cache
7+
/coverage
8+
/lib/*
9+
!/lib/.gitkeep

lib/.gitkeep

Whitespace-only changes.

package.json

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,49 @@
11
{
2-
"name" : "react-socket",
3-
"description" : "A neat way of handling views on other components",
4-
"homepage" : "https://github.com/coma/react-socket",
5-
"repository" : "https://github.com/coma/react-socket",
6-
"main" : "./src/index.js",
7-
"version" : "0.1.4",
8-
"license" : "MIT",
9-
"private" : false,
10-
"authors" : [
11-
"Eduardo García Sanz <[email protected]>"
12-
],
13-
"keywords" : [
2+
"name" : "react-socket",
3+
"description" : "A neat way of handling views on other components",
4+
"homepage" : "https://github.com/coma/react-socket",
5+
"repository" : {
6+
"type": "git",
7+
"url" : "git://github.com/coma/react-socket.git"
8+
},
9+
"bugs" : {
10+
"url" : "https://github.com/coma/react-socket/issues",
11+
"email": "[email protected]"
12+
},
13+
"author" : {
14+
"name" : "Eduardo García Sanz",
15+
"email": "[email protected]"
16+
},
17+
"main" : "lib",
18+
"version" : "1.0.0-beta.0",
19+
"license" : "MIT",
20+
"private" : false,
21+
"keywords" : [
1422
"react",
1523
"socket.io",
1624
"react-component"
1725
],
18-
"dependencies" : {
19-
"react" : "^15.0.1",
20-
"socket.io-client": "^1.4.6"
26+
"peerDependencies": {
27+
"react": "^15.3.1"
2128
},
22-
"devDependencies": {
23-
"proxyquire": "1.5",
24-
"jsdom" : "5.4",
25-
"mocha" : "2.2",
26-
"should" : "7.0",
27-
"eslint" : "0.24"
29+
"dependencies" : {
30+
"socket.io-client": "^1.4.8"
2831
},
29-
"scripts" : {
30-
"lint": "eslint src",
31-
"test": "npm run lint && mocha"
32+
"devDependencies" : {
33+
"babel-cli" : "^6.7.5",
34+
"babel-core" : "^6.7.6",
35+
"babel-eslint" : "^6.0.3",
36+
"babel-preset-es2015" : "^6.6.0",
37+
"babel-preset-react" : "^6.5.0",
38+
"babel-preset-stage-2": "^6.5.0",
39+
"eslint" : "^3.4.0",
40+
"react" : "^15.3.1"
3241
},
33-
"eslintConfig" : {
34-
"env" : {
35-
"node": true
36-
},
37-
"rules": {
38-
"strict" : 0,
39-
"new-cap" : 0,
40-
"eqeqeq" : 2,
41-
"curly" : 2,
42-
"quotes" : [
43-
1,
44-
"single"
45-
],
46-
"no-unreachable" : 2,
47-
"space-before-blocks" : 2,
48-
"space-before-function-paren": 2,
49-
"no-multi-spaces" : 0,
50-
"key-spacing" : 0,
51-
"no-mixed-requires" : 0
52-
}
42+
"scripts" : {
43+
"lint" : "eslint src",
44+
"test" : "npm run lint",
45+
"clean" : "rm -rf lib/*.*",
46+
"build" : "npm run clean && babel src -d lib",
47+
"prepublish": "npm run build"
5348
}
5449
}

src/event.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
2-
Socket: require('./socket'),
3-
Event : require('./event')
4-
};
1+
import Socket from './socket';
2+
import Listener from './listener';
3+
4+
export default {Socket, Listener};

src/listener.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, { Component, PropTypes } from 'react';
2+
import { NAME, get } from './socket';
3+
4+
class Listener extends Component {
5+
6+
componentWillMount () {
7+
8+
const {socket, event, callback} = this.props;
9+
10+
this.socket = get(socket);
11+
this.socket.on(event, callback);
12+
}
13+
14+
componentWillUnmount () {
15+
16+
const {event, callback} = this.props;
17+
18+
this.socket.off(event, callback);
19+
}
20+
21+
render () {
22+
23+
return false;
24+
}
25+
}
26+
27+
Listener.displayName = 'SocketListener';
28+
29+
Listener.propTypes = {
30+
socket : PropTypes.string,
31+
event : PropTypes.string.isRequired,
32+
callback: PropTypes.func.isRequired
33+
};
34+
35+
Listener.defaultProps = {
36+
socket: NAME
37+
};
38+
39+
export default Listener;

src/socket.js

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
1-
var React = require('react'),
2-
io = require('socket.io-client');
3-
4-
var sockets = {};
5-
6-
module.exports = React.createClass({
7-
displayName : 'Socket',
8-
propTypes : {
9-
url : React.PropTypes.string,
10-
name : React.PropTypes.string,
11-
options: React.PropTypes.shape({
12-
reconnection : React.PropTypes.bool,
13-
reconnectionAttempts: React.PropTypes.number,
14-
reconnectionDelay : React.PropTypes.number,
15-
reconnectionDelayMax: React.PropTypes.number,
16-
randomizationFactor : React.PropTypes.number,
17-
timeout : React.PropTypes.number
18-
})
19-
},
20-
getDefaultProps : function () {
21-
22-
return {
23-
name : 'default',
24-
options: {
25-
forceNew: true
26-
}
27-
};
28-
},
29-
statics : {
30-
socket: function (name) {
31-
32-
name = name || 'default';
33-
34-
if (!sockets.hasOwnProperty(name)) {
35-
36-
throw new Error('There is no "' + name + '" socket mounted.');
37-
}
38-
39-
return sockets[name];
40-
}
41-
},
42-
componentWillMount : function () {
1+
import React, { Component, PropTypes } from 'react';
2+
3+
export const NAME = 'default';
4+
5+
const SOCKETS = {};
6+
7+
export const has = name => SOCKETS.hasOwnProperty(name);
8+
9+
export const get = (name = NAME) => {
10+
11+
if (!has(name)) {
12+
13+
throw new Error(`There is no "${ name }" socket mounted.`);
14+
}
15+
16+
return SOCKETS[name];
17+
};
18+
19+
class Socket extends Component {
20+
21+
componentWillMount () {
4322

44-
if (sockets.hasOwnProperty(this.props.name)) {
23+
const {name, url, options} = this.props;
4524

46-
throw new Error('Another "' + this.props.name + '" socket is already mounted.');
25+
if (has(name)) {
26+
27+
throw new Error(`Another "${ name }" socket is already mounted.`);
4728
}
4829

49-
sockets[this.props.name] = io(this.props.url, this.props.options);
50-
},
51-
componentWillUnmount: function () {
30+
SOCKETS[name] = io(url, options);
31+
}
32+
33+
componentWillUnmount () {
34+
35+
const {name} = this.props;
36+
37+
SOCKETS[name].disconnect();
38+
delete SOCKETS[name];
39+
}
5240

53-
sockets[this.props.name].disconnect();
54-
delete sockets[this.props.name];
55-
},
56-
render : function () {
41+
render () {
5742

5843
return false;
5944
}
60-
});
45+
}
46+
47+
Socket.displayName = 'Socket';
48+
49+
Socket.propTypes = {
50+
url : PropTypes.string,
51+
name : PropTypes.string,
52+
options: PropTypes.object
53+
};
54+
55+
Socket.defaultProps = {
56+
url : '/',
57+
name: NAME
58+
};
59+
60+
export default Socket;

0 commit comments

Comments
 (0)