Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safety #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Thread safety #1

wants to merge 5 commits into from

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.

2 participants