Skip to content

Commit 6e14647

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents bf0e81c + 904ff1f commit 6e14647

File tree

3 files changed

+72
-53
lines changed

3 files changed

+72
-53
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function reducer(state, action) {
3030
}
3131
}
3232
```
33-
This initial state object is an immutable.js Map that looks like this:
33+
This initial state object is an Object that looks like this:
3434

3535
```
3636
id: null,
@@ -39,15 +39,15 @@ This initial state object is an immutable.js Map that looks like this:
3939
authToken: null,
4040
authError: null,
4141
error: null,
42-
pendingSubs: List(),
43-
subs: List()
42+
pendingSubs: [],
43+
subs: []
4444
```
45-
###`reduxSocket(socketClusterOptions, hocOptions)` - a HOC to put on your highest level real-time component.
45+
###`reduxSocket(options, hocOptions)` - a HOC to put on your highest level real-time component.
4646
eg `@reduxSocket({authTokenName: 'MyApp.token'}, {keepAlive: 60000})`
4747

4848
For example, if you use websockets for everything, stick this on the main `app`.
4949
If only certain components have websockets, stick this on those containers.
50-
The `socketClusterOptions` are identical to the options you'd pass in to the client socketCluster
50+
The `options` are identical to what you'd pass in to the client socketCluster
5151
(http://socketcluster.io/#!/docs/api-socketcluster-client).
5252

5353
`hocOptions` has the following properties:
@@ -57,9 +57,9 @@ Say the client subs to 1000 items & accidently clicks a link that unmounts the c
5757
if they make it back to the component before the time expires, you won't have to start a new connection or resend
5858
those 1000 documents. Plus, any docs that came in while they were away will be there too. Neat!
5959
- `AuthEngine`: a class that takes in a redux `store` and creates a socket cluster `AuthEngine`.
60-
- `onConnect(socket)`: a callback that returns an active socket when a connection has been established.
60+
- `onConnect(options, hocOptions, socket)`: a callback that returns an active socket when a connection has been established.
6161
Useful if you want to do something like upgrade a transport from HTTP to sockets
62-
- `onDisconnect(timedOut, socket)`: a callback that returns a Boolean that is true due to `keepAlive` expiring
62+
- `onDisconnect(timedOut, options, hocOptions, socket)`: a callback that returns a Boolean that is true due to `keepAlive` expiring
6363
and the now inactive socket.
6464
Similar to above, this is useful if you want to downgrade something from a socket transport to HTTP.
6565

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@
4848
"redux": "^3.5.2",
4949
"rimraf": "^2.5.2"
5050
},
51+
"peerDependencies": {
52+
"socketcluster-client": "^4.3.19"
53+
},
5154
"dependencies": {
5255
"es6-promisify": "^4.1.0",
53-
"immutable": "^3.8.1",
54-
"react": "^15.1.0",
55-
"socketcluster-client": "^4.3.19"
56+
"react": "^15.1.0"
5657
}
5758
}

src/index.js

+61-43
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
// For now, socketcluster-client is a devDep because of https://github.com/npm/npm/issues/3081
2-
import socketCluster from 'socketcluster-client';
31
import React, {Component} from 'react';
42
import promisify from 'es6-promisify';
5-
import {Map, List} from 'immutable';
63

74
// constants
8-
const {CLOSED, CONNECTING, OPEN, AUTHENTICATED, PENDING, UNAUTHENTICATED} = socketCluster.SCSocket;
5+
const CLOSED = 'closed';
6+
const CONNECTING = 'connecting';
7+
const OPEN = 'open';
8+
const AUTHENTICATED = 'authenticated';
9+
const PENDING = 'pending';
10+
const UNAUTHENTICATED = 'unauthenticated';
11+
912
const CONNECT_REQUEST = '@@socketCluster/CONNECT_REQUEST';
1013
const CONNECT_SUCCESS = '@@socketCluster/CONNECT_SUCCESS';
1114
const CONNECT_ERROR = '@@socketCluster/CONNECT_ERROR';
@@ -21,78 +24,90 @@ const DISCONNECT = '@@socketCluster/DISCONNECT';
2124
const DEAUTHENTICATE = '@@socketCluster/DEAUTHENTICATE';
2225

2326
// Reducer
24-
const initialState = Map({
27+
const initialState = {
2528
id: null,
2629
socketState: CLOSED,
2730
authState: PENDING,
2831
authToken: null,
2932
authError: null,
3033
error: null,
31-
pendingSubs: List(),
32-
subs: List()
33-
});
34+
pendingSubs: [],
35+
subs: []
36+
};
3437

3538
export const socketClusterReducer = function (state = initialState, action) {
3639
switch (action.type) {
3740
case DEAUTHENTICATE:
38-
return state.merge({
41+
return {
42+
...state,
3943
authState: UNAUTHENTICATED,
4044
authToken: null
41-
});
45+
};
4246
case DISCONNECT:
4347
return initialState;
4448
case CONNECT_REQUEST:
45-
return state.merge({
49+
return {
50+
...state,
4651
socketState: CONNECTING
47-
});
52+
};
4853
case CONNECT_ERROR:
49-
return state.merge({
54+
return {
55+
...state,
5056
error: action.error
51-
});
57+
};
5258
case CONNECT_SUCCESS:
53-
return state.merge({
59+
return {
60+
...state,
5461
id: action.payload.id,
5562
socketState: action.payload.socketState,
5663
authState: action.payload.authState,
5764
error: action.error
58-
});
65+
};
5966
case AUTH_REQUEST:
60-
return state.merge({
67+
return {
68+
...state,
6169
authState: PENDING
62-
});
70+
};
6371
case AUTH_SUCCESS:
64-
return state.merge({
72+
return {
73+
...state,
6574
authState: AUTHENTICATED,
6675
authToken: action.payload.authToken
67-
});
76+
};
6877
case AUTH_ERROR:
69-
return state.merge({
78+
return {
79+
...state,
7080
authState: UNAUTHENTICATED,
7181
authError: action.error
72-
});
82+
};
7383
case SUBSCRIBE_REQUEST:
74-
return state.merge({
75-
pendingSubs: state.get('pendingSubs').push(action.payload.channelName)
76-
});
84+
return {
85+
...state,
86+
pendingSubs: state.pendingSubs.concat(action.payload.channelName)
87+
};
7788
case SUBSCRIBE_SUCCESS:
78-
return state.merge({
79-
pendingSubs: state.get('pendingSubs').filter(sub => sub !== action.payload.channelName),
80-
subs: state.get('subs').push(action.payload.channelName)
81-
});
89+
return {
90+
...state,
91+
pendingSubs: state.pendingSubs.filter(sub => sub !== action.payload.channelName),
92+
subs: state.subs.concat(action.payload.channelName)
93+
};
8294
case SUBSCRIBE_ERROR:
83-
return state.merge({
84-
pendingSubs: state.get('pendingSubs').filter(sub => sub !== action.payload.channelName),
95+
return {
96+
...state,
97+
pendingSubs: state.pendingSubs.filter(sub => sub !== action.payload.channelName),
8598
error: action.error
86-
});
99+
};
87100
case UNSUBSCRIBE:
88-
return state.merge({
89-
subs: state.get('subs').filter(sub => sub !== action.payload.channelName),
101+
return {
102+
...state,
103+
subs: state.subs.filter(sub => sub !== action.payload.channelName),
90104
error: action.error
91-
});
105+
};
92106
case KICKOUT:
93-
return state.merge({
107+
return {
108+
...state,
94109
error: action.error
95-
});
110+
};
96111
default:
97112
return state;
98113
}
@@ -127,13 +142,13 @@ export const reduxSocket = (options, hocOptions) => ComposedComponent =>
127142
this.handleError();
128143
this.handleSubs();
129144
this.handleAuth();
145+
const {onConnect} = this.hocOptions;
146+
if (onConnect) {
147+
onConnect(this.options, this.hocOptions, this.socket);
148+
}
130149
return;
131150
}
132151
clearTimeout(this.socket.__destructionCountdown);
133-
const {onConnect} = this.hocOptions;
134-
if (onConnect) {
135-
onConnect(this.socket);
136-
}
137152
}
138153

139154
componentWillUnmount() {
@@ -142,7 +157,7 @@ export const reduxSocket = (options, hocOptions) => ComposedComponent =>
142157
this.socket = this.socketCluster.destroy(options);
143158
const {onDisconnect} = this.hocOptions;
144159
if (onDisconnect) {
145-
onDisconnect(true, this.socket);
160+
onDisconnect(true, this.options, this.hocOptions, this.socket);
146161
}
147162
}, this.hocOptions.keepAlive);
148163
}
@@ -161,6 +176,9 @@ export const reduxSocket = (options, hocOptions) => ComposedComponent =>
161176
dispatch({type: SUBSCRIBE_REQUEST, payload: {channelName}});
162177
}
163178
});
179+
socket.on('subscribeRequest', channelName => {
180+
dispatch({type: SUBSCRIBE_REQUEST, payload: {channelName}});
181+
});
164182
socket.on('subscribe', channelName => {
165183
dispatch({type: SUBSCRIBE_SUCCESS, payload: {channelName}});
166184
});
@@ -212,7 +230,7 @@ export const reduxSocket = (options, hocOptions) => ComposedComponent =>
212230
const {onDisconnect} = this.hocOptions;
213231
if (onDisconnect) {
214232
// did not time out, so first param is false
215-
onDisconnect(false, this.socket);
233+
onDisconnect(false, this.options, this.hocOptions, this.socket);
216234
}
217235
});
218236
// triggers while in connecting state

0 commit comments

Comments
 (0)