Skip to content

Commit

Permalink
Replace Array.prototype.find (ovidiuch#37)
Browse files Browse the repository at this point in the history
* Add placeholder config for not minifying prod code

* Replace Array.prototype.find

* Log component errors
  • Loading branch information
ovidiuch authored Mar 5, 2018
1 parent 167c204 commit 18a875c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"jest.framework-setup.js",
"cosmos.config.js",
"cosmos.test.js",
"next.config.js",
"server.js",
"server/**/*.js"
],
Expand Down
3 changes: 3 additions & 0 deletions components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, { Component } from 'react';
import { connect } from 'react-redux';
import Head from 'next/head';
import Error from './pages/Error';
import { logError } from '../utils/rollbar-client';

import type { Node } from 'react';
import type { State } from '../types/state';
Expand Down Expand Up @@ -40,6 +41,8 @@ class Layout extends Component<Props, LocalState> {
}

componentDidCatch(error, { componentStack }) {
logError(error);

this.setState({
error: {
message: error.toString(),
Expand Down
10 changes: 10 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
webpack(cfg) {
// XXX: Uncomment to generate unminified production code
// cfg.plugins = cfg.plugins.filter(
// plugin => plugin.constructor.name !== 'UglifyJsPlugin'
// );

return cfg;
}
};
5 changes: 3 additions & 2 deletions reducers/game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow

import { find } from 'lodash';
import {
WELL_ROWS,
WELL_COLS,
Expand Down Expand Up @@ -460,7 +461,7 @@ export function isPlayer(game: Game, curUser: ?User): boolean {
}

export function getPlayer(game: Game, userId: UserId): Player {
const player = game.players.find(p => p.user.id === userId);
const player = find(game.players, p => p.user.id === userId);

if (!player) {
throw new Error(`Player with userId ${userId} does not exist`);
Expand All @@ -481,7 +482,7 @@ export function getCurPlayer(game: Game, curUser: ?User): Player {

export function getOtherPlayer(game: Game, curPlayer: Player): ?Player {
// NOTE: This only works with max 2 players per game
return game.players.find(p => p !== curPlayer);
return find(game.players, p => p !== curPlayer);
}

export function allPlayersReady(game: Game) {
Expand Down
4 changes: 3 additions & 1 deletion server/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow

import { find } from 'lodash';
import {
users,
sessions,
Expand Down Expand Up @@ -140,7 +141,8 @@ function extractBackfillRequest(req: mixed): BackfillRequest {

function getBackfillActions(req: BackfillRequest): BackfillResponse {
return gameActions[req.gameId].filter(action => {
const player = req.players.find(
const player = find(
req.players,
({ userId }) => userId === action.payload.userId
);

Expand Down
11 changes: 11 additions & 0 deletions utils/rollbar-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* global window */
// @flow

export function logError(err: Error) {
try {
window.Rollbar.error(err);
} catch (err2) {
console.error('Rollbar client failed');
console.error(err2);
}
}

0 comments on commit 18a875c

Please sign in to comment.