Skip to content

Instantly share code, notes, and snippets.

@AyaMorisawa
Created October 5, 2019 15:25
Show Gist options
  • Save AyaMorisawa/10af43373998f0f3de70ec5505533610 to your computer and use it in GitHub Desktop.
Save AyaMorisawa/10af43373998f0f3de70ec5505533610 to your computer and use it in GitHub Desktop.
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