-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actions mapping between parent & child stores (#25)
* implemented actions transformation * implemented actions mapping prototype * refactoring * refactoring * refactoring * moved to dir * refactoring * minor fix * refactoring * Refactoring
- Loading branch information
1 parent
b9e1f45
commit 1cd2e87
Showing
9 changed files
with
252 additions
and
123 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Sergey Kazakov on 12.11.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
struct ScopedAction<Action> { | ||
let storeId: StoreID | ||
let action: Action | ||
} | ||
|
||
struct ActionsMapping<GlobalAction, LocalAction> { | ||
let toGlobal: (LocalAction) -> GlobalAction | ||
let toLocal: (GlobalAction) -> LocalAction | ||
|
||
static func passthrough<A>() -> ActionsMapping<A, A> { | ||
ActionsMapping<A, A>( | ||
toGlobal: { $0 }, | ||
toLocal: { $0 } | ||
) | ||
} | ||
} | ||
|
||
struct ActionsInterceptor<Action> { | ||
let storeId: StoreID | ||
let handler: (Action, @escaping Dispatch<Action>) -> Void | ||
let dispatcher: Dispatch<Action> | ||
|
||
func interceptIfNeeded(_ action: ScopedAction<Action>) { | ||
guard action.storeId == storeId else { return } | ||
handler(action.action, dispatcher) | ||
} | ||
|
||
func localInterceptor<LocalAction>(_ localStoreId: StoreID, | ||
actionsMapping: ActionsMapping<Action, LocalAction>, | ||
dispatcher: @escaping Dispatch<LocalAction> | ||
) -> ActionsInterceptor<LocalAction> { | ||
|
||
ActionsInterceptor<LocalAction>( | ||
storeId: localStoreId, | ||
handler: { action, dispatcher in | ||
handler( | ||
actionsMapping.toGlobal(action), | ||
{ dispatcher(actionsMapping.toLocal($0)) } | ||
) | ||
}, | ||
dispatcher: { action in | ||
dispatcher(action) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Sergey Kazakov on 12/04/2024. | ||
// | ||
|
||
import Foundation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Sergey Kazakov on 12/04/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Store { | ||
|
||
func map<A>(_ transform: @escaping (A) -> Action) -> Store<State, A> { | ||
Store<State, A>( | ||
dispatcher: { action in dispatch(transform(action)) }, | ||
subscribe: subscribe | ||
) | ||
} | ||
} | ||
|
||
extension StateStore { | ||
|
||
func map<A>(_ transform: @escaping (A) -> Action) -> Store<State, A> { | ||
strongStore().map(transform) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Sergey Kazakov on 04/04/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Store { | ||
func map<T>(_ transform: @escaping (State) -> T) -> Store<T, Action> { | ||
Store<T, Action>( | ||
dispatcher: dispatch, | ||
subscribe: { localStateObserver in | ||
subscribe(observer: Observer<State>(id: localStateObserver.id) { state, complete in | ||
let localState = transform(state) | ||
localStateObserver.send(localState, complete: complete) | ||
}) | ||
} | ||
) | ||
} | ||
|
||
func compactMap<T>(_ transform: @escaping (State) -> T?) -> Store<T, Action> { | ||
Store<T, Action>( | ||
dispatcher: dispatch, | ||
subscribe: { localStateObserver in | ||
subscribe(observer: Observer<State>(id: localStateObserver.id) { state, complete in | ||
guard let localState = transform(state) else { | ||
complete(.active) | ||
return | ||
} | ||
|
||
localStateObserver.send(localState, complete: complete) | ||
}) | ||
} | ||
) | ||
} | ||
|
||
func flatMap<T>(_ transform: @escaping (State) -> T?) -> Store<T?, Action> { | ||
Store<T?, Action>( | ||
dispatcher: dispatch, | ||
subscribe: { localStateObserver in | ||
subscribe(observer: Observer<State>(id: localStateObserver.id) { state, complete in | ||
let localState = transform(state) | ||
localStateObserver.send(localState, complete: complete) | ||
}) | ||
} | ||
) | ||
} | ||
} | ||
|
||
extension Store { | ||
func map<T>(_ keyPath: KeyPath<State, T>) -> Store<T, Action> { | ||
map { $0[keyPath: keyPath] } | ||
} | ||
|
||
func compactMap<T>(_ keyPath: KeyPath<State, T?>) -> Store<T, Action> { | ||
compactMap { $0[keyPath: keyPath] } | ||
} | ||
|
||
func flatMap<T>(_ keyPath: KeyPath<State, T?>) -> Store<T?, Action> { | ||
flatMap { $0[keyPath: keyPath] } | ||
} | ||
} | ||
|
||
extension StateStore { | ||
func map<T>(_ keyPath: KeyPath<State, T>) -> Store<T, Action> { | ||
strongStore().map(keyPath) | ||
} | ||
|
||
func compactMap<T>(_ keyPath: KeyPath<State, T?>) -> Store<T, Action> { | ||
strongStore().compactMap(keyPath) | ||
} | ||
|
||
func flatMap<T>(_ keyPath: KeyPath<State, T?>) -> Store<T?, Action> { | ||
strongStore().flatMap(keyPath) | ||
} | ||
} | ||
|
||
extension StateStore { | ||
func map<T>(_ transform: @escaping (State) -> T) -> Store<T, Action> { | ||
strongStore().map(transform) | ||
} | ||
|
||
|
||
func compactMap<T>(_ transform: @escaping (State) -> T?) -> Store<T, Action> { | ||
strongStore().compactMap(transform) | ||
} | ||
|
||
func flatMap<T>(_ transform: @escaping (State) -> T?) -> Store<T?, Action> { | ||
strongStore().flatMap(transform) | ||
} | ||
} |
Oops, something went wrong.