Skip to content

Commit

Permalink
Merge pull request #2 from SwiftRex/LiftToCollection
Browse files Browse the repository at this point in the history
Lift To Collection
  • Loading branch information
luizmb authored Sep 14, 2021
2 parents 13a8f2b + e8416e3 commit dafb6a2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
69 changes: 69 additions & 0 deletions Sources/BridgeMiddleware/BridgeMiddleware+LiftToCollection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation
import SwiftRex

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension BridgeMiddleware where StateType: Identifiable {
public func liftToCollection<GlobalAction, GlobalState, CollectionState: MutableCollection>(
inputAction actionMap: @escaping (GlobalAction) -> ElementIDAction<StateType.ID, InputActionType>?,
outputAction outputMap: @escaping (ElementIDAction<StateType.ID, OutputActionType>) -> GlobalAction,
stateCollection: @escaping (GlobalState) -> CollectionState
) -> BridgeMiddleware<GlobalAction, GlobalAction, GlobalState> where CollectionState.Element == StateType {
let mw = BridgeMiddleware<GlobalAction, GlobalAction, GlobalState>()
self.bridges.forEach { bridge in
mw.bridges.append(
BridgeMiddleware<GlobalAction, GlobalAction, GlobalState>.Bridge(
actionTransformation: { (globalInputAction: GlobalAction) -> GlobalAction? in
guard let localElementIDInputAction = actionMap(globalInputAction),
let localOutputAction = bridge.actionTransformation(localElementIDInputAction.action)
else { return nil }

return outputMap(.init(id: localElementIDInputAction.id, action: localOutputAction))
},
statePredicate: { (getState: GetState<GlobalState>, action: GlobalAction) -> Bool in
guard let itemAction = actionMap(action),
let itemState = stateCollection(getState()).first(where: { $0.id == itemAction.id })
else { return false }

return bridge.statePredicate({ itemState }, itemAction.action)
},
bridgedAtSource: bridge.bridgedAtSource
)
)
}
return mw
}
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension BridgeMiddleware where StateType: Identifiable, InputActionType == OutputActionType {
public func liftToCollection<GlobalAction, GlobalState, CollectionState: MutableCollection>(
action actionMap: WritableKeyPath<GlobalAction, ElementIDAction<StateType.ID, InputActionType>?>,
stateCollection: KeyPath<GlobalState, CollectionState>
) -> BridgeMiddleware<GlobalAction, GlobalAction, GlobalState> where CollectionState.Element == StateType {
let mw = BridgeMiddleware<GlobalAction, GlobalAction, GlobalState>()
self.bridges.forEach { bridge in
mw.bridges.append(
BridgeMiddleware<GlobalAction, GlobalAction, GlobalState>.Bridge(
actionTransformation: { (globalInputAction: GlobalAction) -> GlobalAction? in
guard let localElementIDInputAction = globalInputAction[keyPath: actionMap],
let localOutputAction = bridge.actionTransformation(localElementIDInputAction.action)
else { return nil }

var newAction = globalInputAction
newAction[keyPath: actionMap] = .init(id: localElementIDInputAction.id, action: localOutputAction)
return newAction
},
statePredicate: { (getState: GetState<GlobalState>, action: GlobalAction) -> Bool in
guard let itemAction = action[keyPath: actionMap],
let itemState = getState()[keyPath: stateCollection].first(where: { $0.id == itemAction.id })
else { return false }

return bridge.statePredicate({ itemState }, itemAction.action)
},
bridgedAtSource: bridge.bridgedAtSource
)
)
}
return mw
}
}
16 changes: 11 additions & 5 deletions Sources/BridgeMiddleware/BridgeMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public func >=> <A, B, C>(_ left: @escaping (A) -> B?, _ right: @escaping (B) ->
public class BridgeMiddleware<InputActionType, OutputActionType, StateType>: Middleware {
struct Bridge {
let actionTransformation: (InputActionType) -> OutputActionType?
let statePredicate: (GetState<StateType>) -> Bool
let statePredicate: (GetState<StateType>, InputActionType) -> Bool
let bridgedAtSource: ActionSource
}

private var bridges: [Bridge] = []
var bridges: [Bridge] = []

public init() { }

Expand Down Expand Up @@ -116,7 +116,7 @@ public class BridgeMiddleware<InputActionType, OutputActionType, StateType>: Mid
bridges.append(
Bridge(
actionTransformation: mapping,
statePredicate: stateAfterReducerPredicate,
statePredicate: { state, _ in stateAfterReducerPredicate(state) },
bridgedAtSource: ActionSource(file: file, function: function, line: line, info: nil)
)
)
Expand Down Expand Up @@ -146,7 +146,13 @@ public class BridgeMiddleware<InputActionType, OutputActionType, StateType>: Mid
line: UInt = #line,
function: String = #function
) -> BridgeMiddleware {
bridge(keyPathChecker >=> outputAction, when: stateAfterReducerPredicate, file: file, line: line, function: function)
bridge(
keyPathChecker >=> outputAction,
when: stateAfterReducerPredicate,
file: file,
line: line,
function: function
)
}

/// Bridge an action to another derived action
Expand Down Expand Up @@ -191,7 +197,7 @@ public class BridgeMiddleware<InputActionType, OutputActionType, StateType>: Mid
else { return }

self.bridges
.filter { actionBridge in actionBridge.statePredicate(getState) }
.filter { actionBridge in actionBridge.statePredicate(getState, action) }
.compactMap { actionBridge in
actionBridge
.actionTransformation(action)
Expand Down

0 comments on commit dafb6a2

Please sign in to comment.