Skip to content

Commit

Permalink
0.20.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 11, 2024
1 parent 9f75d61 commit 666cdab
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/VDStore.git", from: "0.19.0")
.package(url: "https://github.com/dankinsoid/VDStore.git", from: "0.20.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["VDStore"])
Expand Down
31 changes: 16 additions & 15 deletions Sources/VDStore/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public struct Store<State> {

/// Injected dependencies.
public var di: StoreDIValues {
diStorage.with(store: self)
diModifier(StoreDIValues().with(store: self))
}

/// A publisher that emits when state changes.
Expand All @@ -117,7 +117,7 @@ public struct Store<State> {
}

private let box: StoreBox<State>
private var diStorage: StoreDIValues
private let diModifier: (StoreDIValues) -> StoreDIValues

public var wrappedValue: State {
get { state }
Expand All @@ -136,18 +136,15 @@ public struct Store<State> {

/// Creates a new `Store` with the initial state.
public nonisolated init(_ state: State) {
self.init(
box: StoreBox(state),
di: StoreDIValues()
)
self.init(box: StoreBox(state))
}

nonisolated init(
box: StoreBox<State>,
di: StoreDIValues
di: @escaping (StoreDIValues) -> StoreDIValues = { $0 }
) {
self.box = box
diStorage = di
diModifier = di
}

/// Scopes the store to one that exposes child state.
Expand Down Expand Up @@ -188,7 +185,7 @@ public struct Store<State> {
) -> Store<ChildState> {
Store<ChildState>(
box: StoreBox<ChildState>(parent: box, get: getter, set: setter),
di: di
di: { [self] in diModifier($0).with(store: self) }
)
}

Expand Down Expand Up @@ -279,21 +276,25 @@ public struct Store<State> {
/// - transform: A closure that transforms the store's dependencies.
/// - Returns: A new store with the transformed dependencies.
public func transformDI(
_ transform: (StoreDIValues) -> StoreDIValues
_ transform: @escaping (StoreDIValues) -> StoreDIValues
) -> Store {
Store(box: box, di: transform(diStorage))
Store(box: box) { [diModifier] in
transform(diModifier($0))
}
}

/// Transforms the store's injected dependencies.
/// - Parameters:
/// - transform: A closure that transforms the store's dependencies.
/// - Returns: A new store with the transformed dependencies.
public func transformDI(
_ transform: (inout StoreDIValues) -> Void
_ transform: @escaping (inout StoreDIValues) -> Void
) -> Store {
var dependencies = diStorage
transform(&dependencies)
return Store(box: box, di: dependencies)
transformDI {
var result = $0
transform(&result)
return result
}
}

/// Suspends the store from updating the UI until the block returns.
Expand Down
1 change: 1 addition & 0 deletions Tests/VDStoreTests/VDStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ final class VDStoreTests: XCTestCase {
let service: SomeService = MockSomeService()
let parentStore = Store(Counter()).di(\.someService, service)
let childStore = parentStore.counter
XCTAssert(parentStore.di.someService === service)
XCTAssert(childStore.di.someService === service)
}

Expand Down

0 comments on commit 666cdab

Please sign in to comment.