-
Notifications
You must be signed in to change notification settings - Fork 36
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
Alex Usbergo
committed
May 19, 2020
1 parent
af55b7f
commit 28b427e
Showing
3 changed files
with
66 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,7 @@ public extension ActionProtocol { | |
} | ||
|
||
public final class UnspecifiedStore: Store<Void> { } | ||
|
||
|
||
|
||
|
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 | ||
import os.log | ||
|
||
// MARK: - Template Actions | ||
|
||
/// General-purpose actions that can be applied to any store. | ||
public struct TemplateAction { | ||
|
||
public struct AssignKeyPath<M, V>: ActionProtocol { | ||
public let keyPath: WritableKeyPath<M, V> | ||
public let value: V | ||
|
||
public func reduce(context: TransactionContext<Store<M>, Self>) { | ||
defer { context.fulfill() } | ||
context.reduceModel { model in model[keyPath: keyPath] = value } | ||
} | ||
} | ||
|
||
public struct AssignOptionalKeyPath<M, V>: ActionProtocol { | ||
public let keyPath: WritableKeyPath<M, V?> | ||
public let value: V? | ||
|
||
public func reduce(context: TransactionContext<Store<M>, Self>) { | ||
defer { context.fulfill() } | ||
context.reduceModel { model in model[keyPath: keyPath] = value } | ||
} | ||
} | ||
|
||
public struct FilterArrayAtKeyPath<M, V: Collection, T>: ActionProtocol where V.Element == T { | ||
public let keyPath: WritableKeyPath<M, V> | ||
public let isIncluded: (T) -> Bool | ||
|
||
public func reduce(context: TransactionContext<Store<M>, Self>) { | ||
defer { context.fulfill() } | ||
context.reduceModel { model in | ||
guard var array = model[keyPath: keyPath] as? [T] else { | ||
os_log(.error, log: OSLog.primary, " Arrays are the only collection type supported.") | ||
return | ||
} | ||
array = array.filter(isIncluded) | ||
// Trivial cast. | ||
guard let collection = array as? V else { return } | ||
model[keyPath: keyPath] = collection | ||
} | ||
} | ||
} | ||
|
||
} |