-
-
Save robertpenner/22d31af621acfc266051 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// really more a struct/interface | |
class DataStore { | |
store: (object: any, key: string) => string; | |
get: (key: string) => any; | |
} | |
class JsonDataStore extends DataStore { | |
getRawJson: () => string; | |
static create(fileWriter: FileWriter): JsonDataStore { | |
return { | |
get: (key: string) => JSON.parse(fileWriter.readAll())[key], | |
store: function(object: any, key: string): string { | |
var allData = JSON.parse(fileWriter.readAll())[key]; | |
allData[key] = object; | |
return fileWriter.writeAll(JSON.stringify(allData)); | |
}, | |
getRawJson: () => fileWriter.readAll() | |
} | |
} | |
} | |
var store = JsonDataStore.create(someRandomFileWriter); | |
store.store(myRandomObject, 'foo'); | |
interface FileWriter { | |
readAll(): string; | |
writeAll(s: string): string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment