Skip to content

Instantly share code, notes, and snippets.

@hliyan
Created November 26, 2017 03:59
Show Gist options
  • Save hliyan/e3fff8a8f68974d4b731b34d9230aaa3 to your computer and use it in GitHub Desktop.
Save hliyan/e3fff8a8f68974d4b731b34d9230aaa3 to your computer and use it in GitHub Desktop.
const Emitter = require('../../lib/emitter');
const events = {
INIT:'init',
CREATE_TODO: 'createTodo'
};
const constants = {
status: {
TODO: 'TODO',
DONE: 'DONE'
}
};
/**
* Encapsulates all todo business logic and state
*/
class TodoEngine extends Emitter {
constructor() {
super();
this.events = events;
this.constants = constants;
this.todoList = [];
}
init() {
this.emit(this.events.INIT, {});
}
createTodo({text}) {
if (!text) {
this.emitError(this.events.CREATE_TODO, {message: 'Text must be non-empty'});
return;
}
let todo = {text: text, status: this.constants.status.TODO};
this.todoList.push(todo);
this.emit(this.events.CREATE_TODO, {todo});
}
getTodoList() {
return this.todoList;
}
}
module.exports = TodoEngine;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment