mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Move listeners into their own class
This commit is contained in:
parent
5986b363a5
commit
95ad4adc8f
@ -5,7 +5,6 @@ import "phoenix_html";
|
|||||||
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import ReactDOM from "react-dom";
|
import ReactDOM from "react-dom";
|
||||||
import watch from "redux-watch";
|
|
||||||
import { createStore } from "redux";
|
import { createStore } from "redux";
|
||||||
|
|
||||||
import Channel from "./services/channel";
|
import Channel from "./services/channel";
|
||||||
@ -13,6 +12,7 @@ import Notifications from "./services/notifications";
|
|||||||
|
|
||||||
import chessBoardReducer from "./reducers/chess-board";
|
import chessBoardReducer from "./reducers/chess-board";
|
||||||
import { setGameId } from "./store/actions";
|
import { setGameId } from "./store/actions";
|
||||||
|
import Listeners from "./store/listeners";
|
||||||
|
|
||||||
import ChessBoard from "./components/chess-board";
|
import ChessBoard from "./components/chess-board";
|
||||||
import MoveList from "./components/move-list";
|
import MoveList from "./components/move-list";
|
||||||
@ -27,14 +27,8 @@ class App extends React.Component {
|
|||||||
|
|
||||||
store.dispatch(setGameId(gameId));
|
store.dispatch(setGameId(gameId));
|
||||||
|
|
||||||
let w = watch(store.getState, "turn");
|
this.listeners = new Listeners(store);
|
||||||
store.subscribe(w((newVal, oldVal, objectPath) => {
|
this.listeners.setListeners(notifications);
|
||||||
const player = store.getState().player;
|
|
||||||
|
|
||||||
if (oldVal != null && newVal == player) {
|
|
||||||
notifications.notifyTurn(player);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
this.channel = new Channel(store, gameId);
|
this.channel = new Channel(store, gameId);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,11 +4,15 @@ class Notifications {
|
|||||||
}
|
}
|
||||||
|
|
||||||
notifyTurn(player) {
|
notifyTurn(player) {
|
||||||
|
this.notify({
|
||||||
|
body: "Your opponent has moved.",
|
||||||
|
icon: `/images/king_${player}.svg`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
notify(options) {
|
||||||
if (!document.hasFocus()) {
|
if (!document.hasFocus()) {
|
||||||
new Notification("Chess", {
|
new Notification("Chess", options);
|
||||||
body: "Your opponent has moved.",
|
|
||||||
icon: `/images/king_${player}.svg`
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
assets/js/store/listeners.js
Normal file
27
assets/js/store/listeners.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import watch from "redux-watch";
|
||||||
|
|
||||||
|
class Listeners {
|
||||||
|
constructor(store) {
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
setListeners(notifications) {
|
||||||
|
this.notifications = notifications;
|
||||||
|
|
||||||
|
let watcher = watch(this.store.getState, "turn");
|
||||||
|
|
||||||
|
this.store.subscribe(
|
||||||
|
watcher(this.notifyTurn.bind(this))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyTurn(newVal, oldVal) {
|
||||||
|
const player = this.store.getState().player;
|
||||||
|
|
||||||
|
if (oldVal != null && newVal == player) {
|
||||||
|
this.notifications.notifyTurn(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Listeners;
|
||||||
Loading…
Reference in New Issue
Block a user