Skip to content

Commit

Permalink
Restore parent store func
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Usbergo committed Jan 31, 2021
1 parent 41dd6f5 commit 56a9ecd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
{
"object": {
"pins": [
{
"package": "OpenCombine",
"repositoryURL": "https://github.com/OpenCombine/OpenCombine.git",
"state": {
"branch": null,
"revision": "28993ae57de5a4ea7e164787636cafad442d568c",
"version": "0.12.0"
}
},
{
"package": "Store",
"repositoryURL": "https://github.com/alexdrone/Store",
"state": {
"branch": "master",
"revision": "51b58ca7c52072356369fc41bf29a0e24bc48a93",
"revision": "41dd6f52d7c09f9ff200cbea9da9a9c1ece9fb39",
"version": null
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "173f567a2dfec11d74588eea82cecea555bdc0bc",
"version": "1.4.0"
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AppStateStore: CodableStore<AppState> {
}

func childStore(id: Item) -> Store<Item> {
Store(model: id, combine: CombineStore(parent: self))

}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/Store/store/CodableStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ open class CodableStore<M: Codable>: Store<M> {
keyPath: WritableKeyPath<M, C>
) -> CodableStore<C> where M: Codable, C: Codable {
let childModelStorage: ModelStorageBase<C> = modelStorage.makeChild(keyPath: keyPath)
return CodableStore<C>(modelStorage: childModelStorage)
let store = CodableStore<C>(modelStorage: childModelStorage)
store._parent = self
return store
}

// MARK: - Model Encode/Decode
Expand Down
18 changes: 17 additions & 1 deletion Sources/Store/store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public protocol AnyStore: class {
/// Manually notify all of the registered middleware services.
/// - note: See `MiddlewareType.onTransactionStateChange`.
func notifyMiddleware(transaction: AnyTransaction)

/// Recursively traverse the parents until it founds one that matches the specified model type.
func parent<T>(type: T.Type) -> Store<T>?
}

/// Represents a store that has an typed associated model.
Expand Down Expand Up @@ -89,6 +92,10 @@ open class Store<M>: ReducibleStore, Identifiable {
// See `AnyStore`.
public var middleware: [Middleware] = []
public private(set) var modelStorage: ModelStorageBase<M>

// Internal
var _parent: AnyStore?

// Private.
private var _performWithoutNotifyingObservers: Bool = false
private var _modelStorageObserver: AnyCancellable?
Expand Down Expand Up @@ -128,7 +135,16 @@ open class Store<M>: ReducibleStore, Identifiable {
/// - parameter keyPath: The keypath pointing at a subtree of the model object.
public func makeChildStore<C>(keyPath: WritableKeyPath<M, C>) -> Store<C> {
let childModelStorage: ModelStorageBase<C> = modelStorage.makeChild(keyPath: keyPath)
return Store<C>(modelStorage: childModelStorage)
let store = Store<C>(modelStorage: childModelStorage)
store._parent = self
return store
}

public func parent<T>(type: T.Type) -> Store<T>? {
if let parent = _parent as? Store<T> {
return parent
}
return _parent?.parent(type: type)
}

// MARK: Model updates
Expand Down

0 comments on commit 56a9ecd

Please sign in to comment.