Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolated Store wrapper #437

Merged
merged 8 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions Sources/Verge/Derived/Derived.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import class Foundation.NSMapTable
import class Foundation.NSString

#if canImport(Combine)
import Combine
#endif
Expand All @@ -32,6 +29,20 @@ public protocol DerivedType<State>: StoreType {
func asDerived() -> Derived<Value>
}

public protocol DerivedMaking {

associatedtype State: Equatable

// TODO: Remove
var state: Changes<State> { get }

func derived<Pipeline: PipelineType>(
_ pipeline: Pipeline,
queue: some TargetQueueType
) -> Derived<Pipeline.Output> where Pipeline.Input == Changes<State>

}

/**
A container object that provides the current value and changes from the source Store.

Expand Down Expand Up @@ -110,8 +121,8 @@ public class Derived<Value: Equatable>: Store<Value, Never>, DerivedType, @unche
}

// TODO: Take over state.modification & state.mutation
indirectSelf.commit("Derived") {
$0._transaction.isDerivedFromUpstream = true
indirectSelf._receive {
$1.isDerivedFromUpstream = true
$0.append(traces: value.traces)
$0.replace(with: newState)
}
Expand Down Expand Up @@ -218,7 +229,7 @@ extension Derived where Value : Equatable {
receive: @escaping (Value) -> Void
) -> StoreSubscription {
sinkState(dropsFirst: dropsFirst, queue: queue) { (changes) in
changes.ifChanged { value in
changes.ifChanged().do { value in
receive(value)
}
}
Expand All @@ -235,7 +246,7 @@ extension Derived where Value : Equatable {
receive: @escaping @MainActor (Value) -> Void
) -> StoreSubscription {
sinkState(dropsFirst: dropsFirst, queue: queue) { @MainActor changes in
changes.ifChanged { value in
changes.ifChanged().do { value in
receive(value)
}
}
Expand Down
2 changes: 0 additions & 2 deletions Sources/Verge/Library/InoutRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ public final class InoutRef<Wrapped> {
}
}

public var _transaction: Transaction = .init()

// MARK: - Initializers

/**
Expand Down
4 changes: 2 additions & 2 deletions Sources/Verge/Store/DispatcherType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ extension DispatcherType {
)

return try store.asStore()._receive(
mutation: { state -> Result in
mutation: { state, transaction -> Result in
try state.map(keyPath: scope) { (ref: inout InoutRef<Scope>) -> Result in
ref.append(trace: trace)
return try mutation(&ref)
Expand Down Expand Up @@ -335,7 +335,7 @@ extension DispatcherType {
mutation: (inout InoutRef<Scope>) throws -> Result
) rethrows -> Result where Scope == WrappedStore.State {
return try store.asStore()._receive(
mutation: { ref -> Result in
mutation: { ref, transaction -> Result in
ref.append(trace: trace)
return try mutation(&ref)
}
Expand Down
Loading
Loading