Skip to content

Commit

Permalink
Use parse/stringify from flatted lib to support circular structures (f…
Browse files Browse the repository at this point in the history
…ixes #222) (#240)

* use parse/stringify from flatted lib to support circular structures

* use the esm variant instead of cjs of flatted

* don't ignore flatted/esm when transforming

* need to use flatted/esm in test also

* fix
  • Loading branch information
Stefan-Hanke authored and nicolodavis committed Jul 25, 2018
1 parent edd1df0 commit 239f8dd
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"dependencies": {
"@koa/cors": "^2.2.1",
"firebase": "^5.0.4",
"flatted": "^0.2.3",
"koa": "^2.3.0",
"koa-body": "^2.5.0",
"koa-router": "^7.2.1",
Expand Down Expand Up @@ -156,7 +157,7 @@
"raf/polyfill"
],
"transformIgnorePatterns": [
"node_modules/(?!(boardgame.io)/)"
"node_modules/(?!(boardgame.io|flatted)/)"
],
"testPathIgnorePatterns": [
"/examples/react-native/",
Expand Down
5 changes: 5 additions & 0 deletions rollup.npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const globals = {
'prop-types': 'PropTypes',
mousetrap: 'Mousetrap',
'socket.io-client': 'io',
flatted: 'Flatted',
};

export default [
Expand Down Expand Up @@ -77,13 +78,17 @@ export default [

{
input: 'packages/core.js',
external: Object.keys(globals),
globals,
output: { file: 'dist/core.js', format: 'umd' },
name: 'Core',
plugins,
},

{
input: 'packages/ai.js',
external: Object.keys(globals),
globals,
output: { file: 'dist/ai.js', format: 'umd' },
name: 'AI',
plugins,
Expand Down
9 changes: 5 additions & 4 deletions src/client/debug/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { PlayerInfo } from './playerinfo';
import { DebugMove } from './debug-move';
import { GameLog } from '../log/log';
import { restore } from '../../core/action-creators';
import { parse, stringify } from 'flatted';
import './debug.css';

/**
Expand Down Expand Up @@ -102,14 +103,14 @@ export class Debug extends React.Component {
}

saveState = () => {
const json = JSON.stringify(this.props.gamestate);
const json = stringify(this.props.gamestate);
window.localStorage.setItem('gamestate', json);
};

restoreState = () => {
const gamestateJSON = window.localStorage.getItem('gamestate');
if (gamestateJSON !== null) {
const gamestate = JSON.parse(gamestateJSON);
const gamestate = parse(gamestateJSON);
this.props.store.dispatch(restore(gamestate));
}
};
Expand Down Expand Up @@ -237,14 +238,14 @@ export class Debug extends React.Component {
<section>
<pre className="json">
<strong>ctx</strong>:{' '}
{JSON.stringify(this.props.gamestate.ctx, null, 2)}
{stringify(this.props.gamestate.ctx, null, 2)}
</pre>
</section>

<section>
<pre className="json">
<strong>G</strong>:{' '}
{JSON.stringify(this.props.gamestate.G, null, 2)}
{stringify(this.props.gamestate.G, null, 2)}
</pre>
</section>
</span>
Expand Down
3 changes: 2 additions & 1 deletion src/client/debug/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import React from 'react';
import { stringify } from 'flatted';
import { restore, makeMove, gameEvent } from '../../core/action-creators';
import Game from '../../core/game';
import { CreateGameReducer } from '../../core/reducer';
Expand Down Expand Up @@ -84,7 +85,7 @@ describe('save / restore', () => {
});

const restoredState = { restore: true };
let restoredJSON = JSON.stringify(restoredState);
let restoredJSON = stringify(restoredState);
const setItem = jest.fn();
const getItem = jest.fn(() => restoredJSON);

Expand Down
3 changes: 2 additions & 1 deletion src/client/debug/gameinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

import React from 'react';
import PropTypes from 'prop-types';
import { stringify } from 'flatted';

const Item = props => (
<div className="gameinfo-item">
<strong>{props.name} </strong>
<div>{JSON.stringify(props.value)}</div>
<div>{stringify(props.value)}</div>
</div>
);

Expand Down
3 changes: 2 additions & 1 deletion src/core/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* https://opensource.org/licenses/MIT.
*/

import { parse, stringify } from 'flatted';
import * as Actions from './action-types';
import { Random } from './random';
import { Events } from './events';
Expand Down Expand Up @@ -72,7 +73,7 @@ export function CreateGameReducer({ game, numPlayers, multiplayer }) {
initial.ctx = Random.detach(initial.ctx);
initial.ctx = Events.detach(initial.ctx);

const deepCopy = obj => JSON.parse(JSON.stringify(obj));
const deepCopy = obj => parse(stringify(obj));
initial._initial = deepCopy(initial);

/**
Expand Down

0 comments on commit 239f8dd

Please sign in to comment.