Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLambion committed Nov 29, 2020
1 parent c8a8dd1 commit 3b0693d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions Sources/SwiftDux/Action/ActionSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extension Publisher where Output == Action, Failure == Never {
/// - Returns: A cancellable to unsubscribe.
public func send(to actionDispatcher: ActionDispatcher) -> AnyCancellable {
let subscriber = ActionSubscriber(actionDispatcher: actionDispatcher)

self.subscribe(subscriber)
return AnyCancellable { subscriber.cancel() }
}
Expand Down
8 changes: 0 additions & 8 deletions Sources/SwiftDux/Middleware/ReduceMiddleware.swift

This file was deleted.

2 changes: 2 additions & 0 deletions Sources/SwiftDux/Middleware/ReducerMiddleware.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Foundation

// Reduces the state of a store with the provided action to produce a new state.
internal final class ReducerMiddleware<State, RootReducer>: Middleware where RootReducer: Reducer, RootReducer.State == State {
let reducer: CompositeReducer<State, StoreReducer<State>, RootReducer>
Expand Down
26 changes: 22 additions & 4 deletions Sources/SwiftDux/State/OrderedState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public struct OrderedState<Substate> where Substate: Identifiable {
valueByIndex[$0.id] = $0
return $0.id
}

self.init(orderOfIds: orderOfIds, values: valueByIndex)
}

Expand All @@ -148,6 +149,7 @@ public struct OrderedState<Substate> where Substate: Identifiable {
guard isKnownUniquelyReferenced(&storage) else {
return OrderedStateStorage(orderOfIds: storage.orderOfIds, values: storage.values)
}

storage.invalidateCache()
return storage
}
Expand All @@ -167,7 +169,9 @@ public struct OrderedState<Substate> where Substate: Identifiable {
/// - Parameter value: A new substate to append to the end of the list.
@inlinable public mutating func append(_ value: Substate) {
self.remove(forId: value.id) // Remove if it already exists.

let copy = copyStorageIfNeeded()

copy.orderOfIds.append(value.id)
copy.values[value.id] = value
self.storage = copy
Expand Down Expand Up @@ -210,6 +214,7 @@ public struct OrderedState<Substate> where Substate: Identifiable {
copy.values[value.id] = value
return value.id
}

copy.orderOfIds.insert(contentsOf: ids, at: index)
self.storage = copy
}
Expand All @@ -220,9 +225,11 @@ public struct OrderedState<Substate> where Substate: Identifiable {
@inlinable public mutating func remove(forId id: Id) {
guard storage.values[id] != nil else { return }
let copy = copyStorageIfNeeded()

if copy.values.removeValue(forKey: id) != nil {
copy.orderOfIds.removeAll { $0 == id }
}

self.storage = copy
}

Expand All @@ -231,6 +238,7 @@ public struct OrderedState<Substate> where Substate: Identifiable {
/// - Parameter index: The index of the substate to remove. This will adjust the order of items.
@inlinable public mutating func remove(at index: Int) {
let copy = copyStorageIfNeeded()

copy.values.removeValue(forKey: copy.orderOfIds[index])
copy.orderOfIds.remove(at: index)
self.storage = copy
Expand All @@ -241,6 +249,7 @@ public struct OrderedState<Substate> where Substate: Identifiable {
/// - Parameter indexSet: Removes all items in the provided indexSet. This will adjust the order of items.
@inlinable public mutating func remove(at indexSet: IndexSet) {
let copy = copyStorageIfNeeded()

indexSet.forEach { copy.values.removeValue(forKey: copy.orderOfIds[$0]) }
copy.orderOfIds.remove(at: indexSet)
self.storage = copy
Expand All @@ -252,12 +261,12 @@ public struct OrderedState<Substate> where Substate: Identifiable {
/// - indexSet: A set of indexes to move to a new location. The order of indexes will be used as part of the new order of the moved items.
/// - index: The new position for the moved items.
@inlinable public mutating func move(from indexSet: IndexSet, to index: Int) {
/// don't allow moving if the moved index is inside the index set.
guard !indexSet.contains(where: { $0 == index }) else { return }
let copy = copyStorageIfNeeded()
let index = Swift.max(Swift.min(index, copy.orderOfIds.count), 0)
let ids = Array(indexSet.map { copy.orderOfIds[$0] })
let offset = Swift.max(indexSet.reduce(0) { (result, i) in i < index ? result + 1 : result }, 0)

copy.orderOfIds.remove(at: indexSet)
copy.orderOfIds.insert(contentsOf: ids, at: index - offset)
self.storage = copy
Expand All @@ -268,6 +277,7 @@ public struct OrderedState<Substate> where Substate: Identifiable {
/// - Parameter areInIncreasingOrder: Orders the items by indicating whether not the second item is bigger than the first item.
@inlinable public mutating func sort(by areInIncreasingOrder: (Substate, Substate) -> Bool) {
let copy = copyStorageIfNeeded()

copy.orderOfIds.sort { areInIncreasingOrder(copy.values[$0]!, copy.values[$1]!) }
self.storage = copy
}
Expand All @@ -277,8 +287,10 @@ public struct OrderedState<Substate> where Substate: Identifiable {
/// - Parameter areInIncreasingOrder: Orders the items by indicating whether not the second item is bigger than the first item.
/// - Returns: A new `OrderedState` with the provided sort operation.
@inlinable public func sorted(by areInIncreasingOrder: (Substate, Substate) -> Bool) -> Self {
let orderOfIds = storage.orderOfIds.sorted { areInIncreasingOrder(storage.values[$0]!, storage.values[$1]!) }
return OrderedState(orderOfIds: orderOfIds, values: storage.values)
OrderedState(
orderOfIds: storage.orderOfIds.sorted { areInIncreasingOrder(storage.values[$0]!, storage.values[$1]!) },
values: storage.values
)
}

/// Filters the substates using a predicate.
Expand All @@ -288,7 +300,7 @@ public struct OrderedState<Substate> where Substate: Identifiable {
@inlinable public func filter(_ isIncluded: (Substate) -> Bool) -> [Substate] {
storage.orderOfIds.compactMap { id -> Substate? in
let value = storage.values[id]!
return isIncluded(value) ? value : nil
return isIncluded(storage.values[id]!) ? value : nil
}
}
}
Expand Down Expand Up @@ -328,8 +340,10 @@ extension OrderedState: MutableCollection {
}
set(newValue) {
guard let newValue = newValue else { return }

if storage.values[position] != nil {
let copy = copyStorageIfNeeded()

copy.values[position] = newValue
self.storage = copy
} else {
Expand Down Expand Up @@ -377,6 +391,7 @@ extension RangeReplaceableCollection where Self: MutableCollection, Index == Int
guard var i = indexes.first, i < count else { return }
var j = index(after: i)
var k = indexes.integerGreaterThan(i) ?? endIndex

while j != endIndex {
if k != j {
swapAt(i, j)
Expand All @@ -386,6 +401,7 @@ extension RangeReplaceableCollection where Self: MutableCollection, Index == Int
}
formIndex(after: &j)
}

removeSubrange(i...)
}
}
Expand All @@ -408,13 +424,15 @@ extension OrderedState: Decodable where Substate: Decodable {
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
var values = [Substate]()

while !container.isAtEnd {
do {
values.append(try container.decode(Substate.self))
} catch {
_ = try container.decode(IgnoredDecodedState.self)
}
}

self.init(values)
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftDux/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ extension Store: ActionDispatcher {
@inlinable public func send(_ action: Action) {
if let action = action as? RunnableAction {
reduceRunnableAction(action)
} else {
reduce(action)
}
reduce(action)
}

/// Sends an action to mutate the state.
Expand All @@ -78,6 +79,7 @@ extension Store: ActionDispatcher {
/// - Parameter action: The action to perform.
@usableFromInline internal func reduceRunnableAction(_ action: RunnableAction) {
var cancellable: AnyCancellable? = nil

cancellable = action.run(store: self.proxy())
.handleEvents(receiveCompletion: { _ in
cancellable?.cancel()
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftDux/UI/ViewModifiers/OnActionViewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ public struct OnActionViewModifier: ViewModifier {

public func body(content: Content) -> some View {
var nextActionDispatcher = actionDispatcher

if let perform = perform {
nextActionDispatcher = OnActionDispatcher(actionModifier: perform, nextDispatcher: actionDispatcher)
}

return content.environment(\.actionDispatcher, nextActionDispatcher)
}
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDuxExtras/Persistence/PersistSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extension Publisher where Output: Codable, Failure == Never {
/// - Returns: A cancellable to unsubscribe.
public func persist<P>(with persistor: P) -> AnyCancellable where P: StatePersistor, P.State == Output {
let subscriber = PersistSubscriber(persistor: persistor)

self.subscribe(subscriber)
return AnyCancellable { [subscriber] in subscriber.cancel() }
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftDuxExtras/PrintActionMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class PrintActionMiddleware<State>: Middleware {
if filter(action) {
printer(String(describing: action))
}

return action
}
}

0 comments on commit 3b0693d

Please sign in to comment.