-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create interface for storage and wrap in-memory implementation in it
- Loading branch information
1 parent
e905983
commit 6272a5d
Showing
4 changed files
with
79 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2017 Google Inc. | ||
* | ||
* 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 { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
/** | ||
* Creates a new InMemory storage. | ||
* @param {function} reducer - The game reducer. | ||
*/ | ||
constructor(reducer) { | ||
this.reducer = reducer; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
this.obj = {}; | ||
} | ||
|
||
/** | ||
* Write the game state to the in-memory object. | ||
* @param {string} id - The game id. | ||
* @param {object} store - A Redux store to persist. | ||
*/ | ||
set(id, store) { | ||
this.obj[id] = store; | ||
} | ||
|
||
/** | ||
* Read the game state from the in-memory object. | ||
* @param {string} id - The game id. | ||
* @returns {object} - A Redux store with the game state, or null | ||
* if no game is found with this id. | ||
*/ | ||
get(id) { | ||
if (id in this.obj) { | ||
return this.obj[id]; | ||
} | ||
return null; | ||
} | ||
} |
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,23 @@ | ||
/* | ||
* Copyright 2017 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import { InMemory } from './db'; | ||
import * as Redux from 'redux'; | ||
|
||
test('basic', () => { | ||
const db = new InMemory(); | ||
const reducer = () => {}; | ||
const store = Redux.createStore(reducer); | ||
|
||
// Must return null when no game exists. | ||
expect(db.get('gameid')).toEqual(null); | ||
// Create game. | ||
db.set('gameid', store); | ||
// Must return created game. | ||
expect(db.get('gameid')).toEqual(store); | ||
}); |
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
1 comment
on commit 6272a5d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Map
class would be another option