import { Actor , Action , IMap } from 'iflux2' ; import { fromJS , List } from 'immutable' ; interface Todo { id : number ; text : string ; done : boolean ; } let uuid = 0 ; export default class TodoActor extends Actor { defaultState ( ) { return { todo : [ ] } ; } @ Action ( 'submit' ) submit ( state , text ) { return state . update ( 'todo' , ( todo ) => todo . push ( fromJS ( { id : ++ uuid , text , done : false } ) ) ) ; } @ Action ( 'toggle' ) toggle ( state , index ) { return state . updateIn ( [ 'todo' , index , 'done' ] , done => ! done ) ; } @ Action ( 'destroy' ) destroy ( state , index ) { return state . deleteIn ( [ 'todo' , index ] ) ; } @ Action ( 'toggleAll' ) toggleAll ( state , checked ) { return state . update ( 'todo' , ( todo ) => todo . map ( ( v ) => v . set ( 'done' , checked ) ) ) ; } @ Action ( 'clearCompleted' ) clearCompleted ( state ) { return state . update ( 'todo' , todo => todo . filter ( ( v ) => ! v . get ( 'done' ) ) ) ; } @ Action ( 'init' ) init ( state , todo ) { return state . set ( 'todo' , fromJS ( todo ) ) ; } }