@@ -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,14 +219,15 @@ public final class Store<State, Action> {
219
219
return . none
220
220
}
221
221
)
222
- localStore. parentDisposable = self . $state. producer. startWithValues { [ weak localStore] state in
222
+ localStore. parentDisposable = self . $state. producer. startWithValues {
223
+ [ weak localStore] state in
223
224
guard let localStore = localStore else { return }
224
225
localStore. state = extractLocalState ( state) ?? localStore. state
225
226
}
226
227
return localStore
227
228
}
228
229
}
229
-
230
+
230
231
/// Scopes the store to a producer of stores of more local state and local actions.
231
232
///
232
233
/// - Parameter toLocalState: A function that transforms a producer of `State` into a producer
@@ -238,25 +239,25 @@ public final class Store<State, Action> {
238
239
) -> Effect < Store < LocalState , Action > , Never > {
239
240
self . producerScope ( state: toLocalState, action: { $0 } )
240
241
}
241
-
242
+
242
243
func send( _ action: Action ) {
243
244
if !self . isSending {
244
245
self . synchronousActionsToSend. append ( action)
245
246
} else {
246
247
self . bufferedActions. append ( action)
247
248
return
248
249
}
249
-
250
+
250
251
while !self . synchronousActionsToSend. isEmpty || !self . bufferedActions. isEmpty {
251
252
let action =
252
253
!self . synchronousActionsToSend. isEmpty
253
254
? self . synchronousActionsToSend. removeFirst ( )
254
255
: self . bufferedActions. removeFirst ( )
255
-
256
+
256
257
self . isSending = true
257
258
let effect = self . reducer ( & self . state, action)
258
259
self . isSending = false
259
-
260
+
260
261
var didComplete = false
261
262
let effectID = UUID ( )
262
263
@@ -281,34 +282,34 @@ public final class Store<State, Action> {
281
282
)
282
283
let effectDisposable = effect. start ( observer)
283
284
isProcessingEffects = false
284
-
285
+
285
286
if !didComplete {
286
287
self . effectDisposables [ effectID] = effectDisposable
287
288
} else {
288
289
effectDisposable. dispose ( )
289
290
}
290
291
}
291
292
}
292
-
293
+
293
294
/// Returns a "stateless" store by erasing state to `Void`.
294
295
public var stateless : Store < Void , Action > {
295
296
self . scope ( state: { _ in ( ) } )
296
297
}
297
-
298
+
298
299
/// Returns an "actionless" store by erasing action to `Never`.
299
300
public var actionless : Store < State , Never > {
300
301
func absurd< A> ( _ never: Never ) -> A { }
301
302
return self . scope ( state: { $0 } , action: absurd)
302
303
}
303
-
304
+
304
305
private init (
305
306
initialState: State ,
306
307
reducer: @escaping ( inout State , Action ) -> Effect < Action , Never >
307
308
) {
308
309
self . reducer = reducer
309
310
self . state = initialState
310
311
}
311
-
312
+
312
313
deinit {
313
314
self . parentDisposable? . dispose ( )
314
315
self . effectDisposables. keys. forEach { id in
@@ -322,30 +323,30 @@ public final class Store<State, Action> {
322
323
public struct Produced < Value> : SignalProducerConvertible {
323
324
private let _producer : Effect < Value , Never >
324
325
private let comparator : ( Value , Value ) -> Bool
325
-
326
+
326
327
public var producer : Effect < Value , Never > {
327
328
_producer. skipRepeats ( comparator)
328
329
}
329
-
330
+
330
331
init (
331
332
by upstream: Effect < Value , Never > ,
332
333
isEqual: @escaping ( Value , Value ) -> Bool
333
334
) {
334
335
self . _producer = upstream
335
336
self . comparator = isEqual
336
337
}
337
-
338
+
338
339
init ( by upstream: Effect < Value , Never > ) where Value: Equatable {
339
340
self . init ( by: upstream, isEqual: == )
340
341
}
341
-
342
+
342
343
/// Returns the resulting producer of a given key path.
343
344
public subscript< LocalValue> (
344
345
dynamicMember keyPath: KeyPath < Value , LocalValue >
345
346
) -> Effect < LocalValue , Never > where LocalValue: Equatable {
346
347
self . producer. map ( keyPath) . skipRepeats ( )
347
348
}
348
-
349
+
349
350
/// Returns the resulting producer of a given key path.
350
351
public subscript< LocalValue> (
351
352
dynamicMember keyPath: KeyPath < Value , LocalValue >
@@ -355,9 +356,9 @@ public struct Produced<Value>: SignalProducerConvertible {
355
356
}
356
357
357
358
@available (
358
- * , deprecated,
359
- message:
360
- """
359
+ * , deprecated,
360
+ message:
361
+ """
361
362
Consider using `Produced<State>` instead, this typealias is added for backward compatibility and will be removed in the next major release.
362
363
"""
363
364
)
0 commit comments