From 27a848da95d98a348234da17719853f3359c6a6a Mon Sep 17 00:00:00 2001 From: Alex Usbergo Date: Tue, 19 May 2020 10:29:40 +0200 Subject: [PATCH] Template Actions --- Sources/Store/TemplateActions.swift | 54 +++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/Sources/Store/TemplateActions.swift b/Sources/Store/TemplateActions.swift index e588882..71241dc 100644 --- a/Sources/Store/TemplateActions.swift +++ b/Sources/Store/TemplateActions.swift @@ -3,13 +3,6 @@ import os.log // MARK: - Template Actions -public enum KeyPath { - /// A non-optional writeable keyPath. - case value(keyPath: WritableKeyPath) - /// A optional writeable keyPath. - case optional(keyPath: WritableKeyPath) -} - /// General-purpose actions that can be applied to any store. public struct TemplateAction { @@ -17,6 +10,16 @@ public struct TemplateAction { public let keyPath: KeyPath public let value: V? + public init(_ keyPath: WritableKeyPath, _ value: V) { + self.keyPath = .value(keyPath: keyPath) + self.value = value + } + + public init(_ keyPath: WritableKeyPath, _ value: V?) { + self.keyPath = .optional(keyPath: keyPath) + self.value = value + } + public func reduce(context: TransactionContext, Self>) { defer { context.fulfill() } context.reduceModel { model in @@ -29,6 +32,16 @@ public struct TemplateAction { public let keyPath: KeyPath public let isIncluded: (T) -> Bool + public init(_ keyPath: WritableKeyPath, _ isIncluded: @escaping (T) -> Bool) { + self.keyPath = .value(keyPath: keyPath) + self.isIncluded = isIncluded + } + + public init(_ keyPath: WritableKeyPath, _ isIncluded: @escaping (T) -> Bool) { + self.keyPath = .optional(keyPath: keyPath) + self.isIncluded = isIncluded + } + public func reduce(context: TransactionContext, Self>) { defer { context.fulfill() } context.reduceModel { model in @@ -41,6 +54,16 @@ public struct TemplateAction { public let keyPath: KeyPath public let index: Int + public init(_ keyPath: WritableKeyPath, index: Int) { + self.keyPath = .value(keyPath: keyPath) + self.index = index + } + + public init(_ keyPath: WritableKeyPath, index: Int) { + self.keyPath = .optional(keyPath: keyPath) + self.index = index + } + public func reduce(context: TransactionContext, Self>) { defer { context.fulfill() } context.reduceModel { model in @@ -52,6 +75,16 @@ public struct TemplateAction { public struct Push: ActionProtocol where V.Element == T { public let keyPath: KeyPath public let object: T + + public init(_ keyPath: WritableKeyPath, object: T) { + self.keyPath = .value(keyPath: keyPath) + self.object = object + } + + public init(_ keyPath: WritableKeyPath, object: T) { + self.keyPath = .optional(keyPath: keyPath) + self.object = object + } public func reduce(context: TransactionContext, Self>) { defer { context.fulfill() } @@ -62,6 +95,13 @@ public struct TemplateAction { } } +public enum KeyPath { + /// A non-optional writeable keyPath. + case value(keyPath: WritableKeyPath) + /// A optional writeable keyPath. + case optional(keyPath: WritableKeyPath) +} + private func _mutateArray( object: inout M, keyPath: KeyPath,