Skip to content

Commit

Permalink
export Debug Panel in boardgame.io/debug
Browse files Browse the repository at this point in the history
This allows you to import it in production code
(it is stripped out if NODE_ENV === 'production' so you need
to explicitly include it if you need it).

import { Debug } from 'boardgame.io/debug';

const client = Client({
  debug: { impl: Debug },
});
  • Loading branch information
nicolodavis committed Nov 27, 2019
1 parent cae05fd commit e7d47ee
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dist/cjs",
"client",
"core",
"debug",
"react",
"react-native",
"server.js",
Expand Down
11 changes: 11 additions & 0 deletions packages/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright 2017 The boardgame.io Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

import Debug from '../src/client/debug/Debug.svelte';

export { Debug };
14 changes: 12 additions & 2 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,26 @@ class _ClientImpl {
this.transport.connect();
this._running = true;

let debugImpl = null;

if (process.env.NODE_ENV !== 'production') {
debugImpl = Debug;
}

if (this.debug && this.debug.impl) {
debugImpl = this.debug.impl;
}

if (
process.env.NODE_ENV !== 'production' &&
debugImpl !== null &&
this.debug !== false &&
this._debugPanel == null
) {
let target = document.body;
if (this.debug && this.debug.target) {
target = this.debug.target;
}
this._debugPanel = new Debug({
this._debugPanel = new debugImpl({
target,
props: {
client: this,
Expand Down
14 changes: 14 additions & 0 deletions src/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Game } from '../core/game';
import { LocalTransport, Local } from './transport/local';
import { SocketIOTransport, SocketIO } from './transport/socketio';
import { update, sync, makeMove, gameEvent } from '../core/action-creators';
import Debug from './debug/Debug.svelte';
import { error } from '../core/logger';

jest.mock('../core/logger', () => ({
Expand Down Expand Up @@ -639,6 +640,19 @@ describe('start / stop', () => {
client.stop();
});

test('override debug implementation', () => {
const client = Client({ game: {}, debug: { impl: Debug } });
client.start();
client.stop();
});

test('production mode', () => {
process.env.NODE_ENV = 'production';
const client = Client({ game: {} });
client.start();
client.stop();
});

test('try to stop without starting', () => {
const client = Client({ game: {} });
client.stop();
Expand Down
1 change: 1 addition & 0 deletions subpackages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module.exports = [
'client',
'core',
'debug',
'react',
'react-native',
'ai',
Expand Down

0 comments on commit e7d47ee

Please sign in to comment.