Skip to content

Commit 239f8dd

Browse files
Stefan-Hankenicolodavis
authored andcommitted
Use parse/stringify from flatted lib to support circular structures (fixes #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
1 parent edd1df0 commit 239f8dd

7 files changed

Lines changed: 23 additions & 8 deletions

File tree

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
"dependencies": {
121121
"@koa/cors": "^2.2.1",
122122
"firebase": "^5.0.4",
123+
"flatted": "^0.2.3",
123124
"koa": "^2.3.0",
124125
"koa-body": "^2.5.0",
125126
"koa-router": "^7.2.1",
@@ -156,7 +157,7 @@
156157
"raf/polyfill"
157158
],
158159
"transformIgnorePatterns": [
159-
"node_modules/(?!(boardgame.io)/)"
160+
"node_modules/(?!(boardgame.io|flatted)/)"
160161
],
161162
"testPathIgnorePatterns": [
162163
"/examples/react-native/",

rollup.npm.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const globals = {
3131
'prop-types': 'PropTypes',
3232
mousetrap: 'Mousetrap',
3333
'socket.io-client': 'io',
34+
flatted: 'Flatted',
3435
};
3536

3637
export default [
@@ -77,13 +78,17 @@ export default [
7778

7879
{
7980
input: 'packages/core.js',
81+
external: Object.keys(globals),
82+
globals,
8083
output: { file: 'dist/core.js', format: 'umd' },
8184
name: 'Core',
8285
plugins,
8386
},
8487

8588
{
8689
input: 'packages/ai.js',
90+
external: Object.keys(globals),
91+
globals,
8792
output: { file: 'dist/ai.js', format: 'umd' },
8893
name: 'AI',
8994
plugins,

src/client/debug/debug.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { PlayerInfo } from './playerinfo';
1616
import { DebugMove } from './debug-move';
1717
import { GameLog } from '../log/log';
1818
import { restore } from '../../core/action-creators';
19+
import { parse, stringify } from 'flatted';
1920
import './debug.css';
2021

2122
/**
@@ -102,14 +103,14 @@ export class Debug extends React.Component {
102103
}
103104

104105
saveState = () => {
105-
const json = JSON.stringify(this.props.gamestate);
106+
const json = stringify(this.props.gamestate);
106107
window.localStorage.setItem('gamestate', json);
107108
};
108109

109110
restoreState = () => {
110111
const gamestateJSON = window.localStorage.getItem('gamestate');
111112
if (gamestateJSON !== null) {
112-
const gamestate = JSON.parse(gamestateJSON);
113+
const gamestate = parse(gamestateJSON);
113114
this.props.store.dispatch(restore(gamestate));
114115
}
115116
};
@@ -237,14 +238,14 @@ export class Debug extends React.Component {
237238
<section>
238239
<pre className="json">
239240
<strong>ctx</strong>:{' '}
240-
{JSON.stringify(this.props.gamestate.ctx, null, 2)}
241+
{stringify(this.props.gamestate.ctx, null, 2)}
241242
</pre>
242243
</section>
243244

244245
<section>
245246
<pre className="json">
246247
<strong>G</strong>:{' '}
247-
{JSON.stringify(this.props.gamestate.G, null, 2)}
248+
{stringify(this.props.gamestate.G, null, 2)}
248249
</pre>
249250
</section>
250251
</span>

src/client/debug/debug.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import React from 'react';
10+
import { stringify } from 'flatted';
1011
import { restore, makeMove, gameEvent } from '../../core/action-creators';
1112
import Game from '../../core/game';
1213
import { CreateGameReducer } from '../../core/reducer';
@@ -84,7 +85,7 @@ describe('save / restore', () => {
8485
});
8586

8687
const restoredState = { restore: true };
87-
let restoredJSON = JSON.stringify(restoredState);
88+
let restoredJSON = stringify(restoredState);
8889
const setItem = jest.fn();
8990
const getItem = jest.fn(() => restoredJSON);
9091

src/client/debug/gameinfo.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
import React from 'react';
1010
import PropTypes from 'prop-types';
11+
import { stringify } from 'flatted';
1112

1213
const Item = props => (
1314
<div className="gameinfo-item">
1415
<strong>{props.name} </strong>
15-
<div>{JSON.stringify(props.value)}</div>
16+
<div>{stringify(props.value)}</div>
1617
</div>
1718
);
1819

src/core/reducer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* https://opensource.org/licenses/MIT.
77
*/
88

9+
import { parse, stringify } from 'flatted';
910
import * as Actions from './action-types';
1011
import { Random } from './random';
1112
import { Events } from './events';
@@ -72,7 +73,7 @@ export function CreateGameReducer({ game, numPlayers, multiplayer }) {
7273
initial.ctx = Random.detach(initial.ctx);
7374
initial.ctx = Events.detach(initial.ctx);
7475

75-
const deepCopy = obj => JSON.parse(JSON.stringify(obj));
76+
const deepCopy = obj => parse(stringify(obj));
7677
initial._initial = deepCopy(initial);
7778

7879
/**

0 commit comments

Comments
 (0)