Skip to content

Instantly share code, notes, and snippets.

@robertpenner
Forked from beattyml1/gist:5d05cfcfa87bb2ecdfee
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertpenner/22d31af621acfc266051 to your computer and use it in GitHub Desktop.
Save robertpenner/22d31af621acfc266051 to your computer and use it in GitHub Desktop.
// 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