@@ -9,14 +9,14 @@ import ReactiveSwift
9
9
public final class Store < State, Action> {
10
10
@MutableProperty
11
11
private( set) var state : State
12
-
12
+
13
13
private var isSending = false
14
14
private let reducer : ( inout State , Action ) -> Effect < Action , Never >
15
15
private var synchronousActionsToSend : [ Action ] = [ ]
16
16
private var bufferedActions : [ Action ] = [ ]
17
17
internal var effectDisposables : [ UUID : Disposable ] = [ : ]
18
18
internal var parentDisposable : Disposable ?
19
-
19
+
20
20
/// Initializes a store from an initial state, a reducer, and an environment.
21
21
///
22
22
/// - Parameters:
@@ -33,7 +33,7 @@ public final class Store<State, Action> {
33
33
reducer: { reducer. run ( & $0, $1, environment) }
34
34
)
35
35
}
36
-
36
+
37
37
/// Scopes the store to one that exposes local state and actions.
38
38
///
39
39
/// This can be useful for deriving new stores to hand to child views in an application. For
@@ -179,7 +179,7 @@ public final class Store<State, Action> {
179
179
}
180
180
return localStore
181
181
}
182
-
182
+
183
183
/// Scopes the store to one that exposes local state.
184
184
///
185
185
/// - Parameter toLocalState: A function that transforms `State` into `LocalState`.
@@ -189,7 +189,7 @@ public final class Store<State, Action> {
189
189
) -> Store < LocalState , Action > {
190
190
self . scope ( state: toLocalState, action: { $0 } )
191
191
}
192
-
192
+
193
193
/// Scopes the store to a producer of stores of more local state and local actions.
194
194
///
195
195
/// - Parameters:
@@ -201,14 +201,14 @@ public final class Store<State, Action> {
201
201
state toLocalState: @escaping ( Effect < State , Never > ) -> Effect < LocalState , Never > ,
202
202
action fromLocalAction: @escaping ( LocalAction ) -> Action
203
203
) -> Effect < Store < LocalState , LocalAction > , Never > {
204
-
204
+
205
205
func extractLocalState( _ state: State ) -> LocalState ? {
206
206
var localState : LocalState ?
207
207
_ = toLocalState ( Effect ( value: state) )
208
208
. startWithValues { localState = $0 }
209
209
return localState
210
210
}
211
-
211
+
212
212
return toLocalState ( self . $state. producer)
213
213
. map { localState in
214
214
let localStore = Store < LocalState , LocalAction > (
@@ -219,15 +219,16 @@ public final class Store<State, Action> {
219
219
return . none
220
220
}
221
221
)
222
-
223
- localStore. parentDisposable = self . $state. producer. startWithValues { [ weak localStore] state in
222
+
223
+ localStore. parentDisposable = self . $state. producer. startWithValues {
224
+ [ weak localStore] state in
224
225
guard let localStore = localStore else { return }
225
226
localStore. state = extractLocalState ( state) ?? localStore. state
226
227
}
227
228
return localStore
228
229
}
229
230
}
230
-
231
+
231
232
/// Scopes the store to a producer of stores of more local state and local actions.
232
233
///
233
234
/// - Parameter toLocalState: A function that transforms a producer of `State` into a producer
@@ -239,30 +240,30 @@ public final class Store<State, Action> {
239
240
) -> Effect < Store < LocalState , Action > , Never > {
240
241
self . producerScope ( state: toLocalState, action: { $0 } )
241
242
}
242
-
243
+
243
244
func send( _ action: Action ) {
244
245
if !self . isSending {
245
246
self . synchronousActionsToSend. append ( action)
246
247
} else {
247
248
self . bufferedActions. append ( action)
248
249
return
249
250
}
250
-
251
+
251
252
while !self . synchronousActionsToSend. isEmpty || !self . bufferedActions. isEmpty {
252
253
let action =
253
254
!self . synchronousActionsToSend. isEmpty
254
255
? self . synchronousActionsToSend. removeFirst ( )
255
256
: self . bufferedActions. removeFirst ( )
256
-
257
+
257
258
self . isSending = true
258
259
let effect = self . reducer ( & self . state, action)
259
260
self . isSending = false
260
-
261
+
261
262
var didComplete = false
262
263
let effectID = UUID ( )
263
-
264
+
264
265
var isProcessingEffects = true
265
-
266
+
266
267
let observer = Signal < Action , Never > . Observer (
267
268
value: { [ weak self] action in
268
269
if isProcessingEffects {
@@ -283,34 +284,34 @@ public final class Store<State, Action> {
283
284
)
284
285
let effectDisposable = effect. start ( observer)
285
286
isProcessingEffects = false
286
-
287
+
287
288
if !didComplete {
288
289
self . effectDisposables [ effectID] = effectDisposable
289
290
} else {
290
291
effectDisposable. dispose ( )
291
292
}
292
293
}
293
294
}
294
-
295
+
295
296
/// Returns a "stateless" store by erasing state to `Void`.
296
297
public var stateless : Store < Void , Action > {
297
298
self . scope ( state: { _ in ( ) } )
298
299
}
299
-
300
+
300
301
/// Returns an "actionless" store by erasing action to `Never`.
301
302
public var actionless : Store < State , Never > {
302
303
func absurd< A> ( _ never: Never ) -> A { }
303
304
return self . scope ( state: { $0 } , action: absurd)
304
305
}
305
-
306
+
306
307
private init (
307
308
initialState: State ,
308
309
reducer: @escaping ( inout State , Action ) -> Effect < Action , Never >
309
310
) {
310
311
self . reducer = reducer
311
312
self . state = initialState
312
313
}
313
-
314
+
314
315
deinit {
315
316
self . parentDisposable? . dispose ( )
316
317
self . effectDisposables. keys. forEach { id in
@@ -323,11 +324,11 @@ public final class Store<State, Action> {
323
324
@dynamicMemberLookup
324
325
public struct Produced < Value> : SignalProducerConvertible {
325
326
public let producer : Effect < Value , Never >
326
-
327
+
327
328
init ( by upstream: Effect < Value , Never > ) {
328
329
self . producer = upstream
329
330
}
330
-
331
+
331
332
/// Returns the resulting producer of a given key path.
332
333
public subscript< LocalValue> (
333
334
dynamicMember keyPath: KeyPath < Value , LocalValue >
@@ -337,9 +338,9 @@ public struct Produced<Value>: SignalProducerConvertible {
337
338
}
338
339
339
340
@available (
340
- * , deprecated,
341
- message:
342
- """
341
+ * , deprecated,
342
+ message:
343
+ """
343
344
Consider using `Produced<State>` instead, this typealias is added for backward compatibility and will be removed in the next major release.
344
345
"""
345
346
)
0 commit comments