Last active
April 21, 2018 10:59
-
-
Save CesarValiente/ac7f2c467838011f9e6b0c4883e05ca4 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
abstract class Store(private val storeThread: ThreadExecutor? = null) { | |
private var actions = LinkedBlockingQueue<Action>() | |
val stateHandlers: CopyOnWriteArrayList<StateHandler> = CopyOnWriteArrayList() | |
val sideEffects: CopyOnWriteArrayList<SideEffect> = CopyOnWriteArrayList() | |
var state = State() | |
protected set | |
@Synchronized | |
fun dispatch(action: Action) { | |
actions.offer(action) | |
when { | |
storeThread != null -> storeThread.execute { handle(actions.poll()) } | |
else -> handle(actions.poll()) | |
} | |
} | |
private fun handle(action: Action) { | |
val newState = reduce(action, state) | |
dispatch(newState) | |
sideEffects.dispatch(action) | |
} | |
private fun dispatch(state: State) { | |
this.state = state | |
stateHandlers.dispatch(state) | |
} | |
// --- The Reducer will be here but we see it in the next section, when we talk about it --- // | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment