Skip to content

Instantly share code, notes, and snippets.

@CesarValiente
Last active April 21, 2018 10:59
Show Gist options
  • Save CesarValiente/ac7f2c467838011f9e6b0c4883e05ca4 to your computer and use it in GitHub Desktop.
Save CesarValiente/ac7f2c467838011f9e6b0c4883e05ca4 to your computer and use it in GitHub Desktop.
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