-
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.
* implemented store map, flatMap, compactMap * store transform * updated store transformations * updated public Store API implemntation to use transformations under the hood * added explicit weak/strong ref stores * use weak store explicitly * updated transformations to use strong stores
- Loading branch information
1 parent
74fbc5c
commit 07cdd57
Showing
5 changed files
with
107 additions
and
30 deletions.
There are no files selected for viewing
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,90 @@ | ||
// | ||
// 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 map<T>(_ transform: @escaping (State) -> T) -> Store<T, Action> { | ||
strongStore().map(transform) | ||
} | ||
|
||
func compactMap<T>(_ keyPath: KeyPath<State, T?>) -> Store<T, Action> { | ||
strongStore().compactMap(keyPath) | ||
} | ||
|
||
func compactMap<T>(_ transform: @escaping (State) -> T?) -> Store<T, Action> { | ||
strongStore().compactMap(transform) | ||
} | ||
|
||
func flatMap<T>(_ keyPath: KeyPath<State, T?>) -> Store<T?, Action> { | ||
strongStore().flatMap(keyPath) | ||
} | ||
|
||
func flatMap<T>(_ transform: @escaping (State) -> T?) -> Store<T?, Action> { | ||
strongStore().flatMap(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
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