Skip to content

Commit

Permalink
0.22.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 12, 2024
1 parent 4fe1c83 commit 2647867
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 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.21.0")
.package(url: "https://github.com/dankinsoid/VDStore.git", from: "0.22.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["VDStore"])
Expand Down
22 changes: 11 additions & 11 deletions Sources/VDStore/Utils/StoreBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,51 +62,51 @@ private final class StoreRootBox<State>: Publisher {
suspendSyncUpdates()
}
} else {
willSet.send()
willSetSubject.send()
}
}
}
didSet {
if updatesCounter == 0, asyncUpdatesCounter == 0 {
didSet.send()
didSetSubject.send()
}
}
}

var willSetPublisher: AnyPublisher<Void, Never> {
willSet.eraseToAnyPublisher()
willSetSubject.eraseToAnyPublisher()
}

private var updatesCounter = 0
private var asyncUpdatesCounter = 0
private let willSet = PassthroughSubject<Void, Never>()
private let didSet = PassthroughSubject<Void, Never>()
private let willSetSubject = PassthroughSubject<Void, Never>()
private let didSetSubject = PassthroughSubject<Void, Never>()

init(_ state: State) {
self.state = state
}

func receive<S>(subscriber: S) where S: Subscriber, Never == S.Failure, Output == S.Input {
didSet
didSetSubject
.compactMap { [weak self] in self?.state }
.prepend(state)
.receive(subscriber: subscriber)
}

func startUpdate() {
if updatesCounter == 0, asyncUpdatesCounter == 0 {
willSet.send()
willSetSubject.send()
}
updatesCounter &+= 1
}

func endUpdate() {
updatesCounter &-= 1
guard updatesCounter == 0 else { return }
didSet.send()
didSetSubject.send()

if asyncUpdatesCounter > 0 {
willSet.send()
willSetSubject.send()
}
}

Expand All @@ -119,15 +119,15 @@ private final class StoreRootBox<State>: Publisher {

private func startAsyncUpdate() {
if asyncUpdatesCounter == 0 {
willSet.send()
willSetSubject.send()
}
asyncUpdatesCounter &+= 1
}

private func endAsyncUpdate() {
asyncUpdatesCounter &-= 1
if asyncUpdatesCounter == 0 {
didSet.send()
didSetSubject.send()
}
}
}

0 comments on commit 2647867

Please sign in to comment.