Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log metadata plugin #865

Merged
merged 15 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/client/debug/log/CustomPayload.svelte

This file was deleted.

4 changes: 2 additions & 2 deletions src/client/debug/log/Log.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@
{/if}
{/each}

{#each renderedLogEntries as { action, payload }, i}
{#each renderedLogEntries as { action, metadata }, i}
<LogEvent
pinned={i === pinned}
logIndex={i}
on:click={OnLogClick}
on:mouseenter={OnMouseEnter}
on:mouseleave={OnMouseLeave}
{action}
{payload} />
{metadata} />
{/each}

{#each renderedLogEntries as { phase }, i}
Expand Down
15 changes: 7 additions & 8 deletions src/client/debug/log/LogEvent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
export let logIndex;
export let action;
export let pinned;
export let payload;
export let payloadComponent;
export let metadata;
export let metadataComponent;

import CustomPayload from './CustomPayload.svelte';
import LogMetadata from './LogMetadata.svelte';
import { createEventDispatcher } from 'svelte';

const dispatch = createEventDispatcher();

const args = action.payload.args;
const renderedArgs = typeof args === 'string' ? args : (args || []).join(',');
const playerID = action.payload.playerID;
let actionType;
let actionType;
switch (action.type) {
case 'UNDO':
actionType = 'undo';
Expand Down Expand Up @@ -134,10 +134,9 @@
on:blur={() => dispatch('mouseleave')}
>
<div>{actionType}({renderedArgs})</div>

{#if payloadComponent}
<svelte:component this={payloadComponent} {payload} />
{#if metadataComponent}
<svelte:component this={metadataComponent} {metadata} />
{:else}
<CustomPayload {payload} />
<LogMetadata {metadata} />
{/if}
</button>
7 changes: 7 additions & 0 deletions src/client/debug/log/LogMetadata.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export let metadata;
const renderedMetadata =
metadata !== undefined ? JSON.stringify(metadata, null, 4) : '';
</script>

<div>{renderedMetadata}</div>
5 changes: 5 additions & 0 deletions src/core/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ function initializeDeltalog(
phase: state.ctx.phase,
};

const pluginLogMetadata = state.plugins.log.data.metadata;
if (pluginLogMetadata !== undefined) {
logEntry.metadata = pluginLogMetadata;
}

if (typeof move === 'object' && move.redact === true) {
logEntry.redact = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import PluginImmer from './plugin-immer';
import PluginRandom from './plugin-random';
import PluginEvents from './plugin-events';
import PluginLog from './plugin-log';
import {
AnyFn,
PartialGameState,
Expand All @@ -28,7 +29,7 @@ interface PluginOpts {
/**
* List of plugins that are always added.
*/
const DEFAULT_PLUGINS = [PluginImmer, PluginRandom, PluginEvents];
const DEFAULT_PLUGINS = [PluginImmer, PluginRandom, PluginEvents, PluginLog];

/**
* Allow plugins to intercept actions and process them.
Expand Down
36 changes: 36 additions & 0 deletions src/plugins/plugin-log.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 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 { Client } from '../client/client';

describe('log-metadata', () => {
test('It sets metadata in a move and then clears the metadata', () => {
const game = {
moves: {
setMetadataMove: (G, ctx) => {
ctx.log.setMetadata({
message: 'test',
});
},
doNothing: G => G,
},
};
const client = Client({ game });
client.moves.setMetadataMove();

expect(client.getState().plugins.log.data).toEqual({});
expect(client.getState().log[0].metadata).toEqual({
message: 'test',
});

client.moves.doNothing();

expect(client.getState().plugins.log.data).toEqual({});
expect(client.getState().log[1].metadata).toEqual(undefined);
});
});
40 changes: 40 additions & 0 deletions src/plugins/plugin-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2018 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 { Plugin } from '../types';

interface LogMetadataData {
metadata?: any;
}

interface LogMetadataAPI {
setMetadata(metadata: any): void;
}

/**
* Plugin that makes it possible to add metadata to log entries.
* During a move, you can set metadata using ctx.log.setMetadata and it will be
* available on the log entry for that move.
*/
const LogPlugin: Plugin<LogMetadataAPI, LogMetadataData> = {
name: 'log',

flush: () => ({}),

api: ({ data }) => {
return {
setMetadata: metadata => {
data.metadata = metadata;
},
};
},

setup: () => ({}),
};

export default LogPlugin;
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export interface LogEntry {
phase: string;
redact?: boolean;
automatic?: boolean;
metadata?: any;
}

interface PluginContext<
Expand Down