Skip to content

Commit da1eac6

Browse files
committed
rename plugin api functions
1 parent 659007a commit da1eac6

5 files changed

Lines changed: 78 additions & 48 deletions

File tree

docs/plugins.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,47 @@ in `G` and much more.
88
#### Creating a Plugin
99

1010
A plugin is an object that contains the following fields.
11+
All fields are optional and typically accept the `game`
12+
object as a parameter.
1113

1214
```js
1315
{
14-
// Optional.
1516
// Function that accepts a move / trigger function
1617
// and returns another function that wraps it. This
1718
// wrapper can modify G before passing it down to
1819
// the wrapped function. It is a good practice to
19-
// undo the change at the end of the call.
20-
fnWrap: (fn) => (G, ctx, ...args) => {
20+
// undo the change at the end of the call. You can
21+
// also use the more convenient preMove / postMove
22+
// hooks below if all you desire is to preprocess
23+
// and postprocess G.
24+
fnWrap: (fn, game) => (G, ctx, ...args) => {
2125
G = preprocess(G);
2226
G = fn(G, ctx, ...args);
2327
G = postprocess(G);
2428
return G;
2529
},
2630

27-
// Optional.
28-
// Called during setup in order to add state to G.
29-
setupG: (G, ctx) => G,
31+
G: {
32+
// Called during setup in order to add state to G.
33+
setup: (G, ctx, game) => G,
3034

31-
// Optional.
32-
// Called during setup in order to add state to ctx.
33-
setupCtx: (ctx) => ctx,
35+
// Called right before a move / event in order to preprocess G.
36+
preMove: (G, game) => G,
37+
38+
// Called right after a move / event in order to postprocess G.
39+
postMove: (G, game) => G,
40+
},
41+
42+
ctx: {
43+
// Called during setup in order to add state to ctx.
44+
setup: (ctx, game) => ctx,
45+
46+
// Called right before a move / event in order to preprocess ctx.
47+
preMove: (ctx, game) => ctx,
48+
49+
// Called right after a move / event in order to postprocess ctx.
50+
postMove: (ctx, game) => ctx,
51+
},
3452
}
3553
```
3654

