Created
November 26, 2017 03:59
-
-
Save hliyan/e3fff8a8f68974d4b731b34d9230aaa3 to your computer and use it in GitHub Desktop.
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
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