Skip to content

Commit f3c62a3

Browse files
authored
refactor(db): Make log handling explicit in StorageAPI.setState (#581)
* feat(db): Add appendLog method to storage API Move the log concatenation logic out of setState and into a separate appendLog method. * feat(master): Don’t persist deltalog in main state object * feat(db): Revert to using setState but with optional deltalog argument
1 parent c7dad76 commit f3c62a3

6 files changed

Lines changed: 35 additions & 26 deletions

File tree

src/master/master.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,12 @@ export class Master {
296296
};
297297
});
298298

299+
const { deltalog, ...stateWithoutDeltalog } = state;
300+
299301
if (IsSynchronous(this.storageAPI)) {
300-
this.storageAPI.setState(key, state);
302+
this.storageAPI.setState(key, stateWithoutDeltalog, deltalog);
301303
} else {
302-
await this.storageAPI.setState(key, state);
304+
await this.storageAPI.setState(key, stateWithoutDeltalog, deltalog);
303305
}
304306
}
305307

src/server/api.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ class AsyncStorage extends StorageAPI.Async {
1919

2020
constructor(args: any = {}) {
2121
super();
22-
const { createGame, fetch, setState, setMetadata, listGames, wipe } = args;
2322
this.mocks = {
24-
createGame: createGame || jest.fn(),
25-
setState: setState || jest.fn(),
26-
fetch: fetch || jest.fn(() => ({})),
27-
setMetadata: setMetadata || jest.fn(),
28-
listGames: listGames || jest.fn(() => []),
29-
wipe: wipe || jest.fn(),
23+
createGame: args.createGame || jest.fn(),
24+
setState: args.setState || jest.fn(),
25+
fetch: args.fetch || jest.fn(() => ({})),
26+
setMetadata: args.setMetadata || jest.fn(),
27+
listGames: args.listGames || jest.fn(() => []),
28+
wipe: args.wipe || jest.fn(),
3029
};
3130
}
3231

src/server/db/base.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,15 @@ export abstract class Async {
7373

7474
/**
7575
* Update the game state.
76+
*
77+
* If passed a deltalog array, setState should append its contents to the
78+
* existing log for this game.
7679
*/
77-
abstract setState(gameID: string, state: State): Promise<void>;
80+
abstract setState(
81+
gameID: string,
82+
state: State,
83+
deltalog?: LogEntry[]
84+
): Promise<void>;
7885

7986
/**
8087
* Update the game metadata.
@@ -130,8 +137,11 @@ export abstract class Sync {
130137

131138
/**
132139
* Update the game state.
140+
*
141+
* If passed a deltalog array, setState should append its contents to the
142+
* existing log for this game.
133143
*/
134-
abstract setState(gameID: string, state: State): void;
144+
abstract setState(gameID: string, state: State, deltalog?: LogEntry[]): void;
135145

136146
/**
137147
* Update the game metadata.

src/server/db/flatfile.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ describe('FlatFile', () => {
8888
phase: '',
8989
};
9090

91-
await db.setState('gameID', { deltalog: [logEntry1] } as State);
92-
await db.setState('gameID', { deltalog: [logEntry2] } as State);
91+
await db.setState('gameID', null, [logEntry1]);
92+
await db.setState('gameID', null, [logEntry2]);
9393

9494
const result = await db.fetch('gameID', { log: true });
9595
expect(result.log).toEqual([logEntry1, logEntry2]);

src/server/db/flatfile.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ export class FlatFile extends StorageAPI.Async {
9494
return this.games.clear();
9595
}
9696

97-
async setState(id: string, state: State) {
98-
let log: LogEntry[] =
99-
((await this.games.getItem(LogKey(id))) as LogEntry[]) || [];
100-
if (state.deltalog) {
101-
log = log.concat(state.deltalog);
97+
async setState(id: string, state: State, deltalog?: LogEntry[]) {
98+
if (deltalog && deltalog.length > 0) {
99+
const key = LogKey(id);
100+
const log: LogEntry[] =
101+
((await this.games.getItem(key)) as LogEntry[]) || [];
102+
await this.games.setItem(key, log.concat(deltalog));
102103
}
103-
await this.games.setItem(LogKey(id), log);
104104
return await this.games.setItem(id, state);
105105
}
106106

src/server/db/inmemory.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,12 @@ export class InMemory extends StorageAPI.Sync {
4848
/**
4949
* Write the game state to the in-memory object.
5050
*/
51-
setState(gameID: string, state: State): void {
52-
this.state.set(gameID, state);
53-
54-
let log = this.log.get(gameID) || [];
55-
if (state.deltalog) {
56-
log = log.concat(state.deltalog);
51+
setState(gameID: string, state: State, deltalog?: LogEntry[]): void {
52+
if (deltalog && deltalog.length > 0) {
53+
const log = this.log.get(gameID) || [];
54+
this.log.set(gameID, log.concat(deltalog));
5755
}
58-
this.log.set(gameID, log);
56+
this.state.set(gameID, state);
5957
}
6058

6159
/**

0 commit comments

Comments
 (0)