Skip to content

Commit

Permalink
cleanup (#303)
Browse files Browse the repository at this point in the history
Patch
  • Loading branch information
muukii authored Sep 16, 2022
1 parent 5145829 commit 852f158
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 193 deletions.
18 changes: 15 additions & 3 deletions Sources/Verge/Library/InoutRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public final class InoutRef<Wrapped> {
*/
@dynamicMemberLookup
public enum Modification: Hashable, CustomDebugStringConvertible {
case determinate(keyPaths: Set<PartialKeyPath<Wrapped>>)
case determinate(
keyPaths: Set<PartialKeyPath<Wrapped>>
)
case indeterminate

public var debugDescription: String {
Expand Down Expand Up @@ -89,7 +91,10 @@ public final class InoutRef<Wrapped> {
return .indeterminate
}

return .determinate(keyPaths: nonatomic_modifiedKeyPaths)
return .determinate(
keyPaths: nonatomic_modifiedKeyPaths
)

}

private(set) var nonatomic_hasModified = false
Expand Down Expand Up @@ -228,7 +233,14 @@ public final class InoutRef<Wrapped> {
}
}

private func maskAsModified(on keyPath: PartialKeyPath<Wrapped>) {
@inline(__always)
private func maskAsModified<U>(on keyPath: KeyPath<Wrapped, U>) {
nonatomic_modifiedKeyPaths.insert(keyPath)
nonatomic_hasModified = true
}

@inline(__always)
private func maskAsModified<U>(on keyPath: KeyPath<Wrapped, U?>) {
nonatomic_modifiedKeyPaths.insert(keyPath)
nonatomic_hasModified = true
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Store/Changes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public final class Changes<Value: Equatable>: @unchecked Sendable, ChangesType,
// MARK: - Primitive methods

extension Changes {

/// Takes a composed value if it's changed from old value.
@inline(__always)
public func takeIfChanged<Composed>(
Expand Down
64 changes: 63 additions & 1 deletion Sources/Verge/Store/Pipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extension _MapPipelineType {
This will be helpful in performance. Therefore most type parameters require Equatable.
*/
public enum Pipelines {

/// KeyPath based pipeline, light weight operation just take value from source.
public struct SelectPipeline<Source: Equatable, Output: Equatable>: _SelectPipelineType {

Expand All @@ -131,6 +131,19 @@ public enum Pipelines {

public func yieldContinuously(_ input: Input) -> ContinuousResult<Output> {

// if let modification = input.modification {
// switch modification {
// case .indeterminate:
// break
// case .determinate(_, let changesKeyPaths):
//
// if changesKeyPaths.contains(keyPath) == false {
// return .noUpdates
// }
//
// }
// }

guard let previous = input.previous else {
return .new(input[keyPath: keyPath])
}
Expand Down Expand Up @@ -220,12 +233,57 @@ public enum Pipelines {
}

}

public struct BasicMapPipeline<Input: Equatable, Output: Equatable>: _PipelineType {

// MARK: - Properties

public let map: (Input) -> Output
public let additionalDropCondition: ((Input) -> Bool)?

public init(
map: @escaping (Input) -> Output,
additionalDropCondition: ((Input) -> Bool)?
) {
self.map = map
self.additionalDropCondition = additionalDropCondition
}

// MARK: - Functions

public func yieldContinuously(_ input: Input) -> ContinuousResult<Output> {

guard let additionalDropCondition = additionalDropCondition, additionalDropCondition(input) else {
return .new(yield(input))
}

return .noUpdates

}

public func yield(_ input: Input) -> Output {
map(input)
}

public func drop(while predicate: @escaping (Input) -> Bool) -> Self {
return .init(
map: map,
additionalDropCondition: additionalDropCondition.map { currentCondition in
{ input in
currentCondition(input) || predicate(input)
}
} ?? predicate
)
}

}

}

extension PipelineType {

/**
For Changes input
Produces output values using KeyPath-based projection.

exactly same with ``PipelineType/select(_:)``
Expand All @@ -236,6 +294,7 @@ extension PipelineType {
}

/**
For Changes input
Produces output values using KeyPath-based projection.

exactly same with ``PipelineType/map(_:)-7xvom``
Expand All @@ -249,6 +308,7 @@ extension PipelineType {
extension PipelineType {

/**
For Changes input
Produces output values using closure-based projection.
`map` closure takes the value projected from `using` closure which is intermediate value.
If the intermediate value is not changed, map closure won't perform.
Expand All @@ -266,6 +326,7 @@ extension PipelineType {
}

/**
For Changes input
Produces output values using closure-based projection.
Using Edge as intermediate, output value will be unwrapped value from the Edge.
*/
Expand All @@ -281,6 +342,7 @@ extension PipelineType {
}

/**
For Changes input
Produces output values using closure-based projection.

## 💡Tips
Expand Down
4 changes: 2 additions & 2 deletions Sources/Verge/Store/StoreMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Foundation
/**
Middleware enables us to do extra operations according to dispatched commits in Store.
*/
open class StoreMiddleware<State> {
open class StoreMiddleware<State: Equatable> {

open func mutate(state: inout InoutRef<State>) {

Expand All @@ -42,7 +42,7 @@ open class StoreMiddleware<State> {
/**
A closure-based middleware. It enables us to create middleware without creating sub-class.
*/
public final class AnonymousStoreMiddleware<State>: StoreMiddleware<State> {
public final class AnonymousStoreMiddleware<State: Equatable>: StoreMiddleware<State> {

private let _mutate: (inout InoutRef<State>) -> Void

Expand Down
142 changes: 0 additions & 142 deletions Sources/Verge/Utility/BatchCommit.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Sources/VergeORM/Derived+ORM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ extension DatabaseContext {
/// Experimental
/// TODO: More performant
public func _derivedQueriedEntities<Entity: EntityType>(
ids: @escaping (IndexesPropertyAdapter<Database>) -> any Collection<Entity.EntityID>,
ids: @escaping (IndexesPropertyAdapter<Database>) -> AnyCollection<Entity.EntityID>,
queue: TargetQueueType = .passthrough
) -> Derived<[Entity.Derived]> {

Expand Down Expand Up @@ -572,13 +572,13 @@ struct _DatabaseMultipleEntityPipeline<Source: Equatable, Database: DatabaseType

// TODO: write inline
private let noChangesComparer: Comparer<Database>
private let index: (IndexesPropertyAdapter<Database>) -> any Collection<Entity.EntityID>
private let index: (IndexesPropertyAdapter<Database>) -> AnyCollection<Entity.EntityID>
private let storage: CachedMapStorage<Entity.EntityID, Entity.Derived> = .init(keySelector: \.raw)
private let makeDerived: (Entity.EntityID) -> Entity.Derived

init(
keyPathToDatabase: KeyPath<Source, Database>,
index: @escaping (IndexesPropertyAdapter<Database>) -> any Collection<Entity.EntityID>,
index: @escaping (IndexesPropertyAdapter<Database>) -> AnyCollection<Entity.EntityID>,
makeDerived: @escaping (Entity.EntityID) -> Entity.Derived
) {

Expand Down
8 changes: 4 additions & 4 deletions Tests/VergeORMTests/DerivedCollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DerivedCollectionTests: XCTestCase {
}

let d = store.databases.db._derivedQueriedEntities(ids: { index in
index.allAuthros.prefix(3)
.init(index.allAuthros.prefix(3))
})

// FIXME: this fails, since the middleware doesn't care the order
Expand Down Expand Up @@ -86,7 +86,7 @@ class DerivedCollectionTests: XCTestCase {
}

let d = store.databases.db._derivedQueriedEntities(ids: { index in
index.allAuthros.filter { $0.raw.first == "1" }
.init(index.allAuthros.filter { $0.raw.first == "1" })
})

XCTAssertEqual(d.value.map { $0.value.wrapped?.entityID.raw }, ["1"])
Expand Down Expand Up @@ -118,7 +118,7 @@ class DerivedCollectionTests: XCTestCase {
}

let d = store.databases.db._derivedQueriedEntities(ids: { index in
index.allAuthros.filter { _ in return true }
.init(index.allAuthros.filter { _ in return true })
})

let tmp = d.value.primitive
Expand All @@ -145,7 +145,7 @@ class DerivedCollectionTests: XCTestCase {
var updateCount = 0

let d = store.databases.db._derivedQueriedEntities(ids: { index in
index.allAuthros.filter { _ in return true }
.init(index.allAuthros.filter { _ in return true })
})

d.sinkValue { _ in
Expand Down
Loading

0 comments on commit 852f158

Please sign in to comment.