src/plugins/main.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ const DEFAULT_PLUGINS = [PluginImmer];
2121
*/
2222
export const SetupCtx = (ctx, game) => {
2323
[...DEFAULT_PLUGINS, ...game.plugins]
24-
.filter(plugin => plugin.setupCtx !== undefined)
24+
.filter(plugin => plugin.ctx !== undefined)
25+
.filter(plugin => plugin.ctx.setup !== undefined)
2526
.forEach(plugin => {
26-
ctx = plugin.setupCtx(ctx, game);
27+
ctx = plugin.ctx.setup(ctx, game);
2728
});
2829
return ctx;
2930
};
@@ -37,9 +38,10 @@ export const SetupCtx = (ctx, game) => {
3738
*/
3839
export const SetupG = (G, ctx, game) => {
3940
[...DEFAULT_PLUGINS, ...game.plugins]
40-
.filter(plugin => plugin.setupG !== undefined)
41+
.filter(plugin => plugin.G !== undefined)
42+
.filter(plugin => plugin.G.setup !== undefined)
4143
.forEach(plugin => {
42-
G = plugin.setupG(G, ctx, game);
44+
G = plugin.G.setup(G, ctx, game);
4345
});
4446
return G;
4547
};
@@ -50,26 +52,28 @@ export const SetupG = (G, ctx, game) => {
5052
* @param {object} ctx - The ctx object.
5153
* @param {object} game - The game object.
5254
*/
53-
export const AddToCtx = (ctx, game) => {
55+
export const CtxPreMove = (ctx, game) => {
5456
[...DEFAULT_PLUGINS, ...game.plugins]
55-
.filter(plugin => plugin.addToCtx !== undefined)
57+
.filter(plugin => plugin.ctx !== undefined)
58+
.filter(plugin => plugin.ctx.preMove !== undefined)
5659
.forEach(plugin => {
57-
ctx = plugin.addToCtx(ctx, game);
60+
ctx = plugin.ctx.preMove(ctx, game);
5861
});
5962
return ctx;
6063
};
6164

6265
/**
63-
* Removes the provided plugins to ctx after processing a move / event.
66+
* Postprocesses ctx after a move / event.
6467
*
6568
* @param {object} ctx - The ctx object.
6669
* @param {object} game - The game object.
6770
*/
68-
export const RemoveFromCtx = (ctx, game) => {
71+
export const CtxPostMove = (ctx, game) => {
6972
[...DEFAULT_PLUGINS, ...game.plugins]
70-
.filter(plugin => plugin.removeFromCtx !== undefined)
73+
.filter(plugin => plugin.ctx !== undefined)
74+
.filter(plugin => plugin.ctx.postMove !== undefined)
7175
.forEach(plugin => {
72-
ctx = plugin.removeFromCtx(ctx, game);
76+
ctx = plugin.ctx.postMove(ctx, game);
7377
});
7478
return ctx;
7579
};
@@ -80,26 +84,28 @@ export const RemoveFromCtx = (ctx, game) => {
8084
* @param {object} G - The G object.
8185
* @param {object} game - The game object.
8286
*/
83-
export const AddToG = (G, game) => {
87+
export const GPreMove = (G, game) => {
8488
[...DEFAULT_PLUGINS, ...game.plugins]
85-
.filter(plugin => plugin.addToG !== undefined)
89+
.filter(plugin => plugin.G !== undefined)
90+
.filter(plugin => plugin.G.preMove !== undefined)
8691
.forEach(plugin => {
87-
G = plugin.addToG(G, game);
92+
G = plugin.G.preMove(G, game);
8893
});
8994
return G;
9095
};
9196

9297
/**
93-
* Removes the provided plugins to G after processing a move / event.
98+
* Postprocesses G after a move / event.
9499
*
95100
* @param {object} G - The G object.
96101
* @param {object} game - The game object.
97102
*/
98-
export const RemoveFromG = (G, game) => {
103+
export const GPostMove = (G, game) => {
99104
[...DEFAULT_PLUGINS, ...game.plugins]
100-
.filter(plugin => plugin.removeFromG !== undefined)
105+
.filter(plugin => plugin.G !== undefined)
106+
.filter(plugin => plugin.G.postMove !== undefined)
101107
.forEach(plugin => {
102-
G = plugin.removeFromG(G, game);
108+
G = plugin.G.postMove(G, game);
103109
});
104110
return G;
105111
};
@@ -111,17 +117,17 @@ export const RemoveFromG = (G, game) => {
111117
* @param {object} game - The game object.
112118
*/
113119
export const FnWrap = (fn, game) => {
114-
const reducer = (acc, { fnWrap }) => fnWrap(acc);
120+
const reducer = (acc, { fnWrap }) => fnWrap(acc, game);
115121
const g = [...DEFAULT_PLUGINS, ...game.plugins]
116122
.filter(plugin => plugin.fnWrap !== undefined)
117123
.reduce(reducer, fn);
118124

119125
return (G, ctx, ...args) => {
120-
G = AddToG(G, game);
121-
ctx = AddToCtx(ctx, game);
126+
G = GPreMove(G, game);
127+
ctx = CtxPreMove(ctx, game);
122128
G = g(G, ctx, ...args);
123-
ctx = RemoveFromCtx(ctx, game);
124-
ctx = RemoveFromG(G, game);
129+
ctx = CtxPostMove(ctx, game);
130+
ctx = GPostMove(G, game);
125131
return G;
126132
};
127133
};

src/plugins/main.test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ describe('plugins', () => {
2929
G = fn(G, ctx);
3030
return { ...G, fnWrap: true };
3131
},
32-
setupG: G => ({ ...G, initG: true }),
33-
setupCtx: ctx => ({ ...ctx, initCtx: true }),
34-
addToCtx: ctx => ({ ...ctx, addToCtx: true }),
35-
removeFromCtx: ctx => ({ ...ctx, removeFromCtx: true }),
36-
addToG: G => ({ ...G, addToG: true }),
37-
removeFromG: G => ({ ...G, removeFromG: true }),
32+
G: {
33+
setup: G => ({ ...G, initG: true }),
34+
preMove: G => ({ ...G, addToG: true }),
35+
postMove: G => ({ ...G, removeFromG: true }),
36+
},
37+
ctx: {
38+
setup: ctx => ({ ...ctx, initCtx: true }),
39+
preMove: ctx => ({ ...ctx, addToCtx: true }),
40+
postMove: ctx => ({ ...ctx, removeFromCtx: true }),
41+
},
3842
},
3943
],
4044
});

src/plugins/plugin-player.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ export default initPlayerState => ({
4848
};
4949
},
5050

51-
setupG: (G, ctx) => {
52-
let players = {};
53-
for (let i = 0; i < ctx.numPlayers; i++) {
54-
const playerState = {};
55-
if (initPlayerState !== undefined) {
56-
initPlayerState(i + '');
51+
G: {
52+
setup: (G, ctx) => {
53+
let players = {};
54+
for (let i = 0; i < ctx.numPlayers; i++) {
55+
const playerState = {};
56+
if (initPlayerState !== undefined) {
57+
initPlayerState(i + '');
58+
}
59+
players[i + ''] = playerState;
5760
}
58-
players[i + ''] = playerState;
59-
}
60-
return { ...G, players };
61+
return { ...G, players };
62+
},
6163
},
6264
});

src/plugins/plugin-player.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { makeMove, gameEvent } from '../core/action-creators';
1313

1414
describe('default values', () => {
1515
test('playerState is not passed', () => {
16-
const value = PluginPlayer().setupG({}, { numPlayers: 2 });
16+
const value = PluginPlayer().G.setup({}, { numPlayers: 2 });
1717
expect(value).toEqual({ players: { '0': {}, '1': {} } });
1818
});
1919
});

0 commit comments

Comments
 (0)