-
Notifications
You must be signed in to change notification settings - Fork 716
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate metadata and state in storage API
- Loading branch information
1 parent
e653681
commit c96e228
Showing
8 changed files
with
154 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { State, Server } from '../../types'; | ||
|
||
abstract class StorageAPI { | ||
/** | ||
* Connect. | ||
*/ | ||
connect() { | ||
return; | ||
} | ||
|
||
/** | ||
* Update the game state. | ||
*/ | ||
abstract setState(gameID: string, state: State): void; | ||
|
||
/** | ||
* Read the latest game state. | ||
*/ | ||
abstract getState(gameID: string): State; | ||
|
||
/** | ||
* Check if a particular game id exists. | ||
*/ | ||
abstract has(gameID: string): boolean; | ||
|
||
/** | ||
* Update the game metadata. | ||
*/ | ||
abstract setMetadata(gameID: string, metadata: Server.GameMetadata): void; | ||
|
||
/** | ||
* Fetch the game metadata. | ||
*/ | ||
abstract getMetadata(gameID: string): Server.GameMetadata; | ||
|
||
/** | ||
* Remove the game state. | ||
*/ | ||
abstract remove(gameID: string): void; | ||
|
||
/** | ||
* Return all games. | ||
*/ | ||
abstract list(): Array<string>; | ||
} | ||
|
||
export default StorageAPI; |
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { State, Server } from '../../types'; | ||
import StorageAPI from './base'; | ||
|
||
/* | ||
* Copyright 2017 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. | ||
*/ | ||
|
||
/** | ||
* InMemory data storage. | ||
*/ | ||
export class InMemory extends StorageAPI { | ||
private games: Map<string, State>; | ||
private metadata: Map<string, Server.GameMetadata>; | ||
|
||
/** | ||
* Creates a new InMemory storage. | ||
*/ | ||
constructor() { | ||
super(); | ||
this.games = new Map(); | ||
this.metadata = new Map(); | ||
} | ||
|
||
/** | ||
* Write the game metadata to the in-memory object. | ||
*/ | ||
setMetadata(gameID: string, metadata: Server.GameMetadata) { | ||
this.metadata.set(gameID, metadata); | ||
} | ||
|
||
/** | ||
* Read the game metadata from the in-memory object. | ||
*/ | ||
getMetadata(gameID: string): Server.GameMetadata { | ||
return this.metadata.get(gameID); | ||
} | ||
|
||
/** | ||
* Write the game state to the in-memory object. | ||
*/ | ||
setState(gameID: string, state: State): void { | ||
this.games.set(gameID, state); | ||
} | ||
|
||
/** | ||
* Read the game state from the in-memory object. | ||
*/ | ||
getState(gameID: string): State { | ||
return this.games.get(gameID); | ||
} | ||
|
||
/** | ||
* Check if a particular game id exists. | ||
*/ | ||
has(gameID: string): boolean { | ||
return this.games.has(gameID); | ||
} | ||
|
||
/** | ||
* Remove the game state from the in-memory object. | ||
*/ | ||
remove(gameID: string) { | ||
if (!this.games.has(gameID)) return; | ||
this.games.delete(gameID); | ||
} | ||
|
||
/** | ||
* Return all keys. | ||
*/ | ||
list(): string[] { | ||
return [...this.games.keys()]; | ||
} | ||
} |