Skip to content

Commit

Permalink
Fix warnings (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Apr 9, 2024
1 parent 7edcee7 commit e3865ba
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Sources/Verge/Library/EventEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

@_implementationOnly import Atomics
import Atomics
import Combine
import Foundation
import os
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Library/StoreActivitySubscription.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Combine
@_implementationOnly import Atomics
import Atomics

/**
A subscription that is compatible with Combine’s Cancellable.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Library/StoreStateSubscription.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Combine
@_implementationOnly import Atomics
import Atomics

@available(*, deprecated, message: "StoreSubscription has separated into StoreStateSubscription and StoreActivitySubscription.")
public typealias StoreSubscription = Cancellable
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Library/VergeAnyCancellable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Combine
@_implementationOnly import Atomics
import Atomics

/// A typealias to `Set<AnyCancellable>`.
public typealias VergeAnyCancellables = Set<AnyCancellable>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Store/AnyTargetQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// THE SOFTWARE.

import Foundation
@_implementationOnly import Atomics
import Atomics

public protocol TargetQueueType {
func execute(_ workItem: @escaping () -> Void)
Expand Down
39 changes: 39 additions & 0 deletions Sources/Verge/Store/Changes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,33 @@ extension Changes {

}

@inline(__always)
fileprivate func _takeIfChanged_packed_nonEquatable<each Element>(
_ compose: (Value) throws -> (repeat each Element),
comparator: some Comparison<(repeat each Element)>
) rethrows -> (repeat each Element)? {

let current = self.primitive

guard let previousValue = previous else {
return try compose(consume current)
}

let old = previousValue.primitive

let composedFromCurrent = try compose(consume current)
let composedFromOld = try compose(old)

let isEqual = comparator(composedFromOld, composedFromCurrent)

guard isEqual == false else {
return nil
}

return composedFromCurrent

}

/// Performs a closure if the selected value changed from the previous one.
///
/// - Parameters:
Expand Down Expand Up @@ -376,6 +403,7 @@ extension Changes {
/**
Performs a closure if the selected value changed from the previous one.
*/
@available(*, deprecated, message: "Use another function that returns IfChangedBox")
public func ifChanged<T, Result>(
_ selector: ChangesKeyPath<T>,
_ comparer: some Comparison<T>,
Expand Down Expand Up @@ -416,6 +444,17 @@ extension Changes {
return .init(value: (repeat each result))
}

public borrowing func ifChanged<each Element>(
_ compose: (borrowing Value) -> (repeat each Element),
comparator: some Comparison<(repeat each Element)>
) -> IfChangedBox<(repeat each Element)> {
guard let result = _takeIfChanged_packed_nonEquatable(compose, comparator: comparator) else {
return .init()
}

return .init(value: (repeat each result))
}

/**
singular variant
*/
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Foundation
import os.log
import ConcurrencyTaskManager

@_implementationOnly import Atomics
import Atomics

#if canImport(Combine)
import Combine
Expand Down
28 changes: 24 additions & 4 deletions Sources/Verge/SwiftUI/OnReceive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ extension View {
/// - Parameters:
/// - instance: The `DispatcherType` instance to subscribe to.
/// - perform: A closure to execute when the `State` changes.
public func onReceiveState<D: StoreDriverType>(_ instance: D, perform: @escaping @MainActor (Changes<D.TargetStore.State>) -> Void) -> some View {
onReceive(instance.store.asStore()._statePublisher().receive(on: DispatchQueue.main), perform: perform)
public func onReceiveState<D: StoreDriverType>(
_ instance: D,
perform: @escaping @MainActor (Changes<D.TargetStore.State>) -> Void
) -> some View {
onReceive(
instance.store.asStore()._statePublisher().receive(on: DispatchQueue.main),
perform: { value in
MainActor.assumeIsolated {
perform(value)
}
}
)
}

/// Adds an action to perform when the specified `DispatcherType` publishes an activity.
Expand All @@ -46,8 +56,18 @@ extension View {
/// - Parameters:
/// - instance: The `DispatcherType` instance to subscribe to.
/// - perform: A closure to execute when the `Activity` is published.
public func onReceiveActivity<D: StoreDriverType>(_ instance: D, perform: @escaping @MainActor (D.TargetStore.Activity) -> Void) -> some View {
onReceive(instance.store.asStore()._activityPublisher().receive(on: DispatchQueue.main), perform: perform)
public func onReceiveActivity<D: StoreDriverType>(
_ instance: D,
perform: @escaping @MainActor (D.TargetStore.Activity) -> Void
) -> some View {
onReceive(
instance.store.asStore()._activityPublisher().receive(on: DispatchQueue.main),
perform: { value in
MainActor.assumeIsolated {
perform(value)
}
}
)
}

}
2 changes: 1 addition & 1 deletion Sources/Verge/SwiftUI/StoreObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ enum Preview_StoreObject: PreviewProvider {
}
}

final class ViewModel: StoreComponentType {
final class ViewModel: StoreDriverType {

struct State: Equatable {
var count: Int = 0
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Utility/Edge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// THE SOFTWARE.

import Foundation
@_implementationOnly import Atomics
import Atomics

public protocol EdgeType : Equatable {
associatedtype State
Expand Down
2 changes: 1 addition & 1 deletion Sources/VergeComparator/PackedCompare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public func areEqual<each Element: Equatable>(_ lhs: (repeat each Element), _ rh

// https://github.com/apple/swift-evolution/blob/main/proposals/0408-pack-iteration.md


func isEqual<T: Equatable>(_ left: T, _ right: T) throws {
if left == right {
return
Expand All @@ -21,3 +20,4 @@ public func areEqual<each Element: Equatable>(_ lhs: (repeat each Element), _ rh

return true
}

2 changes: 1 addition & 1 deletion Sources/VergeNormalization/Tables/TableType.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import VergeTypedIdentifier

/// a storage of the entity
public protocol TableType<Entity>: Equatable {
public protocol TableType<Entity>: Equatable, Sendable {

associatedtype Entity: EntityType

Expand Down
2 changes: 1 addition & 1 deletion Sources/VergeRx/Store+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extension Reactive where Base : StoreDriverType {

}

extension StoreWrapperType {
extension StoreDriverType {

@_disfavoredOverload
public var rx: Reactive<Self> {
Expand Down
2 changes: 1 addition & 1 deletion Tests/VergeNormalizationDerivedTests/CombiningTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class CombiningTests: XCTestCase {
$0.count += 1
}

store.commit {
_ = store.commit {
$0.db.performBatchUpdates { t in
t.modifying.author.insert(Author(rawID: "1", name: "Hiroshi Kimura"))
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/VergeTests/DerivedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ final class DerivedTests: XCTestCase {

updateCount.fulfill()

changes.ifChanged(\.0) { _0 in
changes.ifChanged(\.0).do { _0 in
update0.fulfill()
}

changes.ifChanged(\.1) { _1 in
changes.ifChanged(\.1).do { _1 in
update1.fulfill()
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/VergeTests/StoreAndDerivedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class StoreAndDerivedTests: XCTestCase {

let store = Store<_, Never>(initialState: DemoState())

let nameDerived = store.derived(.select(\.name))
let _ = store.derived(.select(\.name))
let countDerived = store.derived(.select(\.count))

await withTaskGroup(of: Void.self) { group in
Expand Down
4 changes: 2 additions & 2 deletions Tests/VergeTests/StoreMiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ final class StoreMiddlewareTests: XCTestCase {
let store = DemoStore()

store.add(middleware: .modify { @Sendable modifyingState, current in
current.ifChanged(\.count) { _ in
current.ifChanged(\.count).do { _ in
modifyingState.count += 1
}
})

store.add(middleware: .modify { @Sendable modifyingState, current in
current.ifChanged(\.name) { _ in
current.ifChanged(\.name).do { _ in
modifyingState.count = 100
}
})
Expand Down
9 changes: 4 additions & 5 deletions Tests/VergeTests/SyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@ enum SyntaxTests {

let changes: Changes<DemoState> = .init(old: nil, new: .init())

changes.ifChanged(\.name) { name in
changes.ifChanged(\.name).do { name in

}

// changes.ifChanged({ ($0.name, $0.name) }) { args in
// }

changes.ifChanged(\.nonEquatable, .alwaysFalse()) { name in
changes.ifChanged({ $0.nonEquatable }, comparator: .alwaysFalse()).do { name in

}


changes.ifChanged({ $0.name }) { name in
changes.ifChanged({ $0.name }).do { name in

}

changes.ifChanged(\.name, \.count) { name, count in
changes.ifChanged(\.name, \.count).do { name, count in

}

Expand Down
4 changes: 2 additions & 2 deletions Tests/VergeTests/Usage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class SpecificService: StoreDriverType {

}

final class ViewModel: StoreComponentType {
final class ViewModel: StoreDriverType {

struct State: Equatable {}

Expand All @@ -62,7 +62,7 @@ final class ViewModel: StoreComponentType {

// MARK: - Abstraction

protocol MyViewModelType: StoreComponentType where TargetStore.State == String, TargetStore.Activity == Int {
protocol MyViewModelType: StoreDriverType where TargetStore.State == String, TargetStore.Activity == Int {

}

Expand Down

0 comments on commit e3865ba

Please sign in to comment.