Created
October 5, 2019 15:25
-
-
Save AyaMorisawa/10af43373998f0f3de70ec5505533610 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
type Transition<State, Operation> = (state: State, operation: Operation) => State; | |
class Store<State, Operation> { | |
private initialState: State; | |
private operations: Operation[] = []; | |
private transition: Transition<State, Operation>; | |
private currentState: State; | |
constructor(initialState: State, transition: Transition<State, Operation>) { | |
this.initialState = initialState; | |
this.transition = transition; | |
this.currentState = initialState; | |
} | |
public commit(operation: Operation) { | |
this.operations.push(operation); | |
this.currentState = this.transition(this.currentState, operation); | |
} | |
public commitAll(operations: Operation[]) { | |
for (const operation of operations) this.commit(operation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment