{"size": 1190, "ext": "js", "max_stars_repo_path": "demo/universal-routed-flux-demo/services/TodoStorageService.js", "max_stars_repo_name": "xiaomailong/prestudy", "max_stars_repo_stars_event_min_datetime": "2019-03-20T01:30:24.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-25T09:56:31.000Z", "max_issues_repo_path": "demo/universal-routed-flux-demo/services/TodoStorageService.js", "max_issues_repo_name": "xiaomailong/prestudy", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "demo/universal-routed-flux-demo/services/TodoStorageService.js", "max_forks_repo_name": "xiaomailong/prestudy", "max_forks_repo_forks_event_min_datetime": "2022-01-21T01:02:36.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-21T01:02:36.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 27.6744186047, "max_line_length": 93, "alphanum_fraction": 0.4974789916}
var fs = require('fs');
//simulate server delay
const RESPONSE_TIME = 300;

export default class TodoStorageService {
    constructor(storePath = __dirname + '/todos.json') {
        console.log('storage service initialised');
        this._todos = {};
        this.storePath = storePath;
    }
    write(todo, cb){
        this._todos[todo.id] = todo;
        setTimeout(() => {
            fs.writeFile(this.storePath, JSON.stringify(this._todos, null, 4), function(err){
                cb(err);
            });
        }, RESPONSE_TIME);
    }

    read(cb){
        fs.readFile(this.storePath, (err, data) => {
            setTimeout(() => {
                if (err !== null){
                    cb(err, null);
                    return;
                }
                this._todos = JSON.parse(data);
                cb(null, this._todos);
            }, RESPONSE_TIME);
        });
    }

    delete(id, cb){
        delete this._todos[id];
        setTimeout(() => {
            fs.writeFile(this.storePath, JSON.stringify(this._todos, null, 4), function(err){
                cb(err);
            });
        }, RESPONSE_TIME);
    }}

export default new TodoStorageService();
