@@ -11,6 +11,7 @@ public final class Store<State, Action> {
11
11
private var isSending = false
12
12
private let reducer : ( inout State , Action ) -> Effect < Action , Never >
13
13
private var synchronousActionsToSend : [ Action ] = [ ]
14
+ private var bufferedActions : [ Action ] = [ ]
14
15
15
16
/// Initializes a store from an initial state, a reducer, and an environment.
16
17
///
@@ -131,32 +132,18 @@ public final class Store<State, Action> {
131
132
}
132
133
133
134
func send( _ action: Action ) {
134
- self . synchronousActionsToSend. append ( action)
135
-
136
- while !self . synchronousActionsToSend. isEmpty {
137
- let action = self . synchronousActionsToSend. removeFirst ( )
138
-
139
- if self . isSending {
140
- assertionFailure (
141
- """
142
- The store was sent the action \( debugCaseOutput ( action) ) while it was already
143
- processing another action.
144
-
145
- This can happen for a few reasons:
146
-
147
- * The store was sent an action recursively. This can occur when you run an effect \
148
- directly in the reducer, rather than returning it from the reducer. Check the stack (⌘7) \
149
- to find frames corresponding to one of your reducers. That code should be refactored to \
150
- not invoke the effect directly.
151
-
152
- * The store has been sent actions from multiple threads. The `send` method is not \
153
- thread-safe, and should only ever be used from a single thread (typically the main \
154
- thread). Instead of calling `send` from multiple threads you should use effects to \
155
- process expensive computations on background threads so that it can be fed back into the \
156
- store.
157
- """
158
- )
159
- }
135
+ if !self . isSending {
136
+ self . synchronousActionsToSend. append ( action)
137
+ } else {
138
+ self . bufferedActions. append ( action)
139
+ return
140
+ }
141
+
142
+ while !self . synchronousActionsToSend. isEmpty || !self . bufferedActions. isEmpty {
143
+ let action = !self . synchronousActionsToSend. isEmpty
144
+ ? self . synchronousActionsToSend. removeFirst ( )
145
+ : self . bufferedActions. removeFirst ( )
146
+
160
147
self . isSending = true
161
148
let effect = self . reducer ( & self . state, action)
162
149
self . isSending = false
0 commit comments