Add threading warning to Store.send (#772)

* Add threading warning to Store.send

Any async Effect (including fireAndForget) could result in a runtime crash when updating internal state like `effectCancellables`
This adds a debug warning to let users know

* Remove comment
This commit is contained in:
Ian Keen
2021-09-07 07:20:15 -07:00
committed by GitHub
parent 60dc806718
commit c77c6054c8

View File

@@ -382,8 +382,40 @@ public final class Store<State, Action> {
var didComplete = false
let uuid = UUID()
#if DEBUG
let initalThread = Thread.current
initalThread.threadDictionary[uuid] = true
#endif
let effectCancellable = effect.sink(
receiveCompletion: { [weak self] _ in
#if DEBUG
if Thread.current.threadDictionary[uuid] == nil {
breakpoint(
"""
---
Warning: Store.send
The Store class is not thread-safe, and so all interactions with an instance of Store
(including all of its scopes and derived ViewStores) must be done on the same thread.
\(debugCaseOutput(action)) has produced an Effect that was completed on a different thread \
from the one it was executed on.
Starting thread: \(initalThread)
Final thread: \(Thread.current)
Possible fixes for this are:
* Add a .receive(on:) to the Effect to ensure it completes on this Stores correct thread.
"""
)
}
Thread.current.threadDictionary[uuid] = nil
#endif
didComplete = true
self?.effectCancellables[uuid] = nil
},