Skip to content

Instantly share code, notes, and snippets.

@antonycourtney
Last active October 19, 2015 02:34
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 antonycourtney/1724be25e086a4f1d42a to your computer and use it in GitHub Desktop.
Save antonycourtney/1724be25e086a4f1d42a to your computer and use it in GitHub Desktop.
Purely functional data structure for Todo list application state
/**
* todoAppState.js -- Application State for TodoMVC as
* an immutable record
*/
export default class TodoAppState extends Immutable.Record({
todoItems: Immutable.Map() // map from id to TodoItem
}) {
/**
* functional item update -- returns a new state with the given item included in the
* set of todo items. If there is an existing entry for item.id, the result state
* will map id to item (functional update).
*/
addItem(item) {
const nextTodoItems = this.todoItems.set(item.id,item);
return this.set('todoItems',nextTodoItems);
}
/**
* functional delete -- returns a new state with the item for the given id removed
*/
removeItem(id) {
const nextTodoItems = this.todoItems.delete(id);
return this.set('todoItems',nextTodoItems);
}
/** An Immutable.Seq of all todo items */
getAll() {
return this.todoItems.toSetSeq();
}
/* returns true iff all items are complete */
areAllComplete() {
return this.todoItems.every((item) => item.complete);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment