Browse Source

Convert socket reducer state to an immutable record.

pull/1/head
Titouan Rigoudy 9 years ago
parent
commit
2712422837
5 changed files with 19 additions and 15 deletions
  1. +4
    -4
      src/components/ConnectForm.js
  2. +1
    -1
      src/containers/ConnectPage.js
  3. +3
    -3
      src/containers/Footer.js
  4. +1
    -1
      src/createRoutes.js
  5. +10
    -6
      src/reducers/socket.js

+ 4
- 4
src/components/ConnectForm.js View File

@ -12,7 +12,7 @@ const ConnectForm = (props) => {
return actions.socket.open(values.url, actions.socketHandlers); return actions.socket.open(values.url, actions.socketHandlers);
}); });
const isSocketClosed = socket.get("state") === STATE_CLOSED;
const isSocketClosed = socket.state === STATE_CLOSED;
return ( return (
<div id="connect-form"> <div id="connect-form">
@ -25,8 +25,8 @@ const ConnectForm = (props) => {
</button> </button>
</form> </form>
<SocketStatusPane <SocketStatusPane
state={socket.get("state")}
url={socket.get("url")}
state={socket.state}
url={socket.url}
/> />
</div> </div>
); );
@ -35,7 +35,7 @@ const ConnectForm = (props) => {
ConnectForm.propTypes = { ConnectForm.propTypes = {
fields: PropTypes.object.isRequired, fields: PropTypes.object.isRequired,
handleSubmit: PropTypes.func.isRequired, handleSubmit: PropTypes.func.isRequired,
socket: ImmutablePropTypes.map.isRequired,
socket: ImmutablePropTypes.record.isRequired,
actions: PropTypes.object.isRequired actions: PropTypes.object.isRequired
}; };


+ 1
- 1
src/containers/ConnectPage.js View File

@ -31,7 +31,7 @@ class ConnectPage extends React.Component {
getLoginStatusOrRedirect(props) { getLoginStatusOrRedirect(props) {
const { actions, login, router, socket } = props; const { actions, login, router, socket } = props;
if (socket.get("state") === STATE_OPEN)
if (socket.state === STATE_OPEN)
{ {
const loginStatus = login.get("status"); const loginStatus = login.get("status");
switch (loginStatus) { switch (loginStatus) {


+ 3
- 3
src/containers/Footer.js View File

@ -9,8 +9,8 @@ const Footer = ({ login, socket }) => {
return ( return (
<footer> <footer>
<SocketStatusPane <SocketStatusPane
state={socket.get("state")}
url={socket.get("url")}
state={socket.state}
url={socket.url}
/> />
<LoginStatusPane <LoginStatusPane
status={login.get("status")} status={login.get("status")}
@ -24,7 +24,7 @@ const Footer = ({ login, socket }) => {
Footer.propTypes = { Footer.propTypes = {
login: ImmutablePropTypes.map.isRequired, login: ImmutablePropTypes.map.isRequired,
socket: ImmutablePropTypes.map.isRequired
socket: ImmutablePropTypes.record.isRequired
}; };
const mapStateToProps = ({ socket, login }) => ({ socket, login }); const mapStateToProps = ({ socket, login }) => ({ socket, login });


+ 1
- 1
src/createRoutes.js View File

@ -13,7 +13,7 @@ import { LOGIN_STATUS_SUCCESS } from "./constants/login";
const createRoutes = (store) => { const createRoutes = (store) => {
const requireLoggedIn = (nextState, replaceState) => { const requireLoggedIn = (nextState, replaceState) => {
let { socket, login } = store.getState(); let { socket, login } = store.getState();
if (socket.get("state") !== STATE_OPEN ||
if (socket.state !== STATE_OPEN ||
login.get("status") !== LOGIN_STATUS_SUCCESS) login.get("status") !== LOGIN_STATUS_SUCCESS)
{ {
replaceState({}, "/"); replaceState({}, "/");


+ 10
- 6
src/reducers/socket.js View File

@ -7,14 +7,18 @@ import {
import ControlRequest from "../utils/ControlRequest"; import ControlRequest from "../utils/ControlRequest";
const initialState = Immutable.Map({
state: STATE_CLOSED
const SocketRecord = Immutable.Record({
state: STATE_CLOSED,
socket: undefined,
url: undefined
}); });
const initialState = new SocketRecord();
export default (state = initialState, { type, payload }) => { export default (state = initialState, { type, payload }) => {
const sendRequest = (controlRequest) => { const sendRequest = (controlRequest) => {
try { try {
state.get("socket").send(JSON.stringify(controlRequest));
state.socket.send(JSON.stringify(controlRequest));
} catch (err) { } catch (err) {
console.log(`Socket error: failed to send ${controlRequest}`); console.log(`Socket error: failed to send ${controlRequest}`);
} }
@ -23,7 +27,7 @@ export default (state = initialState, { type, payload }) => {
switch (type) { switch (type) {
case types.SOCKET_SET_OPENING: case types.SOCKET_SET_OPENING:
{ {
if (state.get("state") !== STATE_CLOSED) {
if (state.state !== STATE_CLOSED) {
console.log("Cannot open socket, already open"); console.log("Cannot open socket, already open");
return state; return state;
} }
@ -46,7 +50,7 @@ export default (state = initialState, { type, payload }) => {
case types.SOCKET_SET_CLOSING: case types.SOCKET_SET_CLOSING:
// Ooh bad stateful reducing... // Ooh bad stateful reducing...
state.get("socket").close();
state.socket.close();
return state.set("state", STATE_CLOSING); return state.set("state", STATE_CLOSING);
case types.SOCKET_SET_CLOSED: case types.SOCKET_SET_CLOSED:
@ -54,7 +58,7 @@ export default (state = initialState, { type, payload }) => {
case types.SOCKET_SET_ERROR: case types.SOCKET_SET_ERROR:
console.log("Socket error"); console.log("Socket error");
return state.set("state", state.get("socket").readyState);
return state.set("state", state.socket.readyState);
case types.LOGIN_GET_STATUS: case types.LOGIN_GET_STATUS:
sendRequest(ControlRequest.loginStatus()); sendRequest(ControlRequest.loginStatus());


Loading…
Cancel
Save