Skip to content

Commit

Permalink
rename remove to wipe
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Mar 18, 2020
1 parent a75605d commit bff685d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/server/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class AsyncStorage extends StorageAPI.Async {

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

Expand All @@ -43,8 +43,8 @@ class AsyncStorage extends StorageAPI.Async {
this.mocks.setMetadata(...args);
}

async remove(...args) {
this.mocks.remove(...args);
async wipe(...args) {
this.mocks.wipe(...args);
}

async listGames() {
Expand Down Expand Up @@ -564,7 +564,7 @@ describe('.createApiServer', () => {
response = await request(app.callback())
.post('/games/foo/1/leave')
.send('playerID=0&credentials=SECRET1');
expect(db.mocks.remove).toHaveBeenCalledWith('1');
expect(db.mocks.wipe).toHaveBeenCalledWith('1');
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export const addApiToServer = ({
await db.setMetadata(gameID, metadata);
} else {
// remove room
await db.remove(gameID);
await db.wipe(gameID);
}
ctx.body = {};
});
Expand Down
22 changes: 11 additions & 11 deletions src/server/db/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ export abstract class Async {
*/
abstract setState(gameID: string, state: State): Promise<void>;

/**
* Fetch the game state.
*/
abstract fetch(gameID: string, opts: FetchOpts): Promise<FetchResult>;

/**
* Update the game metadata.
*/
Expand All @@ -53,10 +48,15 @@ export abstract class Async {
metadata: Server.GameMetadata
): Promise<void>;

/**
* Fetch the game state.
*/
abstract fetch(gameID: string, opts: FetchOpts): Promise<FetchResult>;

/**
* Remove the game state.
*/
abstract remove(gameID: string): Promise<void>;
abstract wipe(gameID: string): Promise<void>;

/**
* Return all games.
Expand All @@ -82,19 +82,19 @@ export abstract class Sync {
abstract setState(gameID: string, state: State): void;

/**
* Fetch the game state.
* Update the game metadata.
*/
abstract fetch(gameID: string, opts: FetchOpts): FetchResult;
abstract setMetadata(gameID: string, metadata: Server.GameMetadata): void;

/**
* Update the game metadata.
* Fetch the game state.
*/
abstract setMetadata(gameID: string, metadata: Server.GameMetadata): void;
abstract fetch(gameID: string, opts: FetchOpts): FetchResult;

/**
* Remove the game state.
*/
abstract remove(gameID: string): void;
abstract wipe(gameID: string): void;

/**
* Return all games.
Expand Down
20 changes: 10 additions & 10 deletions src/server/db/flatfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FlatFile } from './flatfile';
import { State, Server, LogEntry } from '../../types';

describe('FlatFile', () => {
let db;
let db: FlatFile;

beforeAll(async () => {
db = new FlatFile({ dir: './tmp', logging: false });
Expand All @@ -23,17 +23,17 @@ describe('FlatFile', () => {

test('basic', async () => {
// Must return undefined when no game exists.
let { state } = await db.fetch('gameID', { state: true });
expect(state).toEqual(undefined);
let result = await db.fetch('gameID', { state: true });
expect(result.state).toEqual(undefined);

// Create game.
state = { a: 1 };
let metadata: unknown = { metadata: true };
const state: unknown = { a: 1 };
const metadata: unknown = { metadata: true };
await db.setState('gameID', state as State);
await db.setMetadata('gameID', metadata as Server.GameMetadata);

// Must return created game.
const result = await db.fetch('gameID', { state: true, metadata: true });
result = await db.fetch('gameID', { state: true, metadata: true });
expect(result.state).toEqual({ a: 1 });
expect(result.metadata).toEqual({ metadata: true });

Expand All @@ -42,13 +42,13 @@ describe('FlatFile', () => {
expect(keys).toEqual(['gameID']);

// Must remove game from DB
await db.remove('gameID');
await db.wipe('gameID');
expect(
await db.fetch('gameID', { metadata: true, state: true, log: true })
).toEqual({});

// Shall not return error
await db.remove('gameID');
await db.wipe('gameID');

// Shall create game, then clear DB, then check whether DB is cleared
await db.setState('game2', state as State);
Expand Down Expand Up @@ -78,8 +78,8 @@ describe('FlatFile', () => {
phase: '',
};

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

const result = await db.fetch('gameID', { log: true });
expect(result.log).toEqual([logEntry1, logEntry2]);
Expand Down
2 changes: 1 addition & 1 deletion src/server/db/flatfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class FlatFile extends StorageAPI.Async {
return await this.games.setItem(key, metadata);
}

async remove(id: string) {
async wipe(id: string) {
var keys = await this.games.keys();
if (!(keys.indexOf(id) > -1)) return;
await this.games.removeItem(id);
Expand Down
6 changes: 3 additions & 3 deletions src/server/db/inmemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { InMemory } from './inmemory';
import { State, Server } from '../../types';

describe('InMemory', () => {
let db;
let db: InMemory;

beforeAll(() => {
db = new InMemory();
Expand Down Expand Up @@ -47,9 +47,9 @@ describe('InMemory', () => {

test('remove', () => {
// Must remove game from DB
db.remove('gameID');
db.wipe('gameID');
expect(db.fetch('gameID', { state: true })).toEqual({});
// Shall not return error
db.remove('gameID');
db.wipe('gameID');
});
});
2 changes: 1 addition & 1 deletion src/server/db/inmemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class InMemory extends StorageAPI.Sync {
/**
* Remove the game state from the in-memory object.
*/
remove(gameID: string) {
wipe(gameID: string) {
this.games.delete(gameID);
this.metadata.delete(gameID);
}
Expand Down

0 comments on commit bff685d

Please sign in to comment.