Skip to content

Commit

Permalink
refactor(db): Make log handling explicit in StorageAPI.setState (#581)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
delucis authored Mar 27, 2020
1 parent c7dad76 commit f3c62a3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/master/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,12 @@ export class Master {
};
});

const { deltalog, ...stateWithoutDeltalog } = state;

if (IsSynchronous(this.storageAPI)) {
this.storageAPI.setState(key, state);
this.storageAPI.setState(key, stateWithoutDeltalog, deltalog);
} else {
await this.storageAPI.setState(key, state);
await this.storageAPI.setState(key, stateWithoutDeltalog, deltalog);
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/server/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ class AsyncStorage extends StorageAPI.Async {

constructor(args: any = {}) {
super();
const { createGame, fetch, setState, setMetadata, listGames, wipe } = args;
this.mocks = {
createGame: createGame || jest.fn(),
setState: setState || jest.fn(),
fetch: fetch || jest.fn(() => ({})),
setMetadata: setMetadata || jest.fn(),
listGames: listGames || jest.fn(() => []),
wipe: wipe || jest.fn(),
createGame: args.createGame || jest.fn(),
setState: args.setState || jest.fn(),
fetch: args.fetch || jest.fn(() => ({})),
setMetadata: args.setMetadata || jest.fn(),
listGames: args.listGames || jest.fn(() => []),
wipe: args.wipe || jest.fn(),
};
}

Expand Down
14 changes: 12 additions & 2 deletions src/server/db/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ export abstract class Async {

/**
* Update the game state.
*
* If passed a deltalog array, setState should append its contents to the
* existing log for this game.
*/
abstract setState(gameID: string, state: State): Promise<void>;
abstract setState(
gameID: string,
state: State,
deltalog?: LogEntry[]
): Promise<void>;

/**
* Update the game metadata.
Expand Down Expand Up @@ -130,8 +137,11 @@ export abstract class Sync {

/**
* Update the game state.
*
* If passed a deltalog array, setState should append its contents to the
* existing log for this game.
*/
abstract setState(gameID: string, state: State): void;
abstract setState(gameID: string, state: State, deltalog?: LogEntry[]): void;

/**
* Update the game metadata.
Expand Down
4 changes: 2 additions & 2 deletions src/server/db/flatfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ describe('FlatFile', () => {
phase: '',
};

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

const result = await db.fetch('gameID', { log: true });
expect(result.log).toEqual([logEntry1, logEntry2]);
Expand Down
12 changes: 6 additions & 6 deletions src/server/db/flatfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ export class FlatFile extends StorageAPI.Async {
return this.games.clear();
}

async setState(id: string, state: State) {
let log: LogEntry[] =
((await this.games.getItem(LogKey(id))) as LogEntry[]) || [];
if (state.deltalog) {
log = log.concat(state.deltalog);
async setState(id: string, state: State, deltalog?: LogEntry[]) {
if (deltalog && deltalog.length > 0) {
const key = LogKey(id);
const log: LogEntry[] =
((await this.games.getItem(key)) as LogEntry[]) || [];
await this.games.setItem(key, log.concat(deltalog));
}
await this.games.setItem(LogKey(id), log);
return await this.games.setItem(id, state);
}

Expand Down
12 changes: 5 additions & 7 deletions src/server/db/inmemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ export class InMemory extends StorageAPI.Sync {
/**
* Write the game state to the in-memory object.
*/
setState(gameID: string, state: State): void {
this.state.set(gameID, state);

let log = this.log.get(gameID) || [];
if (state.deltalog) {
log = log.concat(state.deltalog);
setState(gameID: string, state: State, deltalog?: LogEntry[]): void {
if (deltalog && deltalog.length > 0) {
const log = this.log.get(gameID) || [];
this.log.set(gameID, log.concat(deltalog));
}
this.log.set(gameID, log);
this.state.set(gameID, state);
}

/**
Expand Down

0 comments on commit f3c62a3

Please sign in to comment.