generated from dankinsoid/iOSLibraryTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4faacd
commit 9f75d61
Showing
6 changed files
with
219 additions
and
68 deletions.
There are no files selected for viewing
Binary file modified
BIN
+572 Bytes
(100%)
...xcodeproj/project.xcworkspace/xcuserdata/danil.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
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,48 @@ | ||
import Foundation | ||
|
||
public extension Store { | ||
|
||
func or<T>(_ defaultValue: @escaping @autoclosure () -> T) -> Store<T> where T? == State { | ||
scope { | ||
$0 ?? defaultValue() | ||
} set: { | ||
$0 = $1 | ||
} | ||
} | ||
|
||
func onChange<V>( | ||
of keyPath: WritableKeyPath<State, V>, | ||
removeDuplicates isDuplicate: @escaping (V, V) -> Bool, | ||
_ operation: @MainActor @escaping (_ oldValue: V, _ newValue: V, inout State) -> Void | ||
) -> Store { | ||
scope { | ||
$0 | ||
} set: { | ||
let oldValue = $0[keyPath: keyPath] | ||
$0 = $1 | ||
operation(oldValue, $1[keyPath: keyPath], &$0) | ||
} | ||
} | ||
|
||
func onChange<V: Equatable>( | ||
of keyPath: WritableKeyPath<State, V>, | ||
_ operation: @MainActor @escaping (_ oldValue: V, _ newValue: V, inout State) -> Void | ||
) -> Store { | ||
onChange(of: keyPath, removeDuplicates: ==, operation) | ||
} | ||
} | ||
|
||
public extension Store where State: MutableCollection { | ||
|
||
func forEach(_ operation: @MainActor (Store<State.Element>) throws -> Void) rethrows { | ||
for index in state.indices { | ||
try operation(self[index]) | ||
} | ||
} | ||
|
||
func forEach(_ operation: @MainActor (Store<State.Element>) async throws -> Void) async rethrows { | ||
for index in state.indices { | ||
try await operation(self[index]) | ||
} | ||
} | ||
} |
Oops, something went wrong.