Skip to content

Commit

Permalink
Template Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Usbergo committed May 19, 2020
1 parent bc6cec2 commit 27a848d
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions Sources/Store/TemplateActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import os.log

// MARK: - Template Actions

public enum KeyPath<M, V> {
/// A non-optional writeable keyPath.
case value(keyPath: WritableKeyPath<M, V>)
/// A optional writeable keyPath.
case optional(keyPath: WritableKeyPath<M, V?>)
}

/// General-purpose actions that can be applied to any store.
public struct TemplateAction {

public struct AssignKeyPath<M, V>: ActionProtocol {
public let keyPath: KeyPath<M, V>
public let value: V?

public init(_ keyPath: WritableKeyPath<M, V>, _ value: V) {
self.keyPath = .value(keyPath: keyPath)
self.value = value
}

public init(_ keyPath: WritableKeyPath<M, V?>, _ value: V?) {
self.keyPath = .optional(keyPath: keyPath)
self.value = value
}

public func reduce(context: TransactionContext<Store<M>, Self>) {
defer { context.fulfill() }
context.reduceModel { model in
Expand All @@ -29,6 +32,16 @@ public struct TemplateAction {
public let keyPath: KeyPath<M, V>
public let isIncluded: (T) -> Bool

public init(_ keyPath: WritableKeyPath<M, V>, _ isIncluded: @escaping (T) -> Bool) {
self.keyPath = .value(keyPath: keyPath)
self.isIncluded = isIncluded
}

public init(_ keyPath: WritableKeyPath<M, V?>, _ isIncluded: @escaping (T) -> Bool) {
self.keyPath = .optional(keyPath: keyPath)
self.isIncluded = isIncluded
}

public func reduce(context: TransactionContext<Store<M>, Self>) {
defer { context.fulfill() }
context.reduceModel { model in
Expand All @@ -41,6 +54,16 @@ public struct TemplateAction {
public let keyPath: KeyPath<M, V>
public let index: Int

public init(_ keyPath: WritableKeyPath<M, V>, index: Int) {
self.keyPath = .value(keyPath: keyPath)
self.index = index
}

public init(_ keyPath: WritableKeyPath<M, V?>, index: Int) {
self.keyPath = .optional(keyPath: keyPath)
self.index = index
}

public func reduce(context: TransactionContext<Store<M>, Self>) {
defer { context.fulfill() }
context.reduceModel { model in
Expand All @@ -52,6 +75,16 @@ public struct TemplateAction {
public struct Push<M, V: Collection, T>: ActionProtocol where V.Element == T {
public let keyPath: KeyPath<M, V>
public let object: T

public init(_ keyPath: WritableKeyPath<M, V>, object: T) {
self.keyPath = .value(keyPath: keyPath)
self.object = object
}

public init(_ keyPath: WritableKeyPath<M, V?>, object: T) {
self.keyPath = .optional(keyPath: keyPath)
self.object = object
}

public func reduce(context: TransactionContext<Store<M>, Self>) {
defer { context.fulfill() }
Expand All @@ -62,6 +95,13 @@ public struct TemplateAction {
}
}

public enum KeyPath<M, V> {
/// A non-optional writeable keyPath.
case value(keyPath: WritableKeyPath<M, V>)
/// A optional writeable keyPath.
case optional(keyPath: WritableKeyPath<M, V?>)
}

private func _mutateArray<M, V: Collection, T>(
object: inout M,
keyPath: KeyPath<M, V>,
Expand Down

0 comments on commit 27a848d

Please sign in to comment.