Skip to content

Commit

Permalink
Adds ID for stores
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Usbergo committed Jun 6, 2020
1 parent 8a39a46 commit b957547
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
34 changes: 17 additions & 17 deletions Sources/Store/action/TemplateAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct TemplateAction {
public let id: String
public let reduce: (inout M) -> Void

public init(id: String = _Id.reduce, reduce: @escaping (inout M) -> Void) {
public init(id: String = _ID.reduce, reduce: @escaping (inout M) -> Void) {
self.id = id
self.reduce = reduce
}
Expand All @@ -34,19 +34,19 @@ public struct TemplateAction {
public let keyPath: KeyPathArg<M, V>
public let value: V?

public init(id: String = _Id.assign, _ keyPath: KeyPathArg<M, V>, _ value: V) {
public init(id: String = _ID.assign, _ keyPath: KeyPathArg<M, V>, _ value: V) {
self.id = id
self.keyPath = keyPath
self.value = value
}

public init(id: String = _Id.assign, _ keyPath: WritableKeyPath<M, V>, _ value: V) {
public init(id: String = _ID.assign, _ keyPath: WritableKeyPath<M, V>, _ value: V) {
self.id = id
self.keyPath = .value(keyPath: keyPath)
self.value = value
}

public init(id: String = _Id.assign,_ keyPath: WritableKeyPath<M, V?>, _ value: V?) {
public init(id: String = _ID.assign,_ keyPath: WritableKeyPath<M, V?>, _ value: V?) {
self.id = id
self.keyPath = .optional(keyPath: keyPath)
self.value = value
Expand All @@ -73,7 +73,7 @@ public struct TemplateAction {
public let isIncluded: (V.Element) -> Bool

public init(
id: String = _Id.filter,
id: String = _ID.filter,
_ keyPath: KeyPathArg<M, V>,
_ isIncluded: @escaping (V.Element) -> Bool
) {
Expand All @@ -83,7 +83,7 @@ public struct TemplateAction {
}

public init(
id: String = _Id.filter,
id: String = _ID.filter,
_ keyPath: WritableKeyPath<M, V>,
_ isIncluded: @escaping (V.Element) -> Bool
) {
Expand All @@ -93,7 +93,7 @@ public struct TemplateAction {
}

public init(
id: String = _Id.filter,
id: String = _ID.filter,
_ keyPath: WritableKeyPath<M, V?>,
_ isIncluded: @escaping (V.Element) -> Bool
) {
Expand Down Expand Up @@ -122,19 +122,19 @@ public struct TemplateAction {
public let keyPath: KeyPathArg<M, V>
public let index: Int

public init(id: String = _Id.remove, _ keyPath: KeyPathArg<M, V>, index: Int) {
public init(id: String = _ID.remove, _ keyPath: KeyPathArg<M, V>, index: Int) {
self.id = id
self.keyPath = keyPath
self.index = index
}

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

public init(id: String = _Id.remove, _ keyPath: WritableKeyPath<M, V?>, index: Int) {
public init(id: String = _ID.remove, _ keyPath: WritableKeyPath<M, V?>, index: Int) {
self.id = id
self.keyPath = .optional(keyPath: keyPath)
self.index = index
Expand All @@ -160,19 +160,19 @@ public struct TemplateAction {
public let keyPath: KeyPathArg<M, V>
public let object: V.Element

public init(id: String = _Id.append, _ keyPath: KeyPathArg<M, V>, object: V.Element) {
public init(id: String = _ID.append, _ keyPath: KeyPathArg<M, V>, object: V.Element) {
self.id = id
self.keyPath = keyPath
self.object = object
}

public init(id: String = _Id.append, _ keyPath: WritableKeyPath<M, V>, object: V.Element) {
public init(id: String = _ID.append, _ keyPath: WritableKeyPath<M, V>, object: V.Element) {
self.id = id
self.keyPath = .value(keyPath: keyPath)
self.object = object
}

public init(id: String = _Id.append, _ keyPath: WritableKeyPath<M, V?>, object: V.Element) {
public init(id: String = _ID.append, _ keyPath: WritableKeyPath<M, V?>, object: V.Element) {
self.id = id
self.keyPath = .optional(keyPath: keyPath)
self.object = object
Expand All @@ -198,19 +198,19 @@ public struct TemplateAction {
public let keyPath: KeyPathArg<M, V>
public let object: V.Element

public init(id: String = _Id.push, _ keyPath: KeyPathArg<M, V>, object: V.Element) {
public init(id: String = _ID.push, _ keyPath: KeyPathArg<M, V>, object: V.Element) {
self.id = id
self.keyPath = keyPath
self.object = object
}

public init(id: String = _Id.push, _ keyPath: WritableKeyPath<M, V>, object: V.Element) {
public init(id: String = _ID.push, _ keyPath: WritableKeyPath<M, V>, object: V.Element) {
self.id = id
self.keyPath = .value(keyPath: keyPath)
self.object = object
}

public init(id: String = _Id.push, _ keyPath: WritableKeyPath<M, V?>, object: V.Element) {
public init(id: String = _ID.push, _ keyPath: WritableKeyPath<M, V?>, object: V.Element) {
self.id = id
self.keyPath = .optional(keyPath: keyPath)
self.object = object
Expand Down Expand Up @@ -238,7 +238,7 @@ public enum KeyPathArg<M, V> {
case optional(keyPath: WritableKeyPath<M, V?>)
}

public struct _Id {
public struct _ID {
public static let reduce = "__template_reduce"
public static let assign = "__template_assign"
public static let filter = "__template_filter"
Expand Down
13 changes: 11 additions & 2 deletions Sources/Store/store/CodableStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ open class CodableStore<M: Codable>: Store<M> {
/// Last serialized snapshot for the model.
private var _lastModelSnapshot: [FlatEncoding.KeyPath: Codable?] = [:]

public init(model: M, diffing: Diffing = .async) {
public init(
id: StoreID = singletonID,
model: M,
diffing: Diffing = .async
) {
self.diffing = diffing
super.init(model: model)
self._lastModelSnapshot = CodableStore.encodeFlat(model: model)
}

public init<P>(model: M, diffing: Diffing = .async, combine: CombineStore<P, M>) {
public init<P>(
id: StoreID = singletonID,
model: M,
diffing: Diffing = .async,
combine: CombineStore<P, M>
) {
self.diffing = diffing
super.init(model: model, combine: combine)
self._lastModelSnapshot = CodableStore.encodeFlat(model: model)
Expand Down
32 changes: 27 additions & 5 deletions Sources/Store/store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Combine
import Foundation

public protocol AnyStoreProtocol: class {
var id: String { get }
/// In charge of reconciling this store with its parent one (if applicable).
var combine: AnyCombineStore? { get }
/// All of the registered middleware.
Expand Down Expand Up @@ -30,7 +31,9 @@ public protocol StoreProtocol: AnyStoreProtocol {
func reduceModel(transaction: TransactionProtocol?, closure: (inout ModelType) -> Void)
}

open class Store<M>: StoreProtocol, ObservableObject {
open class Store<M>: StoreProtocol, ObservableObject, Identifiable {
/// The stable identity of the entity.
public var id: String
/// A publisher that emits before the object has changed.
public let objectWillChange = ObservableObjectPublisher()
/// The current state of this store.
Expand All @@ -43,13 +46,15 @@ open class Store<M>: StoreProtocol, ObservableObject {
private var _stateLock = SpinLock()
private var _performWithoutNotifyingObservers: Bool = false

public init(model: M) {
public init(id: StoreID = singletonID, model: M) {
self.id = id.id
self.model = model
self.combine = nil
register(middleware: LoggerMiddleware())
}

public init<P>(model: M, combine: CombineStore<P, M>) {
public init<P>(id: StoreID = singletonID, model: M, combine: CombineStore<P, M>) {
self.id = id.id
self.model = model
self.combine = combine
register(middleware: LoggerMiddleware())
Expand Down Expand Up @@ -109,8 +114,11 @@ open class Store<M>: StoreProtocol, ObservableObject {

// MARK: Child/Parent Store

public func makeChildStore<C>(keyPath: WritableKeyPath<M, C>) -> Store<C> {
Store<C>(model: model[keyPath: keyPath], combine: CombineStore(
public func makeChildStore<C>(
id: StoreID = singletonID,
keyPath: WritableKeyPath<M, C>
) -> Store<C> {
Store<C>(id: id, model: model[keyPath: keyPath], combine: CombineStore(
parent: self,
notify: true,
merge: .keyPath(keyPath: keyPath)))
Expand Down Expand Up @@ -164,4 +172,18 @@ open class Store<M>: StoreProtocol, ObservableObject {
transactions.run(handler: handler)
return transactions
}

/// Returns the store default identifier (applicable for singleton store)
public static var singletonID: StoreID { StoreID.init(type: self) }
}

// MARK: - ID

public struct StoreID {
/// The unique identifier for the store.
public let id: String

init<T>(type: T.Type, id: String = "_singleton") {
self.id = "\(String(describing: type)):\(id)"
}
}
1 change: 0 additions & 1 deletion Sources/Store/transactions/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public final class Transaction<A: ActionProtocol>: TransactionProtocol, Identifi
os_log(.error, log: OSLog.primary, "context/store is nil - the operation won't be executed.")
return
}

let context = TransactionContext(
operation: operation,
store: store,
Expand Down

0 comments on commit b957547

Please sign in to comment.