Skip to content

Conversation

@vmaraccini
Copy link

@vmaraccini vmaraccini commented Jul 11, 2017

Make reductions thread-safe and make Store subscriptions fire on the Main Thread.

This PR proposes a way to make store reductions thread-safe by using objc_sync_enter and objc_sync_exit synchronization primitives, using the Store itself as a lock.

open func _defaultDispatch(action: Action) {
    objc_sync_enter(self)
    let newState = reducer(action, state)
    state = newState
    objc_sync_exit(self)
}

TODO:

  • Allow for the selection of a callback queue where newState methods will be called. The currently preferred way to do this would be to add an optional parameter when subscribing to store changes.

DispatchQueue.main.sync {
newState(state: typedState)
}
newState(state: typedState)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove the mainThread enforcement?

Copy link
Author

@vmaraccini vmaraccini Jul 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be optional; Always calling newState in the main thread may be unnecessary, or even undesired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants