mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Refactor channel service
This commit is contained in:
parent
fe0c58917f
commit
1c6ff47d8b
@ -8,40 +8,25 @@ import ReactDOM from "react-dom";
|
|||||||
import { createStore } from "redux";
|
import { createStore } from "redux";
|
||||||
import { Provider } from "react-redux";
|
import { Provider } from "react-redux";
|
||||||
|
|
||||||
import chessBoardReducer from "./reducers/chess-board";
|
|
||||||
|
|
||||||
const store = createStore(chessBoardReducer);
|
|
||||||
|
|
||||||
import { setPlayer, setGame, setGameId } from "./store/actions";
|
|
||||||
|
|
||||||
import API from "./services/api";
|
|
||||||
import Channel from "./services/channel";
|
import Channel from "./services/channel";
|
||||||
|
|
||||||
|
import chessBoardReducer from "./reducers/chess-board";
|
||||||
|
import { setPlayer, setGame, setGameId } from "./store/actions";
|
||||||
import ChessBoard from "./components/chess-board";
|
import ChessBoard from "./components/chess-board";
|
||||||
|
|
||||||
|
const store = createStore(chessBoardReducer);
|
||||||
|
|
||||||
class App extends React.Component {
|
class App extends React.Component {
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
const { gameId, store } = this.props;
|
const { gameId, store } = this.props;
|
||||||
|
|
||||||
store.dispatch(setGameId(gameId));
|
store.dispatch(setGameId(gameId));
|
||||||
|
|
||||||
this.channel = Channel.gameChannel(gameId);
|
this.channel = new Channel(store, gameId);
|
||||||
|
|
||||||
this.channel.on("game:update", data => {
|
|
||||||
if (data.player != undefined) {
|
|
||||||
store.dispatch(setPlayer(data.player));
|
|
||||||
};
|
|
||||||
store.dispatch(setGame(data));
|
|
||||||
});
|
|
||||||
|
|
||||||
this.channel.join()
|
|
||||||
.receive("error", resp => {
|
|
||||||
console.log("Unable to join", resp);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMove(gameId, move) {
|
sendMove(gameId, move) {
|
||||||
this.channel.push("game:move", move);
|
this.channel.sendMove(move);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
@ -1,11 +1,34 @@
|
|||||||
import socket from "../socket";
|
import socket from "../socket";
|
||||||
|
import { setPlayer, setGame } from "../store/actions";
|
||||||
|
|
||||||
const Channel = {
|
class Channel {
|
||||||
gameChannel: (gameId) => {
|
constructor(store, gameId) {
|
||||||
const channel = socket.channel(`game:${gameId}`, {});
|
this.store = store;
|
||||||
|
this.channel = socket.channel(`game:${gameId}`, {});
|
||||||
|
|
||||||
return channel;
|
this.join();
|
||||||
},
|
this.subscribe();
|
||||||
|
}
|
||||||
|
|
||||||
|
join() {
|
||||||
|
this.channel.join()
|
||||||
|
.receive("error", resp => {
|
||||||
|
console.log("Unable to join", resp);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribe() {
|
||||||
|
this.channel.on("game:update", data => {
|
||||||
|
if (data.player != undefined) {
|
||||||
|
this.store.dispatch(setPlayer(data.player));
|
||||||
};
|
};
|
||||||
|
this.store.dispatch(setGame(data));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMove(move) {
|
||||||
|
this.channel.push("game:move", move);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default Channel;
|
export default Channel;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user