Skip to content

Commit

Permalink
Simplify ReduceStore
Browse files Browse the repository at this point in the history
  • Loading branch information
yonekawa committed Nov 29, 2015
1 parent bdd3928 commit d767fbb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ Reducer should be pure and have no side-effects.

```swift
class CalculateStore: ReduceStore<Int> {
override init() {
super.init()
init() {
super.init(initialState: 0)

self.reduce(CalculateActions.Plus.self) { (state, result) -> Int in
switch result {
Expand All @@ -265,10 +265,6 @@ class CalculateStore: ReduceStore<Int> {
}
}
}

override var initialState: Int {
return 0
}
}
```

Expand Down
10 changes: 5 additions & 5 deletions SwiftFlux/Utils/ReduceStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import Result

public class ReduceStore<T: Equatable>: StoreBase {
override public init() {}
public init(initialState: T) {
self.initialState = initialState
super.init()
}

private var initialState: T
private var internalState: T?
public var state: T {
return internalState ?? initialState
}

public var initialState: T {
fatalError("\(self.dynamicType) has not overridden ReduceStore.initialState, which is required")
}

public func reduce<A: Action>(type: A.Type, reducer: (T, Result<A.Payload, A.Error>) -> T) -> String {
return self.register(type) { (result) in
let startState = self.state
Expand Down
8 changes: 2 additions & 6 deletions SwiftFluxTests/Utils/ReduceStoreSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class ReduceStoreSpec: QuickSpec {
}

class CalculateStore: ReduceStore<Int> {
override init() {
super.init()
init() {
super.init(initialState: 0)

self.reduce(CalculateActions.Plus.self) { (state, result) -> Int in
switch result {
Expand All @@ -47,10 +47,6 @@ class ReduceStoreSpec: QuickSpec {
}
}
}

override var initialState: Int {
return 0
}
}

override func spec() {
Expand Down

0 comments on commit d767fbb

Please sign in to comment.